Chris@16
|
1 #ifndef DATE_TIME_DATE_FORMATTING_HPP___
|
Chris@16
|
2 #define DATE_TIME_DATE_FORMATTING_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
|
Chris@16
|
5 * Use, modification and distribution is subject to the
|
Chris@16
|
6 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 #include "boost/date_time/iso_format.hpp"
|
Chris@16
|
13 #include "boost/date_time/compiler_config.hpp"
|
Chris@16
|
14 #include <string>
|
Chris@16
|
15 #include <sstream>
|
Chris@16
|
16 #include <iomanip>
|
Chris@16
|
17
|
Chris@16
|
18 /* NOTE: "formatter" code for older compilers, ones that define
|
Chris@16
|
19 * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
|
Chris@16
|
20 * date_formatting_limited.hpp
|
Chris@16
|
21 */
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost {
|
Chris@16
|
24 namespace date_time {
|
Chris@16
|
25
|
Chris@16
|
26 //! Formats a month as as string into an ostream
|
Chris@16
|
27 template<class month_type, class format_type, class charT=char>
|
Chris@16
|
28 class month_formatter
|
Chris@16
|
29 {
|
Chris@16
|
30 typedef std::basic_ostream<charT> ostream_type;
|
Chris@16
|
31 public:
|
Chris@16
|
32 //! Formats a month as as string into an ostream
|
Chris@16
|
33 /*! This function demands that month_type provide
|
Chris@16
|
34 * functions for converting to short and long strings
|
Chris@16
|
35 * if that capability is used.
|
Chris@16
|
36 */
|
Chris@16
|
37 static ostream_type& format_month(const month_type& month,
|
Chris@16
|
38 ostream_type &os)
|
Chris@16
|
39 {
|
Chris@16
|
40 switch (format_type::month_format())
|
Chris@16
|
41 {
|
Chris@16
|
42 case month_as_short_string:
|
Chris@16
|
43 {
|
Chris@16
|
44 os << month.as_short_string();
|
Chris@16
|
45 break;
|
Chris@16
|
46 }
|
Chris@16
|
47 case month_as_long_string:
|
Chris@16
|
48 {
|
Chris@16
|
49 os << month.as_long_string();
|
Chris@16
|
50 break;
|
Chris@16
|
51 }
|
Chris@16
|
52 case month_as_integer:
|
Chris@16
|
53 {
|
Chris@16
|
54 os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
|
Chris@16
|
55 break;
|
Chris@16
|
56 }
|
Chris@16
|
57 default:
|
Chris@16
|
58 break;
|
Chris@16
|
59
|
Chris@16
|
60 }
|
Chris@16
|
61 return os;
|
Chris@16
|
62 } // format_month
|
Chris@16
|
63 };
|
Chris@16
|
64
|
Chris@16
|
65
|
Chris@16
|
66 //! Convert ymd to a standard string formatting policies
|
Chris@16
|
67 template<class ymd_type, class format_type, class charT=char>
|
Chris@16
|
68 class ymd_formatter
|
Chris@16
|
69 {
|
Chris@16
|
70 public:
|
Chris@16
|
71 //! Convert ymd to a standard string formatting policies
|
Chris@16
|
72 /*! This is standard code for handling date formatting with
|
Chris@16
|
73 * year-month-day based date information. This function
|
Chris@16
|
74 * uses the format_type to control whether the string will
|
Chris@16
|
75 * contain separator characters, and if so what the character
|
Chris@16
|
76 * will be. In addtion, it can format the month as either
|
Chris@16
|
77 * an integer or a string as controled by the formatting
|
Chris@16
|
78 * policy
|
Chris@16
|
79 */
|
Chris@16
|
80 static std::basic_string<charT> ymd_to_string(ymd_type ymd)
|
Chris@16
|
81 {
|
Chris@16
|
82 typedef typename ymd_type::month_type month_type;
|
Chris@16
|
83 std::basic_ostringstream<charT> ss;
|
Chris@16
|
84
|
Chris@16
|
85 // Temporarily switch to classic locale to prevent possible formatting
|
Chris@16
|
86 // of year with comma or other character (for example 2,008).
|
Chris@16
|
87 ss.imbue(std::locale::classic());
|
Chris@16
|
88 ss << ymd.year;
|
Chris@16
|
89 ss.imbue(std::locale());
|
Chris@16
|
90
|
Chris@16
|
91 if (format_type::has_date_sep_chars()) {
|
Chris@16
|
92 ss << format_type::month_sep_char();
|
Chris@16
|
93 }
|
Chris@16
|
94 //this name is a bit ugly, oh well....
|
Chris@16
|
95 month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss);
|
Chris@16
|
96 if (format_type::has_date_sep_chars()) {
|
Chris@16
|
97 ss << format_type::day_sep_char();
|
Chris@16
|
98 }
|
Chris@16
|
99 ss << std::setw(2) << std::setfill(ss.widen('0'))
|
Chris@16
|
100 << ymd.day;
|
Chris@16
|
101 return ss.str();
|
Chris@16
|
102 }
|
Chris@16
|
103 };
|
Chris@16
|
104
|
Chris@16
|
105
|
Chris@16
|
106 //! Convert a date to string using format policies
|
Chris@16
|
107 template<class date_type, class format_type, class charT=char>
|
Chris@16
|
108 class date_formatter
|
Chris@16
|
109 {
|
Chris@16
|
110 public:
|
Chris@16
|
111 typedef std::basic_string<charT> string_type;
|
Chris@16
|
112 //! Convert to a date to standard string using format policies
|
Chris@16
|
113 static string_type date_to_string(date_type d)
|
Chris@16
|
114 {
|
Chris@16
|
115 typedef typename date_type::ymd_type ymd_type;
|
Chris@16
|
116 if (d.is_not_a_date()) {
|
Chris@16
|
117 return string_type(format_type::not_a_date());
|
Chris@16
|
118 }
|
Chris@16
|
119 if (d.is_neg_infinity()) {
|
Chris@16
|
120 return string_type(format_type::neg_infinity());
|
Chris@16
|
121 }
|
Chris@16
|
122 if (d.is_pos_infinity()) {
|
Chris@16
|
123 return string_type(format_type::pos_infinity());
|
Chris@16
|
124 }
|
Chris@16
|
125 ymd_type ymd = d.year_month_day();
|
Chris@16
|
126 return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
|
Chris@16
|
127 }
|
Chris@16
|
128 };
|
Chris@16
|
129
|
Chris@16
|
130
|
Chris@16
|
131 } } //namespace date_time
|
Chris@16
|
132
|
Chris@16
|
133
|
Chris@16
|
134 #endif
|
Chris@16
|
135
|