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: 2005/12/06 03:49:15 $ |
---|
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 | 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 | |
---|
58 | } |
---|
59 | return os; |
---|
60 | } // format_month |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | //! Convert ymd to a standard string formatting policies |
---|
65 | template<class ymd_type, class format_type, class charT=char> |
---|
66 | class ymd_formatter |
---|
67 | { |
---|
68 | public: |
---|
69 | //! Convert ymd to a standard string formatting policies |
---|
70 | /*! This is standard code for handling date formatting with |
---|
71 | * year-month-day based date information. This function |
---|
72 | * uses the format_type to control whether the string will |
---|
73 | * contain separator characters, and if so what the character |
---|
74 | * will be. In addtion, it can format the month as either |
---|
75 | * an integer or a string as controled by the formatting |
---|
76 | * policy |
---|
77 | */ |
---|
78 | static std::basic_string<charT> ymd_to_string(ymd_type ymd) |
---|
79 | { |
---|
80 | typedef typename ymd_type::month_type month_type; |
---|
81 | std::basic_ostringstream<charT> ss; |
---|
82 | ss << ymd.year; |
---|
83 | if (format_type::has_date_sep_chars()) { |
---|
84 | ss << format_type::month_sep_char(); |
---|
85 | } |
---|
86 | //this name is a bit ugly, oh well.... |
---|
87 | month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss); |
---|
88 | if (format_type::has_date_sep_chars()) { |
---|
89 | ss << format_type::day_sep_char(); |
---|
90 | } |
---|
91 | ss << std::setw(2) << std::setfill(ss.widen('0')) |
---|
92 | << ymd.day; |
---|
93 | return ss.str(); |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | |
---|
98 | //! Convert a date to string using format policies |
---|
99 | template<class date_type, class format_type, class charT=char> |
---|
100 | class date_formatter |
---|
101 | { |
---|
102 | public: |
---|
103 | typedef std::basic_string<charT> string_type; |
---|
104 | //! Convert to a date to standard string using format policies |
---|
105 | static string_type date_to_string(date_type d) |
---|
106 | { |
---|
107 | typedef typename date_type::ymd_type ymd_type; |
---|
108 | if (d.is_not_a_date()) { |
---|
109 | return string_type(format_type::not_a_date()); |
---|
110 | } |
---|
111 | if (d.is_neg_infinity()) { |
---|
112 | return string_type(format_type::neg_infinity()); |
---|
113 | } |
---|
114 | if (d.is_pos_infinity()) { |
---|
115 | return string_type(format_type::pos_infinity()); |
---|
116 | } |
---|
117 | ymd_type ymd = d.year_month_day(); |
---|
118 | return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd); |
---|
119 | } |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | } } //namespace date_time |
---|
124 | |
---|
125 | |
---|
126 | #endif |
---|
127 | |
---|