File indexing completed on 2025-01-18 09:30:34
0001 #ifndef GREGORIAN_FACET_HPP___
0002 #define GREGORIAN_FACET_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <boost/date_time/compiler_config.hpp>
0013 #include <boost/date_time/gregorian/gregorian_types.hpp>
0014 #include <boost/date_time/date_formatting_locales.hpp> // sets BOOST_DATE_TIME_NO_LOCALE
0015 #include <boost/date_time/gregorian/parsers.hpp>
0016 #include <boost/io/ios_state.hpp>
0017
0018
0019 #ifndef BOOST_DATE_TIME_NO_LOCALE
0020
0021 #include <string>
0022 #include <memory>
0023 #include <locale>
0024 #include <iostream>
0025 #include <exception>
0026
0027 namespace boost {
0028 namespace gregorian {
0029
0030
0031 struct BOOST_SYMBOL_VISIBLE greg_facet_config
0032 {
0033 typedef boost::gregorian::greg_month month_type;
0034 typedef boost::date_time::special_values special_value_enum;
0035 typedef boost::gregorian::months_of_year month_enum;
0036 typedef boost::date_time::weekdays weekday_enum;
0037 };
0038
0039 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
0040
0041 typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
0042
0043
0044
0045
0046
0047
0048
0049
0050 template <class charT, class traits>
0051 inline
0052 std::basic_ostream<charT, traits>&
0053 operator<<(std::basic_ostream<charT, traits>& os, const date& d)
0054 {
0055 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0056 typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
0057 greg_ostream_formatter::date_put(d, os);
0058 return os;
0059 }
0060
0061
0062
0063
0064
0065
0066 template <class charT, class traits>
0067 inline
0068 std::basic_ostream<charT, traits>&
0069 operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
0070 {
0071 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0072 typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
0073 std::locale locale = os.getloc();
0074 if (std::has_facet<facet_def>(locale)) {
0075 const facet_def& f = std::use_facet<facet_def>(locale);
0076 greg_month_formatter::format_month(m, os, f);
0077
0078 }
0079 else {
0080 boost::io::basic_ios_fill_saver<charT> ifs(os);
0081 os << std::setw(2) << std::setfill(os.widen('0')) << m.as_number();
0082 }
0083
0084 return os;
0085 }
0086
0087
0088
0089
0090
0091
0092 template <class charT, class traits>
0093 inline
0094 std::basic_ostream<charT, traits>&
0095 operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
0096 {
0097 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0098 typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
0099 std::locale locale = os.getloc();
0100 if (std::has_facet<facet_def>(locale)) {
0101 const facet_def& f = std::use_facet<facet_def>(locale);
0102 greg_weekday_formatter::format_weekday(wd, os, f, true);
0103 }
0104 else {
0105 os << wd.as_short_string();
0106 }
0107
0108 return os;
0109 }
0110
0111
0112
0113
0114
0115
0116
0117 template <class charT, class traits>
0118 inline
0119 std::basic_ostream<charT, traits>&
0120 operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
0121 {
0122 os << '[';
0123 os << dp.begin();
0124 os << '/';
0125 os << dp.last();
0126 os << ']';
0127 return os;
0128 }
0129
0130 template <class charT, class traits>
0131 inline
0132 std::basic_ostream<charT, traits>&
0133 operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
0134 {
0135
0136 os << dd.get_rep();
0137 return os;
0138 }
0139
0140
0141 template <class charT, class traits>
0142 inline
0143 std::basic_ostream<charT, traits>&
0144 operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
0145 {
0146 boost::io::basic_ios_fill_saver<charT> ifs(os);
0147 os << std::setw(2) << std::setfill(os.widen('0')) << pd.day() << ' '
0148 << pd.month().as_short_string() ;
0149 return os;
0150 }
0151
0152
0153 template <class charT, class traits>
0154 inline
0155 std::basic_ostream<charT, traits>&
0156 operator<<(std::basic_ostream<charT, traits>& os,
0157 const nth_kday_of_month& nkd)
0158 {
0159 os << nkd.nth_week_as_str() << ' '
0160 << nkd.day_of_week() << " of "
0161 << nkd.month().as_short_string() ;
0162 return os;
0163 }
0164
0165
0166 template <class charT, class traits>
0167 inline
0168 std::basic_ostream<charT, traits>&
0169 operator<<(std::basic_ostream<charT, traits>& os,
0170 const first_kday_of_month& fkd)
0171 {
0172 os << "first " << fkd.day_of_week() << " of "
0173 << fkd.month().as_short_string() ;
0174 return os;
0175 }
0176
0177
0178 template <class charT, class traits>
0179 inline
0180 std::basic_ostream<charT, traits>&
0181 operator<<(std::basic_ostream<charT, traits>& os,
0182 const last_kday_of_month& lkd)
0183 {
0184 os << "last " << lkd.day_of_week() << " of "
0185 << lkd.month().as_short_string() ;
0186 return os;
0187 }
0188
0189
0190 template <class charT, class traits>
0191 inline
0192 std::basic_ostream<charT, traits>&
0193 operator<<(std::basic_ostream<charT, traits>& os,
0194 const first_kday_after& fka)
0195 {
0196 os << fka.day_of_week() << " after";
0197 return os;
0198 }
0199
0200
0201 template <class charT, class traits>
0202 inline
0203 std::basic_ostream<charT, traits>&
0204 operator<<(std::basic_ostream<charT, traits>& os,
0205 const first_kday_before& fkb)
0206 {
0207 os << fkb.day_of_week() << " before";
0208 return os;
0209 }
0210 #endif
0211
0212
0213 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
0214
0215 template<class charT>
0216 inline
0217 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
0218 {
0219 std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
0220 d = from_stream(beg, eos);
0221 return is;
0222 }
0223 #endif
0224
0225
0226 template<class charT>
0227 inline
0228 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
0229 date_duration& dd)
0230 {
0231 long v;
0232 is >> v;
0233 dd = date_duration(v);
0234 return is;
0235 }
0236
0237
0238 template<class charT>
0239 inline
0240 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
0241 date_period& dp)
0242 {
0243 std::basic_string<charT> s;
0244 is >> s;
0245 dp = date_time::from_simple_string_type<date>(s);
0246 return is;
0247 }
0248
0249
0250 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
0251
0252
0253
0254
0255 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
0256
0257 #ifndef BOOST_NO_STD_WSTRING
0258
0259 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
0260
0261
0262
0263 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
0264 #endif
0265
0266
0267 template<class charT>
0268 inline
0269 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
0270 {
0271 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
0272
0273 std::basic_string<charT> s;
0274 is >> s;
0275
0276 if(!std::has_facet<facet_def>(is.getloc())) {
0277 std::locale loc = is.getloc();
0278 charT a = '\0';
0279 is.imbue(generate_locale(loc, a));
0280 }
0281
0282 short num = 0;
0283
0284 try{
0285 const facet_def& f = std::use_facet<facet_def>(is.getloc());
0286 num = date_time::find_match(f.get_short_month_names(),
0287 f.get_long_month_names(),
0288 (greg_month::max)(), s);
0289
0290 }
0291
0292
0293
0294 catch(std::bad_cast&){
0295 charT a = '\0';
0296
0297 #if defined(BOOST_NO_CXX11_SMART_PTR)
0298
0299 std::auto_ptr< const facet_def > f(create_facet_def(a));
0300
0301 #else
0302
0303 std::unique_ptr< const facet_def > f(create_facet_def(a));
0304
0305 #endif
0306
0307 num = date_time::find_match(f->get_short_month_names(),
0308 f->get_long_month_names(),
0309 (greg_month::max)(), s);
0310
0311 }
0312
0313 ++num;
0314 m = greg_month(num);
0315
0316 return is;
0317 }
0318
0319
0320 template<class charT>
0321 inline
0322 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
0323 {
0324 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
0325
0326 std::basic_string<charT> s;
0327 is >> s;
0328
0329 if(!std::has_facet<facet_def>(is.getloc())) {
0330 std::locale loc = is.getloc();
0331 charT a = '\0';
0332 is.imbue(generate_locale(loc, a));
0333 }
0334
0335 short num = 0;
0336 try{
0337 const facet_def& f = std::use_facet<facet_def>(is.getloc());
0338 num = date_time::find_match(f.get_short_weekday_names(),
0339 f.get_long_weekday_names(),
0340 (greg_weekday::max)() + 1, s);
0341
0342 }
0343
0344
0345
0346 catch(std::bad_cast&){
0347 charT a = '\0';
0348
0349 #if defined(BOOST_NO_CXX11_SMART_PTR)
0350
0351 std::auto_ptr< const facet_def > f(create_facet_def(a));
0352
0353 #else
0354
0355 std::unique_ptr< const facet_def > f(create_facet_def(a));
0356
0357 #endif
0358
0359 num = date_time::find_match(f->get_short_weekday_names(),
0360 f->get_long_weekday_names(),
0361 (greg_weekday::max)() + 1, s);
0362
0363 }
0364
0365 wd = greg_weekday(num);
0366 return is;
0367 }
0368
0369 } }
0370
0371 #endif
0372
0373 #endif
0374