1 #ifndef DATE_TIME_DATE_FORMATTING_HPP___
2 #define DATE_TIME_DATE_FORMATTING_HPP___
4 /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
12 #include "boost/date_time/iso_format.hpp"
13 #include "boost/date_time/compiler_config.hpp"
18 /* NOTE: "formatter" code for older compilers, ones that define
19 * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
20 * date_formatting_limited.hpp
26 //! Formats a month as as string into an ostream
27 template<class month_type, class format_type, class charT=char>
30 typedef std::basic_ostream<charT> ostream_type;
32 //! Formats a month as as string into an ostream
33 /*! This function demands that month_type provide
34 * functions for converting to short and long strings
35 * if that capability is used.
37 static ostream_type& format_month(const month_type& month,
40 switch (format_type::month_format())
42 case month_as_short_string:
44 os << month.as_short_string();
47 case month_as_long_string:
49 os << month.as_long_string();
52 case month_as_integer:
54 os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
66 //! Convert ymd to a standard string formatting policies
67 template<class ymd_type, class format_type, class charT=char>
71 //! Convert ymd to a standard string formatting policies
72 /*! This is standard code for handling date formatting with
73 * year-month-day based date information. This function
74 * uses the format_type to control whether the string will
75 * contain separator characters, and if so what the character
76 * will be. In addtion, it can format the month as either
77 * an integer or a string as controled by the formatting
80 static std::basic_string<charT> ymd_to_string(ymd_type ymd)
82 typedef typename ymd_type::month_type month_type;
83 std::basic_ostringstream<charT> ss;
85 // Temporarily switch to classic locale to prevent possible formatting
86 // of year with comma or other character (for example 2,008).
87 ss.imbue(std::locale::classic());
89 ss.imbue(std::locale());
91 if (format_type::has_date_sep_chars()) {
92 ss << format_type::month_sep_char();
94 //this name is a bit ugly, oh well....
95 month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss);
96 if (format_type::has_date_sep_chars()) {
97 ss << format_type::day_sep_char();
99 ss << std::setw(2) << std::setfill(ss.widen('0'))
106 //! Convert a date to string using format policies
107 template<class date_type, class format_type, class charT=char>
111 typedef std::basic_string<charT> string_type;
112 //! Convert to a date to standard string using format policies
113 static string_type date_to_string(date_type d)
115 typedef typename date_type::ymd_type ymd_type;
116 if (d.is_not_a_date()) {
117 return string_type(format_type::not_a_date());
119 if (d.is_neg_infinity()) {
120 return string_type(format_type::neg_infinity());
122 if (d.is_pos_infinity()) {
123 return string_type(format_type::pos_infinity());
125 ymd_type ymd = d.year_month_day();
126 return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
131 } } //namespace date_time