Chris@16: #ifndef DATE_TIME_DATE_FORMATTING_LIMITED_HPP___ Chris@16: #define DATE_TIME_DATE_FORMATTING_LIMITED_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002-2004 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/iso_format.hpp" Chris@16: #include "boost/date_time/compiler_config.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 month_formatter Chris@16: { Chris@16: public: Chris@16: //! Formats a month as as string into an ostream Chris@16: /*! This function demands that month_type provide Chris@16: * functions for converting to short and long strings Chris@16: * if that capability is used. Chris@16: */ Chris@16: static std::ostream& format_month(const month_type& month, Chris@16: std::ostream& os) Chris@16: { Chris@16: switch (format_type::month_format()) Chris@16: { Chris@16: case month_as_short_string: Chris@16: { Chris@16: os << month.as_short_string(); Chris@16: break; Chris@16: } Chris@16: case month_as_long_string: Chris@16: { Chris@16: os << month.as_long_string(); Chris@16: break; Chris@16: } Chris@16: case month_as_integer: Chris@16: { Chris@16: os << std::setw(2) << std::setfill('0') << month.as_number(); Chris@16: break; Chris@16: } Chris@16: Chris@16: } Chris@16: return os; Chris@16: } // format_month Chris@16: }; Chris@16: Chris@16: Chris@16: //! Convert ymd to a standard string formatting policies Chris@16: template Chris@16: class ymd_formatter Chris@16: { Chris@16: public: 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 std::string ymd_to_string(ymd_type ymd) Chris@16: { Chris@16: typedef typename ymd_type::month_type month_type; Chris@16: std::ostringstream ss; Chris@16: ss << ymd.year; Chris@16: if (format_type::has_date_sep_chars()) { Chris@16: ss << format_type::month_sep_char(); Chris@16: } Chris@16: //this name is a bit ugly, oh well.... Chris@16: month_formatter::format_month(ymd.month, ss); Chris@16: if (format_type::has_date_sep_chars()) { Chris@16: ss << format_type::day_sep_char(); Chris@16: } Chris@16: ss << std::setw(2) << std::setfill('0') Chris@16: << ymd.day; Chris@16: return ss.str(); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: //! Convert a date to string using format policies Chris@16: template Chris@16: class date_formatter Chris@16: { Chris@16: public: Chris@16: //! Convert to a date to standard string using format policies Chris@16: static std::string date_to_string(date_type d) Chris@16: { Chris@16: typedef typename date_type::ymd_type ymd_type; Chris@16: if (d.is_not_a_date()) { Chris@16: return format_type::not_a_date(); Chris@16: } Chris@16: if (d.is_neg_infinity()) { Chris@16: return format_type::neg_infinity(); Chris@16: } Chris@16: if (d.is_pos_infinity()) { Chris@16: return format_type::pos_infinity(); Chris@16: } Chris@16: ymd_type ymd = d.year_month_day(); Chris@16: return ymd_formatter::ymd_to_string(ymd); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: Chris@16: #endif Chris@16: