1 #ifndef POSIXTIME_FORMATTERS_LIMITED_HPP___
2 #define POSIXTIME_FORMATTERS_LIMITED_HPP___
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
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"
21 namespace posix_time
{
23 //! Time duration to string -hh::mm::ss.fffffff. Example: 10:09:03.0123456
24 /*!\ingroup time_format
26 inline std::string
to_simple_string(time_duration td
) {
27 std::ostringstream ss
;
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())
35 //ss << "not-a-number";
36 ss
<< "not-a-date-time";
49 if(td
.is_negative()) {
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 < 1300))
60 boost::int64_t frac_sec
=
61 date_time::absolute_value(td
.fractional_seconds());
62 // JDG [7/6/02 VC++ compatibility]
64 _i64toa(frac_sec
, buff
, 10);
66 time_duration::fractional_seconds_type frac_sec
=
67 date_time::absolute_value(td
.fractional_seconds());
70 ss
<< "." << std::setw(time_duration::num_fractional_digits())
73 // JDG [7/6/02 VC++ compatibility]
74 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
84 //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456
85 /*!\ingroup time_format
89 to_iso_string(time_duration td
)
91 std::ostringstream ss
;
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()) {
98 //ss << "not-a-number";
99 ss
<< "not-a-date-time";
112 if(td
.is_negative()) {
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 < 1300))
123 boost::int64_t frac_sec
=
124 date_time::absolute_value(td
.fractional_seconds());
125 // JDG [7/6/02 VC++ compatibility]
127 _i64toa(frac_sec
, buff
, 10);
129 time_duration::fractional_seconds_type frac_sec
=
130 date_time::absolute_value(td
.fractional_seconds());
133 ss
<< "." << std::setw(time_duration::num_fractional_digits())
136 // JDG [7/6/02 VC++ compatibility]
137 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
147 //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff
148 /*!\ingroup time_format
152 to_simple_string(ptime t
)
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());
163 //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
164 /*!\ingroup time_format
168 to_simple_string(time_period tp
)
170 std::string
d1(to_simple_string(tp
.begin()));
171 std::string
d2(to_simple_string(tp
.last()));
172 return std::string("[" + d1
+ "/" + d2
+"]");
175 //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator
176 /*!\ingroup time_format
179 std::string
to_iso_string(ptime t
)
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());
190 //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
191 /*!\ingroup time_format
195 to_iso_extended_string(ptime t
)
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());
207 } } //namespace posix_time