1 | #ifndef POSIXTIME_FORMATTERS_LIMITED_HPP___ |
---|
2 | #define POSIXTIME_FORMATTERS_LIMITED_HPP___ |
---|
3 | |
---|
4 | /* Copyright (c) 2002,2003 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: 2010-01-10 14:17:23 -0500 (Sun, 10 Jan 2010) $ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <boost/date_time/gregorian/gregorian.hpp> |
---|
13 | #include <boost/date_time/compiler_config.hpp> |
---|
14 | #include <boost/date_time/iso_format.hpp> |
---|
15 | #include <boost/date_time/date_format_simple.hpp> |
---|
16 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
---|
17 | #include <boost/date_time/time_formatting_streams.hpp> |
---|
18 | #include <boost/date_time/time_resolution_traits.hpp> // absolute_value |
---|
19 | |
---|
20 | namespace boost { |
---|
21 | |
---|
22 | namespace posix_time { |
---|
23 | |
---|
24 | //! Time duration to string -hh::mm::ss.fffffff. Example: 10:09:03.0123456 |
---|
25 | /*!\ingroup time_format |
---|
26 | */ |
---|
27 | inline std::string to_simple_string(time_duration td) { |
---|
28 | std::ostringstream ss; |
---|
29 | if(td.is_special()) { |
---|
30 | /* simply using 'ss << td.get_rep()' won't work on compilers |
---|
31 | * that don't support locales. This way does. */ |
---|
32 | // switch copied from date_names_put.hpp |
---|
33 | switch(td.get_rep().as_special()) |
---|
34 | { |
---|
35 | case not_a_date_time: |
---|
36 | //ss << "not-a-number"; |
---|
37 | ss << "not-a-date-time"; |
---|
38 | break; |
---|
39 | case pos_infin: |
---|
40 | ss << "+infinity"; |
---|
41 | break; |
---|
42 | case neg_infin: |
---|
43 | ss << "-infinity"; |
---|
44 | break; |
---|
45 | default: |
---|
46 | ss << ""; |
---|
47 | } |
---|
48 | } |
---|
49 | else { |
---|
50 | if(td.is_negative()) { |
---|
51 | ss << '-'; |
---|
52 | } |
---|
53 | ss << std::setw(2) << std::setfill('0') |
---|
54 | << date_time::absolute_value(td.hours()) << ":"; |
---|
55 | ss << std::setw(2) << std::setfill('0') |
---|
56 | << date_time::absolute_value(td.minutes()) << ":"; |
---|
57 | ss << std::setw(2) << std::setfill('0') |
---|
58 | << date_time::absolute_value(td.seconds()); |
---|
59 | //TODO the following is totally non-generic, yelling FIXME |
---|
60 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) |
---|
61 | boost::int64_t frac_sec = |
---|
62 | date_time::absolute_value(td.fractional_seconds()); |
---|
63 | // JDG [7/6/02 VC++ compatibility] |
---|
64 | char buff[32]; |
---|
65 | _i64toa(frac_sec, buff, 10); |
---|
66 | #else |
---|
67 | time_duration::fractional_seconds_type frac_sec = |
---|
68 | date_time::absolute_value(td.fractional_seconds()); |
---|
69 | #endif |
---|
70 | if (frac_sec != 0) { |
---|
71 | ss << "." << std::setw(time_duration::num_fractional_digits()) |
---|
72 | << std::setfill('0') |
---|
73 | |
---|
74 | // JDG [7/6/02 VC++ compatibility] |
---|
75 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) |
---|
76 | << buff; |
---|
77 | #else |
---|
78 | << frac_sec; |
---|
79 | #endif |
---|
80 | } |
---|
81 | }// else |
---|
82 | return ss.str(); |
---|
83 | } |
---|
84 | |
---|
85 | //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456 |
---|
86 | /*!\ingroup time_format |
---|
87 | */ |
---|
88 | inline |
---|
89 | std::string |
---|
90 | to_iso_string(time_duration td) |
---|
91 | { |
---|
92 | std::ostringstream ss; |
---|
93 | if(td.is_special()) { |
---|
94 | /* simply using 'ss << td.get_rep()' won't work on compilers |
---|
95 | * that don't support locales. This way does. */ |
---|
96 | // switch copied from date_names_put.hpp |
---|
97 | switch(td.get_rep().as_special()) { |
---|
98 | case not_a_date_time: |
---|
99 | //ss << "not-a-number"; |
---|
100 | ss << "not-a-date-time"; |
---|
101 | break; |
---|
102 | case pos_infin: |
---|
103 | ss << "+infinity"; |
---|
104 | break; |
---|
105 | case neg_infin: |
---|
106 | ss << "-infinity"; |
---|
107 | break; |
---|
108 | default: |
---|
109 | ss << ""; |
---|
110 | } |
---|
111 | } |
---|
112 | else { |
---|
113 | if(td.is_negative()) { |
---|
114 | ss << '-'; |
---|
115 | } |
---|
116 | ss << std::setw(2) << std::setfill('0') |
---|
117 | << date_time::absolute_value(td.hours()); |
---|
118 | ss << std::setw(2) << std::setfill('0') |
---|
119 | << date_time::absolute_value(td.minutes()); |
---|
120 | ss << std::setw(2) << std::setfill('0') |
---|
121 | << date_time::absolute_value(td.seconds()); |
---|
122 | //TODO the following is totally non-generic, yelling FIXME |
---|
123 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) |
---|
124 | boost::int64_t frac_sec = |
---|
125 | date_time::absolute_value(td.fractional_seconds()); |
---|
126 | // JDG [7/6/02 VC++ compatibility] |
---|
127 | char buff[32]; |
---|
128 | _i64toa(frac_sec, buff, 10); |
---|
129 | #else |
---|
130 | time_duration::fractional_seconds_type frac_sec = |
---|
131 | date_time::absolute_value(td.fractional_seconds()); |
---|
132 | #endif |
---|
133 | if (frac_sec != 0) { |
---|
134 | ss << "." << std::setw(time_duration::num_fractional_digits()) |
---|
135 | << std::setfill('0') |
---|
136 | |
---|
137 | // JDG [7/6/02 VC++ compatibility] |
---|
138 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) |
---|
139 | << buff; |
---|
140 | #else |
---|
141 | << frac_sec; |
---|
142 | #endif |
---|
143 | } |
---|
144 | }// else |
---|
145 | return ss.str(); |
---|
146 | } |
---|
147 | |
---|
148 | //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff |
---|
149 | /*!\ingroup time_format |
---|
150 | */ |
---|
151 | inline |
---|
152 | std::string |
---|
153 | to_simple_string(ptime t) |
---|
154 | { |
---|
155 | std::string ts = gregorian::to_simple_string(t.date());// + " "; |
---|
156 | if(!t.time_of_day().is_special()) { |
---|
157 | return ts + " " + to_simple_string(t.time_of_day()); |
---|
158 | } |
---|
159 | else { |
---|
160 | return ts; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff] |
---|
165 | /*!\ingroup time_format |
---|
166 | */ |
---|
167 | inline |
---|
168 | std::string |
---|
169 | to_simple_string(time_period tp) |
---|
170 | { |
---|
171 | std::string d1(to_simple_string(tp.begin())); |
---|
172 | std::string d2(to_simple_string(tp.last())); |
---|
173 | return std::string("[" + d1 + "/" + d2 +"]"); |
---|
174 | } |
---|
175 | |
---|
176 | //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator |
---|
177 | /*!\ingroup time_format |
---|
178 | */ |
---|
179 | inline |
---|
180 | std::string to_iso_string(ptime t) |
---|
181 | { |
---|
182 | std::string ts = gregorian::to_iso_string(t.date());// + "T"; |
---|
183 | if(!t.time_of_day().is_special()) { |
---|
184 | return ts + "T" + to_iso_string(t.time_of_day()); |
---|
185 | } |
---|
186 | else { |
---|
187 | return ts; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator |
---|
192 | /*!\ingroup time_format |
---|
193 | */ |
---|
194 | inline |
---|
195 | std::string |
---|
196 | to_iso_extended_string(ptime t) |
---|
197 | { |
---|
198 | std::string ts = gregorian::to_iso_extended_string(t.date());// + "T"; |
---|
199 | if(!t.time_of_day().is_special()) { |
---|
200 | return ts + "T" + to_simple_string(t.time_of_day()); |
---|
201 | } |
---|
202 | else { |
---|
203 | return ts; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | } } //namespace posix_time |
---|
209 | |
---|
210 | |
---|
211 | #endif |
---|
212 | |
---|