Chris@16: #ifndef GREGORIAN_FACET_HPP___ Chris@16: #define GREGORIAN_FACET_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include "boost/date_time/gregorian/gregorian_types.hpp" Chris@16: #include "boost/date_time/date_formatting_locales.hpp" // sets BOOST_DATE_TIME_NO_LOCALE Chris@16: #include "boost/date_time/gregorian/parsers.hpp" Chris@16: Chris@16: //This file is basically commented out if locales are not supported Chris@16: #ifndef BOOST_DATE_TIME_NO_LOCALE Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace gregorian { Chris@16: Chris@16: //! Configuration of the output facet template Chris@16: struct greg_facet_config Chris@16: { Chris@16: typedef boost::gregorian::greg_month month_type; Chris@16: typedef boost::date_time::special_values special_value_enum; Chris@16: typedef boost::gregorian::months_of_year month_enum; Chris@16: typedef boost::date_time::weekdays weekday_enum; Chris@16: }; Chris@16: Chris@16: #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO) Chris@16: //! Create the base facet type for gregorian::date Chris@16: typedef boost::date_time::date_names_put greg_base_facet; Chris@16: Chris@16: //! ostream operator for gregorian::date Chris@16: /*! Uses the date facet to determine various output parameters including: Chris@16: * - string values for the month (eg: Jan, Feb, Mar) (default: English) Chris@16: * - string values for special values (eg: not-a-date-time) (default: English) Chris@16: * - selection of long, short strings, or numerical month representation (default: short string) Chris@16: * - month day year order (default yyyy-mmm-dd) Chris@16: */ Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const date& d) Chris@16: { Chris@16: typedef boost::date_time::date_names_put facet_def; Chris@16: typedef boost::date_time::ostream_date_formatter greg_ostream_formatter; Chris@16: greg_ostream_formatter::date_put(d, os); Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar... Chris@16: /*! Uses the date facet to determine output string as well as selection of long or short strings. Chris@16: * Default if no facet is installed is to output a 2 wide numeric value for the month Chris@16: * eg: 01 == Jan, 02 == Feb, ... 12 == Dec. Chris@16: */ Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const greg_month& m) Chris@16: { Chris@16: typedef boost::date_time::date_names_put facet_def; Chris@16: typedef boost::date_time::ostream_month_formatter greg_month_formatter; Chris@16: std::locale locale = os.getloc(); Chris@16: if (std::has_facet(locale)) { Chris@16: const facet_def& f = std::use_facet(locale); Chris@16: greg_month_formatter::format_month(m, os, f); Chris@16: Chris@16: } Chris@16: else { //default to numeric Chris@16: charT fill_char = '0'; Chris@16: os << std::setw(2) << std::setfill(fill_char) << m.as_number(); Chris@16: } Chris@16: Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ... Chris@16: /*! Uses the date facet to determine output string as well as selection of long or short string. Chris@16: * Default if no facet is installed is to output a 3 char english string for the Chris@16: * day of the week. Chris@16: */ Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const greg_weekday& wd) Chris@16: { Chris@16: typedef boost::date_time::date_names_put facet_def; Chris@16: typedef boost::date_time::ostream_weekday_formatter greg_weekday_formatter; Chris@16: std::locale locale = os.getloc(); Chris@16: if (std::has_facet(locale)) { Chris@16: const facet_def& f = std::use_facet(locale); Chris@101: greg_weekday_formatter::format_weekday(wd, os, f, true); Chris@16: } Chris@16: else { //default to short English string eg: Sun, Mon, Tue, Wed... Chris@16: os << wd.as_short_string(); Chris@16: } Chris@16: Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31] Chris@16: /*! Uses the date facet to determine output string as well as selection of long Chris@16: * or short string fr dates. Chris@16: * Default if no facet is installed is to output a 3 char english string for the Chris@16: * day of the week. Chris@16: */ Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const date_period& dp) Chris@16: { Chris@16: os << '['; //TODO: facet or manipulator for periods? Chris@16: os << dp.begin(); Chris@16: os << '/'; //TODO: facet or manipulator for periods? Chris@16: os << dp.last(); Chris@16: os << ']'; Chris@16: return os; Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const date_duration& dd) Chris@16: { Chris@16: //os << dd.days(); Chris@16: os << dd.get_rep(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::partial_date. Output: "Jan 1" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const partial_date& pd) Chris@16: { Chris@16: os << std::setw(2) << std::setfill('0') << pd.day() << ' ' Chris@16: << pd.month().as_short_string() ; Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const nth_kday_of_month& nkd) Chris@16: { Chris@16: os << nkd.nth_week_as_str() << ' ' Chris@16: << nkd.day_of_week() << " of " Chris@16: << nkd.month().as_short_string() ; Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const first_kday_of_month& fkd) Chris@16: { Chris@16: os << "first " << fkd.day_of_week() << " of " Chris@16: << fkd.month().as_short_string() ; Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const last_kday_of_month& lkd) Chris@16: { Chris@16: os << "last " << lkd.day_of_week() << " of " Chris@16: << lkd.month().as_short_string() ; Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::first_kday_after. Output: "first Mon after" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const first_kday_after& fka) Chris@16: { Chris@16: os << fka.day_of_week() << " after"; Chris@16: return os; Chris@16: } Chris@16: Chris@16: //! operator<< for gregorian::first_kday_before. Output: "first Mon before" Chris@16: template Chris@16: inline Chris@16: std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const first_kday_before& fkb) Chris@16: { Chris@16: os << fkb.day_of_week() << " before"; Chris@16: return os; Chris@16: } Chris@16: #endif // USE_DATE_TIME_PRE_1_33_FACET_IO Chris@16: /**************** Input Streaming ******************/ Chris@16: Chris@16: #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) Chris@16: //! operator>> for gregorian::date Chris@16: template Chris@16: inline Chris@16: std::basic_istream& operator>>(std::basic_istream& is, date& d) Chris@16: { Chris@16: std::istream_iterator, charT> beg(is), eos; Chris@16: d = from_stream(beg, eos); Chris@16: return is; Chris@16: } Chris@16: #endif // BOOST_NO_STD_ITERATOR_TRAITS Chris@16: Chris@16: //! operator>> for gregorian::date_duration Chris@16: template Chris@16: inline Chris@16: std::basic_istream& operator>>(std::basic_istream& is, Chris@16: date_duration& dd) Chris@16: { Chris@16: long v; Chris@16: is >> v; Chris@16: dd = date_duration(v); Chris@16: return is; Chris@16: } Chris@16: Chris@16: //! operator>> for gregorian::date_period Chris@16: template Chris@16: inline Chris@16: std::basic_istream& operator>>(std::basic_istream& is, Chris@16: date_period& dp) Chris@16: { Chris@16: std::basic_string s; Chris@16: is >> s; Chris@16: dp = date_time::from_simple_string_type(s); Chris@16: return is; Chris@16: } Chris@16: Chris@16: //! generates a locale with the set of gregorian name-strings of type char* Chris@16: BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type); Chris@16: Chris@16: //! Returns a pointer to a facet with a default set of names (English) Chris@16: /* Necessary in the event an exception is thrown from op>> for Chris@16: * weekday or month. See comments in those functions for more info */ Chris@16: BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put* create_facet_def(char type); Chris@16: Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: //! generates a locale with the set of gregorian name-strings of type wchar_t* Chris@16: BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type); Chris@16: //! Returns a pointer to a facet with a default set of names (English) Chris@16: /* Necessary in the event an exception is thrown from op>> for Chris@16: * weekday or month. See comments in those functions for more info */ Chris@16: BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put* create_facet_def(wchar_t type); Chris@16: #endif // BOOST_NO_STD_WSTRING Chris@16: Chris@16: //! operator>> for gregorian::greg_month - throws exception if invalid month given Chris@16: template Chris@16: inline Chris@16: std::basic_istream& operator>>(std::basic_istream& is,greg_month& m) Chris@16: { Chris@16: typedef boost::date_time::all_date_names_put facet_def; Chris@16: Chris@16: std::basic_string s; Chris@16: is >> s; Chris@16: Chris@16: if(!std::has_facet(is.getloc())) { Chris@16: std::locale loc = is.getloc(); Chris@16: charT a = '\0'; Chris@16: is.imbue(generate_locale(loc, a)); Chris@16: } Chris@16: Chris@16: short num = 0; Chris@16: Chris@16: try{ Chris@16: const facet_def& f = std::use_facet(is.getloc()); Chris@16: num = date_time::find_match(f.get_short_month_names(), Chris@16: f.get_long_month_names(), Chris@16: (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size, Chris@16: // which is needed by find_match Chris@16: } Chris@16: /* bad_cast will be thrown if the desired facet is not accessible Chris@16: * so we can generate the facet. This has the drawback of using english Chris@16: * names as a default. */ Chris@16: catch(std::bad_cast&){ Chris@16: charT a = '\0'; Chris@16: std::auto_ptr< const facet_def > f(create_facet_def(a)); Chris@16: num = date_time::find_match(f->get_short_month_names(), Chris@16: f->get_long_month_names(), Chris@16: (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size, Chris@16: // which is needed by find_match Chris@16: } Chris@16: Chris@16: ++num; // months numbered 1-12 Chris@16: m = greg_month(num); Chris@16: Chris@16: return is; Chris@16: } Chris@16: Chris@16: //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given Chris@16: template Chris@16: inline Chris@16: std::basic_istream& operator>>(std::basic_istream& is,greg_weekday& wd) Chris@16: { Chris@16: typedef boost::date_time::all_date_names_put facet_def; Chris@16: Chris@16: std::basic_string s; Chris@16: is >> s; Chris@16: Chris@16: if(!std::has_facet(is.getloc())) { Chris@16: std::locale loc = is.getloc(); Chris@16: charT a = '\0'; Chris@16: is.imbue(generate_locale(loc, a)); Chris@16: } Chris@16: Chris@16: short num = 0; Chris@16: try{ Chris@16: const facet_def& f = std::use_facet(is.getloc()); Chris@16: num = date_time::find_match(f.get_short_weekday_names(), Chris@16: f.get_long_weekday_names(), Chris@16: (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed Chris@16: // to form the array size which is needed by find_match Chris@16: } Chris@16: /* bad_cast will be thrown if the desired facet is not accessible Chris@16: * so we can generate the facet. This has the drawback of using english Chris@16: * names as a default. */ Chris@16: catch(std::bad_cast&){ Chris@16: charT a = '\0'; Chris@16: std::auto_ptr< const facet_def > f(create_facet_def(a)); Chris@16: num = date_time::find_match(f->get_short_weekday_names(), Chris@16: f->get_long_weekday_names(), Chris@16: (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed Chris@16: // to form the array size which is needed by find_match Chris@16: } Chris@16: Chris@16: wd = greg_weekday(num); // weekdays numbered 0-6 Chris@16: return is; Chris@16: } Chris@16: Chris@16: } } //namespace gregorian Chris@16: Chris@16: #endif Chris@16: Chris@16: Chris@16: #endif Chris@16: