Chris@16: #ifndef DATE_TIME_DATE_NAMES_PUT_HPP___ Chris@16: #define DATE_TIME_DATE_NAMES_PUT_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002-2005 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: Chris@16: #include "boost/date_time/locale_config.hpp" // set BOOST_DATE_TIME_NO_LOCALE Chris@16: Chris@16: #ifndef BOOST_DATE_TIME_NO_LOCALE Chris@16: Chris@16: #include "boost/date_time/special_defs.hpp" Chris@16: #include "boost/date_time/date_defs.hpp" Chris@16: #include "boost/date_time/parse_format_base.hpp" Chris@16: #include "boost/lexical_cast.hpp" Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! Output facet base class for gregorian dates. Chris@16: /*! This class is a base class for date facets used to localize the Chris@16: * names of months and the names of days in the week. Chris@16: * Chris@16: * Requirements of Config Chris@16: * - define an enumeration month_enum that enumerates the months. Chris@16: * The enumeration should be '1' based eg: Jan==1 Chris@16: * - define as_short_string and as_long_string Chris@16: * Chris@16: * (see langer & kreft p334). Chris@16: * Chris@16: */ Chris@16: template > Chris@16: class date_names_put : public std::locale::facet Chris@16: { Chris@16: public: Chris@16: date_names_put() {} Chris@16: typedef OutputIterator iter_type; Chris@16: typedef typename Config::month_type month_type; Chris@16: typedef typename Config::month_enum month_enum; Chris@16: typedef typename Config::weekday_enum weekday_enum; Chris@16: typedef typename Config::special_value_enum special_value_enum; Chris@16: //typedef typename Config::format_type format_type; Chris@16: typedef std::basic_string string_type; Chris@16: typedef charT char_type; Chris@16: static const char_type default_special_value_names[3][17]; Chris@16: static const char_type separator[2]; Chris@16: Chris@16: static std::locale::id id; Chris@16: Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: Chris@16: void put_special_value(iter_type& oitr, special_value_enum sv) const Chris@16: { Chris@16: do_put_special_value(oitr, sv); Chris@16: } Chris@16: void put_month_short(iter_type& oitr, month_enum moy) const Chris@16: { Chris@16: do_put_month_short(oitr, moy); Chris@16: } Chris@16: void put_month_long(iter_type& oitr, month_enum moy) const Chris@16: { Chris@16: do_put_month_long(oitr, moy); Chris@16: } Chris@16: void put_weekday_short(iter_type& oitr, weekday_enum wd) const Chris@16: { Chris@16: do_put_weekday_short(oitr, wd); Chris@16: } Chris@16: void put_weekday_long(iter_type& oitr, weekday_enum wd) const Chris@16: { Chris@16: do_put_weekday_long(oitr, wd); Chris@16: } Chris@16: bool has_date_sep_chars() const Chris@16: { Chris@16: return do_has_date_sep_chars(); Chris@16: } Chris@16: void year_sep_char(iter_type& oitr) const Chris@16: { Chris@16: do_year_sep_char(oitr); Chris@16: } Chris@16: //! char between year-month Chris@16: void month_sep_char(iter_type& oitr) const Chris@16: { Chris@16: do_month_sep_char(oitr); Chris@16: } Chris@16: //! Char to separate month-day Chris@16: void day_sep_char(iter_type& oitr) const Chris@16: { Chris@16: do_day_sep_char(oitr); Chris@16: } Chris@16: //! Determines the order to put the date elements Chris@16: ymd_order_spec date_order() const Chris@16: { Chris@16: return do_date_order(); Chris@16: } Chris@16: //! Determines if month is displayed as integer, short or long string Chris@16: month_format_spec month_format() const Chris@16: { Chris@16: return do_month_format(); Chris@16: } Chris@16: Chris@16: protected: Chris@16: //! Default facet implementation uses month_type defaults Chris@16: virtual void do_put_month_short(iter_type& oitr, month_enum moy) const Chris@16: { Chris@16: month_type gm(moy); Chris@16: charT c = '\0'; Chris@16: put_string(oitr, gm.as_short_string(c)); Chris@16: } Chris@16: //! Default facet implementation uses month_type defaults Chris@16: virtual void do_put_month_long(iter_type& oitr, Chris@16: month_enum moy) const Chris@16: { Chris@16: month_type gm(moy); Chris@16: charT c = '\0'; Chris@16: put_string(oitr, gm.as_long_string(c)); Chris@16: } Chris@16: //! Default facet implementation for special value types Chris@16: virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const Chris@16: { Chris@16: if(sv <= 2) { // only output not_a_date_time, neg_infin, or pos_infin Chris@16: string_type s(default_special_value_names[sv]); Chris@16: put_string(oitr, s); Chris@16: } Chris@16: } Chris@16: virtual void do_put_weekday_short(iter_type&, weekday_enum) const Chris@16: { Chris@16: } Chris@16: virtual void do_put_weekday_long(iter_type&, weekday_enum) const Chris@16: { Chris@16: } Chris@16: virtual bool do_has_date_sep_chars() const Chris@16: { Chris@16: return true; Chris@16: } Chris@16: virtual void do_year_sep_char(iter_type& oitr) const Chris@16: { Chris@16: string_type s(separator); Chris@16: put_string(oitr, s); Chris@16: } Chris@16: //! char between year-month Chris@16: virtual void do_month_sep_char(iter_type& oitr) const Chris@16: { Chris@16: string_type s(separator); Chris@16: put_string(oitr, s); Chris@16: } Chris@16: //! Char to separate month-day Chris@16: virtual void do_day_sep_char(iter_type& oitr) const Chris@16: { Chris@16: string_type s(separator); //put in '-' Chris@16: put_string(oitr, s); Chris@16: } Chris@16: //! Default for date order Chris@16: virtual ymd_order_spec do_date_order() const Chris@16: { Chris@16: return ymd_order_iso; Chris@16: } Chris@16: //! Default month format Chris@16: virtual month_format_spec do_month_format() const Chris@16: { Chris@16: return month_as_short_string; Chris@16: } Chris@16: void put_string(iter_type& oi, const charT* const s) const Chris@16: { Chris@16: string_type s1(boost::lexical_cast(s)); Chris@16: typename string_type::iterator si,end; Chris@16: for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) { Chris@16: *oi = *si; Chris@16: } Chris@16: } Chris@16: void put_string(iter_type& oi, const string_type& s1) const Chris@16: { Chris@16: typename string_type::const_iterator si,end; Chris@16: for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) { Chris@16: *oi = *si; Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: const typename date_names_put::char_type Chris@16: date_names_put::default_special_value_names[3][17] = { Chris@16: {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'}, Chris@16: {'-','i','n','f','i','n','i','t','y'}, Chris@16: {'+','i','n','f','i','n','i','t','y'} }; Chris@16: Chris@16: template Chris@16: const typename date_names_put::char_type Chris@16: date_names_put::separator[2] = Chris@16: {'-', '\0'} ; Chris@16: Chris@16: Chris@16: //! Generate storage location for a std::locale::id Chris@16: template Chris@16: std::locale::id date_names_put::id; Chris@16: Chris@16: //! A date name output facet that takes an array of char* to define strings Chris@16: template > Chris@16: class all_date_names_put : public date_names_put Chris@16: { Chris@16: public: Chris@16: all_date_names_put(const charT* const month_short_names[], Chris@16: const charT* const month_long_names[], Chris@16: const charT* const special_value_names[], Chris@16: const charT* const weekday_short_names[], Chris@16: const charT* const weekday_long_names[], Chris@16: charT separator_char = '-', Chris@16: ymd_order_spec order_spec = ymd_order_iso, Chris@16: month_format_spec month_format = month_as_short_string) : Chris@16: month_short_names_(month_short_names), Chris@16: month_long_names_(month_long_names), Chris@16: special_value_names_(special_value_names), Chris@16: weekday_short_names_(weekday_short_names), Chris@16: weekday_long_names_(weekday_long_names), Chris@16: order_spec_(order_spec), Chris@16: month_format_spec_(month_format) Chris@16: { Chris@16: separator_char_[0] = separator_char; Chris@16: separator_char_[1] = '\0'; Chris@16: Chris@16: } Chris@16: typedef OutputIterator iter_type; Chris@16: typedef typename Config::month_enum month_enum; Chris@16: typedef typename Config::weekday_enum weekday_enum; Chris@16: typedef typename Config::special_value_enum special_value_enum; Chris@16: Chris@16: const charT* const* get_short_month_names() const Chris@16: { Chris@16: return month_short_names_; Chris@16: } Chris@16: const charT* const* get_long_month_names() const Chris@16: { Chris@16: return month_long_names_; Chris@16: } Chris@16: const charT* const* get_special_value_names() const Chris@16: { Chris@16: return special_value_names_; Chris@16: } Chris@16: const charT* const* get_short_weekday_names()const Chris@16: { Chris@16: return weekday_short_names_; Chris@16: } Chris@16: const charT* const* get_long_weekday_names()const Chris@16: { Chris@16: return weekday_long_names_; Chris@16: } Chris@16: Chris@16: protected: Chris@16: //! Generic facet that takes array of chars Chris@16: virtual void do_put_month_short(iter_type& oitr, month_enum moy) const Chris@16: { Chris@16: this->put_string(oitr, month_short_names_[moy-1]); Chris@16: } Chris@16: //! Long month names Chris@16: virtual void do_put_month_long(iter_type& oitr, month_enum moy) const Chris@16: { Chris@16: this->put_string(oitr, month_long_names_[moy-1]); Chris@16: } Chris@16: //! Special values names Chris@16: virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const Chris@16: { Chris@16: this->put_string(oitr, special_value_names_[sv]); Chris@16: } Chris@16: virtual void do_put_weekday_short(iter_type& oitr, weekday_enum wd) const Chris@16: { Chris@16: this->put_string(oitr, weekday_short_names_[wd]); Chris@16: } Chris@16: virtual void do_put_weekday_long(iter_type& oitr, weekday_enum wd) const Chris@16: { Chris@16: this->put_string(oitr, weekday_long_names_[wd]); Chris@16: } Chris@16: //! char between year-month Chris@16: virtual void do_month_sep_char(iter_type& oitr) const Chris@16: { Chris@16: this->put_string(oitr, separator_char_); Chris@16: } Chris@16: //! Char to separate month-day Chris@16: virtual void do_day_sep_char(iter_type& oitr) const Chris@16: { Chris@16: this->put_string(oitr, separator_char_); Chris@16: } Chris@16: //! Set the date ordering Chris@16: virtual ymd_order_spec do_date_order() const Chris@16: { Chris@16: return order_spec_; Chris@16: } Chris@16: //! Set the date ordering Chris@16: virtual month_format_spec do_month_format() const Chris@16: { Chris@16: return month_format_spec_; Chris@16: } Chris@16: Chris@16: private: Chris@16: const charT* const* month_short_names_; Chris@16: const charT* const* month_long_names_; Chris@16: const charT* const* special_value_names_; Chris@16: const charT* const* weekday_short_names_; Chris@16: const charT* const* weekday_long_names_; Chris@16: charT separator_char_[2]; Chris@16: ymd_order_spec order_spec_; Chris@16: month_format_spec month_format_spec_; Chris@16: }; Chris@16: Chris@16: } } //namespace boost::date_time Chris@16: Chris@16: #endif //BOOST_NO_STD_LOCALE Chris@16: Chris@16: #endif