Chris@16: #ifndef DATE_TIME_DATE_FORMATTING_LOCALES_HPP___ Chris@16: #define DATE_TIME_DATE_FORMATTING_LOCALES_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: 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/iso_format.hpp" Chris@16: #include "boost/date_time/date_names_put.hpp" Chris@16: #include "boost/date_time/parse_format_base.hpp" Chris@16: //#include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! Formats a month as as string into an ostream Chris@16: template Chris@16: class ostream_month_formatter Chris@16: { Chris@16: public: Chris@16: typedef typename facet_type::month_type month_type; Chris@16: typedef std::basic_ostream ostream_type; Chris@16: Chris@16: //! Formats a month as as string into an output iterator Chris@16: static void format_month(const month_type& month, Chris@16: ostream_type& os, Chris@16: const facet_type& f) Chris@16: { Chris@16: Chris@16: switch (f.month_format()) Chris@16: { Chris@16: case month_as_short_string: Chris@16: { Chris@16: std::ostreambuf_iterator oitr(os); Chris@16: f.put_month_short(oitr, month.as_enum()); Chris@16: break; Chris@16: } Chris@16: case month_as_long_string: Chris@16: { Chris@16: std::ostreambuf_iterator oitr(os); Chris@16: f.put_month_long(oitr, month.as_enum()); Chris@16: break; Chris@16: } Chris@16: case month_as_integer: Chris@16: { Chris@16: charT fill_char = '0'; Chris@16: os << std::setw(2) << std::setfill(fill_char) << month.as_number(); Chris@16: break; Chris@16: } Chris@16: Chris@16: } Chris@16: } // format_month Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: //! Formats a weekday Chris@16: template Chris@16: class ostream_weekday_formatter Chris@16: { Chris@16: public: Chris@16: typedef typename facet_type::month_type month_type; Chris@16: typedef std::basic_ostream ostream_type; Chris@16: Chris@16: //! Formats a month as as string into an output iterator Chris@16: static void format_weekday(const weekday_type& wd, Chris@16: ostream_type& os, Chris@16: const facet_type& f, Chris@16: bool as_long_string) Chris@16: { Chris@16: Chris@16: std::ostreambuf_iterator oitr(os); Chris@16: if (as_long_string) { Chris@16: f.put_weekday_long(oitr, wd.as_enum()); Chris@16: } Chris@16: else { Chris@16: f.put_weekday_short(oitr, wd.as_enum()); Chris@16: } Chris@16: Chris@16: } // format_weekday Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: //! Convert ymd to a standard string formatting policies Chris@16: template Chris@16: class ostream_ymd_formatter Chris@16: { Chris@16: public: Chris@16: typedef typename ymd_type::month_type month_type; Chris@16: typedef ostream_month_formatter month_formatter_type; Chris@16: typedef std::basic_ostream ostream_type; Chris@16: typedef std::basic_string foo_type; Chris@16: Chris@16: //! Convert ymd to a standard string formatting policies Chris@16: /*! This is standard code for handling date formatting with Chris@16: * year-month-day based date information. This function Chris@16: * uses the format_type to control whether the string will Chris@16: * contain separator characters, and if so what the character Chris@16: * will be. In addtion, it can format the month as either Chris@16: * an integer or a string as controled by the formatting Chris@16: * policy Chris@16: */ Chris@16: // static string_type ymd_to_string(ymd_type ymd) Chris@16: // { Chris@16: // std::ostringstream ss; Chris@16: // facet_type dnp; Chris@16: // ymd_put(ymd, ss, dnp); Chris@16: // return ss.str(); Chris@16: // } Chris@16: Chris@16: Chris@16: // Put ymd to ostream -- part of ostream refactor Chris@16: static void ymd_put(ymd_type ymd, Chris@16: ostream_type& os, Chris@16: const facet_type& f) Chris@16: { Chris@16: std::ostreambuf_iterator oitr(os); Chris@16: charT fill_char = '0'; Chris@16: switch (f.date_order()) { Chris@16: case ymd_order_iso: { Chris@16: os << ymd.year; Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.month_sep_char(oitr); Chris@16: } Chris@16: month_formatter_type::format_month(ymd.month, os, f); Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.day_sep_char(oitr); Chris@16: } Chris@16: os << std::setw(2) << std::setfill(fill_char) Chris@16: << ymd.day; Chris@16: break; Chris@16: } Chris@16: case ymd_order_us: { Chris@16: month_formatter_type::format_month(ymd.month, os, f); Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.day_sep_char(oitr); Chris@16: } Chris@16: os << std::setw(2) << std::setfill(fill_char) Chris@16: << ymd.day; Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.month_sep_char(oitr); Chris@16: } Chris@16: os << ymd.year; Chris@16: break; Chris@16: } Chris@16: case ymd_order_dmy: { Chris@16: os << std::setw(2) << std::setfill(fill_char) Chris@16: << ymd.day; Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.day_sep_char(oitr); Chris@16: } Chris@16: month_formatter_type::format_month(ymd.month, os, f); Chris@16: if (f.has_date_sep_chars()) { Chris@16: f.month_sep_char(oitr); Chris@16: } Chris@16: os << ymd.year; Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: //! Convert a date to string using format policies Chris@16: template Chris@16: class ostream_date_formatter Chris@16: { Chris@16: public: Chris@16: typedef std::basic_ostream ostream_type; Chris@16: typedef typename date_type::ymd_type ymd_type; Chris@16: Chris@16: //! Put date into an ostream Chris@16: static void date_put(const date_type& d, Chris@16: ostream_type& os, Chris@16: const facet_type& f) Chris@16: { Chris@16: special_values sv = d.as_special(); Chris@16: if (sv == not_special) { Chris@16: ymd_type ymd = d.year_month_day(); Chris@16: ostream_ymd_formatter::ymd_put(ymd, os, f); Chris@16: } Chris@16: else { // output a special value Chris@16: std::ostreambuf_iterator coi(os); Chris@16: f.put_special_value(coi, sv); Chris@16: } Chris@16: } Chris@16: Chris@16: Chris@16: //! Put date into an ostream Chris@16: static void date_put(const date_type& d, Chris@16: ostream_type& os) Chris@16: { Chris@16: //retrieve the local from the ostream Chris@16: std::locale locale = os.getloc(); Chris@16: if (std::has_facet(locale)) { Chris@16: const facet_type& f = std::use_facet(locale); Chris@16: date_put(d, os, f); Chris@16: } Chris@16: else { Chris@16: //default to something sensible if no facet installed Chris@16: facet_type default_facet; Chris@16: date_put(d, os, default_facet); Chris@16: } Chris@16: } // date_to_ostream Chris@16: }; //class date_formatter Chris@16: Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: #endif Chris@16: Chris@16: #endif Chris@16: