File indexing completed on 2025-01-18 09:30:37
0001 #ifndef DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
0002 #define DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "boost/date_time/locale_config.hpp" // set BOOST_DATE_TIME_NO_LOCALE
0014
0015 #ifndef BOOST_DATE_TIME_NO_LOCALE
0016
0017 #include "boost/date_time/iso_format.hpp"
0018 #include "boost/date_time/date_names_put.hpp"
0019 #include "boost/date_time/parse_format_base.hpp"
0020 #include <boost/io/ios_state.hpp>
0021
0022 #include <sstream>
0023 #include <iomanip>
0024
0025
0026 namespace boost {
0027 namespace date_time {
0028
0029
0030 template<class facet_type,
0031 class charT = char>
0032 class ostream_month_formatter
0033 {
0034 public:
0035 typedef typename facet_type::month_type month_type;
0036 typedef std::basic_ostream<charT> ostream_type;
0037
0038
0039 static void format_month(const month_type& month,
0040 ostream_type& os,
0041 const facet_type& f)
0042 {
0043
0044 switch (f.month_format())
0045 {
0046 case month_as_short_string:
0047 {
0048 std::ostreambuf_iterator<charT> oitr(os);
0049 f.put_month_short(oitr, month.as_enum());
0050 break;
0051 }
0052 case month_as_long_string:
0053 {
0054 std::ostreambuf_iterator<charT> oitr(os);
0055 f.put_month_long(oitr, month.as_enum());
0056 break;
0057 }
0058 case month_as_integer:
0059 {
0060 boost::io::basic_ios_fill_saver<charT> ifs(os);
0061 os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
0062 break;
0063 }
0064
0065 }
0066 }
0067
0068 };
0069
0070
0071
0072 template<class weekday_type,
0073 class facet_type,
0074 class charT = char>
0075 class ostream_weekday_formatter
0076 {
0077 public:
0078 typedef typename facet_type::month_type month_type;
0079 typedef std::basic_ostream<charT> ostream_type;
0080
0081
0082 static void format_weekday(const weekday_type& wd,
0083 ostream_type& os,
0084 const facet_type& f,
0085 bool as_long_string)
0086 {
0087
0088 std::ostreambuf_iterator<charT> oitr(os);
0089 if (as_long_string) {
0090 f.put_weekday_long(oitr, wd.as_enum());
0091 }
0092 else {
0093 f.put_weekday_short(oitr, wd.as_enum());
0094 }
0095
0096 }
0097
0098 };
0099
0100
0101
0102 template<class ymd_type,
0103 class facet_type,
0104 class charT = char>
0105 class ostream_ymd_formatter
0106 {
0107 public:
0108 typedef typename ymd_type::month_type month_type;
0109 typedef ostream_month_formatter<facet_type, charT> month_formatter_type;
0110 typedef std::basic_ostream<charT> ostream_type;
0111 typedef std::basic_string<charT> foo_type;
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 static void ymd_put(ymd_type ymd,
0133 ostream_type& os,
0134 const facet_type& f)
0135 {
0136 boost::io::basic_ios_fill_saver<charT> ifs(os);
0137 std::ostreambuf_iterator<charT> oitr(os);
0138 switch (f.date_order()) {
0139 case ymd_order_iso: {
0140 os << ymd.year;
0141 if (f.has_date_sep_chars()) {
0142 f.month_sep_char(oitr);
0143 }
0144 month_formatter_type::format_month(ymd.month, os, f);
0145 if (f.has_date_sep_chars()) {
0146 f.day_sep_char(oitr);
0147 }
0148 os << std::setw(2) << std::setfill(os.widen('0'))
0149 << ymd.day;
0150 break;
0151 }
0152 case ymd_order_us: {
0153 month_formatter_type::format_month(ymd.month, os, f);
0154 if (f.has_date_sep_chars()) {
0155 f.day_sep_char(oitr);
0156 }
0157 os << std::setw(2) << std::setfill(os.widen('0'))
0158 << ymd.day;
0159 if (f.has_date_sep_chars()) {
0160 f.month_sep_char(oitr);
0161 }
0162 os << ymd.year;
0163 break;
0164 }
0165 case ymd_order_dmy: {
0166 os << std::setw(2) << std::setfill(os.widen('0'))
0167 << ymd.day;
0168 if (f.has_date_sep_chars()) {
0169 f.day_sep_char(oitr);
0170 }
0171 month_formatter_type::format_month(ymd.month, os, f);
0172 if (f.has_date_sep_chars()) {
0173 f.month_sep_char(oitr);
0174 }
0175 os << ymd.year;
0176 break;
0177 }
0178 }
0179 }
0180 };
0181
0182
0183
0184 template<class date_type,
0185 class facet_type,
0186 class charT = char>
0187 class ostream_date_formatter
0188 {
0189 public:
0190 typedef std::basic_ostream<charT> ostream_type;
0191 typedef typename date_type::ymd_type ymd_type;
0192
0193
0194 static void date_put(const date_type& d,
0195 ostream_type& os,
0196 const facet_type& f)
0197 {
0198 special_values sv = d.as_special();
0199 if (sv == not_special) {
0200 ymd_type ymd = d.year_month_day();
0201 ostream_ymd_formatter<ymd_type, facet_type, charT>::ymd_put(ymd, os, f);
0202 }
0203 else {
0204 std::ostreambuf_iterator<charT> coi(os);
0205 f.put_special_value(coi, sv);
0206 }
0207 }
0208
0209
0210
0211 static void date_put(const date_type& d,
0212 ostream_type& os)
0213 {
0214
0215 std::locale locale = os.getloc();
0216 if (std::has_facet<facet_type>(locale)) {
0217 const facet_type& f = std::use_facet<facet_type>(locale);
0218 date_put(d, os, f);
0219 }
0220 else {
0221
0222 facet_type default_facet;
0223 date_put(d, os, default_facet);
0224 }
0225 }
0226 };
0227
0228
0229 } }
0230
0231 #endif
0232
0233 #endif
0234