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