1#ifndef DATE_TIME_DATE_FORMATTING_HPP___
2#define DATE_TIME_DATE_FORMATTING_HPP___
3
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
9 * $Date$
10 */
11
12#include "boost/date_time/iso_format.hpp"
13#include "boost/date_time/compiler_config.hpp"
14#include <string>
15#include <sstream>
16#include <iomanip>
17
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
21 */
22
23namespace boost {
24namespace date_time {
25
26 //! Formats a month as as string into an ostream
27 template<class month_type, class format_type, class charT=char>
28 class month_formatter
29 {
30 typedef std::basic_ostream<charT> ostream_type;
31 public:
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.
36 */
37 static ostream_type& format_month(const month_type& month,
38 ostream_type &os)
39 {
40 switch (format_type::month_format())
41 {
42 case month_as_short_string:
43 {
44 os << month.as_short_string();
45 break;
46 }
47 case month_as_long_string:
48 {
49 os << month.as_long_string();
50 break;
51 }
52 case month_as_integer:
53 {
54 os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
55 break;
56 }
57 default:
58 break;
59
60 }
61 return os;
62 } // format_month
63 };
64
65
66 //! Convert ymd to a standard string formatting policies
67 template<class ymd_type, class format_type, class charT=char>
68 class ymd_formatter
69 {
70 public:
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
78 * policy
79 */
80 static std::basic_string<charT> ymd_to_string(ymd_type ymd)
81 {
82 typedef typename ymd_type::month_type month_type;
83 std::basic_ostringstream<charT> ss;
84
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());
88 ss << ymd.year;
89 ss.imbue(std::locale());
90
91 if (format_type::has_date_sep_chars()) {
92 ss << format_type::month_sep_char();
93 }
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();
98 }
99 ss << std::setw(2) << std::setfill(ss.widen('0'))
100 << ymd.day;
101 return ss.str();
102 }
103 };
104
105
106 //! Convert a date to string using format policies
107 template<class date_type, class format_type, class charT=char>
108 class date_formatter
109 {
110 public:
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)
114 {
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());
118 }
119 if (d.is_neg_infinity()) {
120 return string_type(format_type::neg_infinity());
121 }
122 if (d.is_pos_infinity()) {
123 return string_type(format_type::pos_infinity());
124 }
125 ymd_type ymd = d.year_month_day();
126 return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
127 }
128 };
129
130
131} } //namespace date_time
132
133
134#endif
135
136

source code of boost/boost/date_time/date_formatting.hpp