1 | #ifndef GREGORIAN_FORMATTERS_HPP___ |
2 | #define GREGORIAN_FORMATTERS_HPP___ |
3 | |
4 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
5 | * Use, modification and distribution is subject to the |
6 | * Boost Software License, Version 1.0. (See accompanying |
7 | * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
8 | * Author: Jeff Garland, Bart Garst |
9 | * $Date$ |
10 | */ |
11 | |
12 | #include "boost/date_time/compiler_config.hpp" |
13 | #include "boost/date_time/gregorian/gregorian_types.hpp" |
14 | #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS) |
15 | #include "boost/date_time/date_formatting_limited.hpp" |
16 | #else |
17 | #include "boost/date_time/date_formatting.hpp" |
18 | #endif |
19 | #include "boost/date_time/iso_format.hpp" |
20 | #include "boost/date_time/date_format_simple.hpp" |
21 | |
22 | /* NOTE: "to_*_string" code for older compilers, ones that define |
23 | * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in |
24 | * formatters_limited.hpp |
25 | */ |
26 | |
27 | namespace boost { |
28 | namespace gregorian { |
29 | |
30 | // wrapper function for to_simple_(w)string(date) |
31 | template<class charT> |
32 | inline |
33 | std::basic_string<charT> to_simple_string_type(const date& d) { |
34 | return date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d); |
35 | } |
36 | //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01 |
37 | /*!\ingroup date_format |
38 | */ |
39 | inline std::string to_simple_string(const date& d) { |
40 | return to_simple_string_type<char>(d); |
41 | } |
42 | |
43 | |
44 | // wrapper function for to_simple_(w)string(date_period) |
45 | template<class charT> |
46 | inline std::basic_string<charT> to_simple_string_type(const date_period& d) { |
47 | typedef std::basic_string<charT> string_type; |
48 | charT b = '[', m = '/', e=']'; |
49 | |
50 | string_type d1(date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d.begin())); |
51 | string_type d2(date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d.last())); |
52 | return string_type(b + d1 + m + d2 + e); |
53 | } |
54 | //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02] |
55 | /*!\ingroup date_format |
56 | */ |
57 | inline std::string to_simple_string(const date_period& d) { |
58 | return to_simple_string_type<char>(d); |
59 | } |
60 | |
61 | // wrapper function for to_iso_(w)string(date_period) |
62 | template<class charT> |
63 | inline std::basic_string<charT> to_iso_string_type(const date_period& d) { |
64 | charT sep = '/'; |
65 | std::basic_string<charT> s(date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d.begin())); |
66 | return s + sep + date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d.last()); |
67 | } |
68 | //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231 |
69 | /*!\ingroup date_format |
70 | */ |
71 | inline std::string to_iso_string(const date_period& d) { |
72 | return to_iso_string_type<char>(d); |
73 | } |
74 | |
75 | |
76 | // wrapper function for to_iso_extended_(w)string(date) |
77 | template<class charT> |
78 | inline std::basic_string<charT> to_iso_extended_string_type(const date& d) { |
79 | return date_time::date_formatter<date,date_time::iso_extended_format<charT>,charT>::date_to_string(d); |
80 | } |
81 | //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31 |
82 | /*!\ingroup date_format |
83 | */ |
84 | inline std::string to_iso_extended_string(const date& d) { |
85 | return to_iso_extended_string_type<char>(d); |
86 | } |
87 | |
88 | // wrapper function for to_iso_(w)string(date) |
89 | template<class charT> |
90 | inline std::basic_string<charT> to_iso_string_type(const date& d) { |
91 | return date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d); |
92 | } |
93 | //! Convert to iso standard string YYYYMMDD. Example: 20021231 |
94 | /*!\ingroup date_format |
95 | */ |
96 | inline std::string to_iso_string(const date& d) { |
97 | return to_iso_string_type<char>(d); |
98 | } |
99 | |
100 | |
101 | |
102 | |
103 | // wrapper function for to_sql_(w)string(date) |
104 | template<class charT> |
105 | inline std::basic_string<charT> to_sql_string_type(const date& d) |
106 | { |
107 | date::ymd_type ymd = d.year_month_day(); |
108 | std::basic_ostringstream<charT> ss; |
109 | ss << ymd.year << "-" |
110 | << std::setw(2) << std::setfill(ss.widen('0')) |
111 | << ymd.month.as_number() //solves problem with gcc 3.1 hanging |
112 | << "-" |
113 | << std::setw(2) << std::setfill(ss.widen('0')) |
114 | << ymd.day; |
115 | return ss.str(); |
116 | } |
117 | inline std::string to_sql_string(const date& d) { |
118 | return to_sql_string_type<char>(d); |
119 | } |
120 | |
121 | |
122 | #if !defined(BOOST_NO_STD_WSTRING) |
123 | //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02] |
124 | /*!\ingroup date_format |
125 | */ |
126 | inline std::wstring to_simple_wstring(const date_period& d) { |
127 | return to_simple_string_type<wchar_t>(d); |
128 | } |
129 | //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01 |
130 | /*!\ingroup date_format |
131 | */ |
132 | inline std::wstring to_simple_wstring(const date& d) { |
133 | return to_simple_string_type<wchar_t>(d); |
134 | } |
135 | //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231 |
136 | /*!\ingroup date_format |
137 | */ |
138 | inline std::wstring to_iso_wstring(const date_period& d) { |
139 | return to_iso_string_type<wchar_t>(d); |
140 | } |
141 | //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31 |
142 | /*!\ingroup date_format |
143 | */ |
144 | inline std::wstring to_iso_extended_wstring(const date& d) { |
145 | return to_iso_extended_string_type<wchar_t>(d); |
146 | } |
147 | //! Convert to iso standard string YYYYMMDD. Example: 20021231 |
148 | /*!\ingroup date_format |
149 | */ |
150 | inline std::wstring to_iso_wstring(const date& d) { |
151 | return to_iso_string_type<wchar_t>(d); |
152 | } |
153 | inline std::wstring to_sql_wstring(const date& d) { |
154 | return to_sql_string_type<wchar_t>(d); |
155 | } |
156 | #endif // BOOST_NO_STD_WSTRING |
157 | |
158 | } } //namespace gregorian |
159 | |
160 | |
161 | #endif |
162 | |
163 | |