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 or http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2004/08/29 19:31:11 $ |
---|
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 | |
---|
23 | namespace boost { |
---|
24 | namespace 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 | public: |
---|
31 | //! Formats a month as as string into an ostream |
---|
32 | /*! This function demands that month_type provide |
---|
33 | * functions for converting to short and long strings |
---|
34 | * if that capability is used. |
---|
35 | */ |
---|
36 | static std::basic_ostream<charT>& format_month(const month_type& month, |
---|
37 | std::basic_ostream<charT>& os) |
---|
38 | { |
---|
39 | switch (format_type::month_format()) |
---|
40 | { |
---|
41 | case month_as_short_string: |
---|
42 | { |
---|
43 | os << month.as_short_string(); |
---|
44 | break; |
---|
45 | } |
---|
46 | case month_as_long_string: |
---|
47 | { |
---|
48 | os << month.as_long_string(); |
---|
49 | break; |
---|
50 | } |
---|
51 | case month_as_integer: |
---|
52 | { |
---|
53 | os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number(); |
---|
54 | break; |
---|
55 | } |
---|
56 | |
---|
57 | } |
---|
58 | return os; |
---|
59 | } // format_month |
---|
60 | }; |
---|
61 | |
---|
62 | |
---|
63 | //! Convert ymd to a standard string formatting policies |
---|
64 | template<class ymd_type, class format_type, class charT=char> |
---|
65 | class ymd_formatter |
---|
66 | { |
---|
67 | public: |
---|
68 | //! Convert ymd to a standard string formatting policies |
---|
69 | /*! This is standard code for handling date formatting with |
---|
70 | * year-month-day based date information. This function |
---|
71 | * uses the format_type to control whether the string will |
---|
72 | * contain separator characters, and if so what the character |
---|
73 | * will be. In addtion, it can format the month as either |
---|
74 | * an integer or a string as controled by the formatting |
---|
75 | * policy |
---|
76 | */ |
---|
77 | static std::basic_string<charT> ymd_to_string(ymd_type ymd) |
---|
78 | { |
---|
79 | typedef typename ymd_type::month_type month_type; |
---|
80 | std::basic_ostringstream<charT> ss; |
---|
81 | ss << ymd.year; |
---|
82 | if (format_type::has_date_sep_chars()) { |
---|
83 | ss << format_type::month_sep_char(); |
---|
84 | } |
---|
85 | //this name is a bit ugly, oh well.... |
---|
86 | month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss); |
---|
87 | if (format_type::has_date_sep_chars()) { |
---|
88 | ss << format_type::day_sep_char(); |
---|
89 | } |
---|
90 | ss << std::setw(2) << std::setfill(ss.widen('0')) |
---|
91 | << ymd.day; |
---|
92 | return ss.str(); |
---|
93 | } |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | //! Convert a date to string using format policies |
---|
98 | template<class date_type, class format_type, class charT=char> |
---|
99 | class date_formatter |
---|
100 | { |
---|
101 | public: |
---|
102 | typedef std::basic_string<charT> string_type; |
---|
103 | //! Convert to a date to standard string using format policies |
---|
104 | static string_type date_to_string(date_type d) |
---|
105 | { |
---|
106 | typedef typename date_type::ymd_type ymd_type; |
---|
107 | if (d.is_not_a_date()) { |
---|
108 | return string_type(format_type::not_a_date()); |
---|
109 | } |
---|
110 | if (d.is_neg_infinity()) { |
---|
111 | return string_type(format_type::neg_infinity()); |
---|
112 | } |
---|
113 | if (d.is_pos_infinity()) { |
---|
114 | return string_type(format_type::pos_infinity()); |
---|
115 | } |
---|
116 | ymd_type ymd = d.year_month_day(); |
---|
117 | return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd); |
---|
118 | } |
---|
119 | }; |
---|
120 | |
---|
121 | |
---|
122 | } } //namespace date_time |
---|
123 | |
---|
124 | |
---|
125 | #endif |
---|
126 | |
---|