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