1 #ifndef GREGORIAN_FORMATTERS_HPP___
2 #define GREGORIAN_FORMATTERS_HPP___
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
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"
17 #include "boost/date_time/date_formatting.hpp"
19 #include "boost/date_time/iso_format.hpp"
20 #include "boost/date_time/date_format_simple.hpp"
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
30 // wrapper function for to_simple_(w)string(date)
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
);
36 //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01
37 /*!\ingroup date_format
39 inline std::string
to_simple_string(const date
& d
) {
40 return to_simple_string_type
<char>(d
);
44 // wrapper function for to_simple_(w)string(date_period)
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
=']';
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
);
54 //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02]
55 /*!\ingroup date_format
57 inline std::string
to_simple_string(const date_period
& d
) {
58 return to_simple_string_type
<char>(d
);
61 // wrapper function for to_iso_(w)string(date_period)
63 inline std::basic_string
<charT
> to_iso_string_type(const date_period
& d
) {
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());
68 //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231
69 /*!\ingroup date_format
71 inline std::string
to_iso_string(const date_period
& d
) {
72 return to_iso_string_type
<char>(d
);
76 // wrapper function for to_iso_extended_(w)string(date)
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
);
81 //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31
82 /*!\ingroup date_format
84 inline std::string
to_iso_extended_string(const date
& d
) {
85 return to_iso_extended_string_type
<char>(d
);
88 // wrapper function for to_iso_(w)string(date)
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
);
93 //! Convert to iso standard string YYYYMMDD. Example: 20021231
94 /*!\ingroup date_format
96 inline std::string
to_iso_string(const date
& d
) {
97 return to_iso_string_type
<char>(d
);
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
)
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
113 << std::setw(2) << std::setfill(ss
.widen('0'))
117 inline std::string
to_sql_string(const date
& d
) {
118 return to_sql_string_type
<char>(d
);
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
126 inline std::wstring
to_simple_wstring(const date_period
& d
) {
127 return to_simple_string_type
<wchar_t>(d
);
129 //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01
130 /*!\ingroup date_format
132 inline std::wstring
to_simple_wstring(const date
& d
) {
133 return to_simple_string_type
<wchar_t>(d
);
135 //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231
136 /*!\ingroup date_format
138 inline std::wstring
to_iso_wstring(const date_period
& d
) {
139 return to_iso_string_type
<wchar_t>(d
);
141 //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31
142 /*!\ingroup date_format
144 inline std::wstring
to_iso_extended_wstring(const date
& d
) {
145 return to_iso_extended_string_type
<wchar_t>(d
);
147 //! Convert to iso standard string YYYYMMDD. Example: 20021231
148 /*!\ingroup date_format
150 inline std::wstring
to_iso_wstring(const date
& d
) {
151 return to_iso_string_type
<wchar_t>(d
);
153 inline std::wstring
to_sql_wstring(const date
& d
) {
154 return to_sql_string_type
<wchar_t>(d
);
156 #endif // BOOST_NO_STD_WSTRING
158 } } //namespace gregorian