123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #ifndef POSIXTIME_FORMATTERS_HPP___
- #define POSIXTIME_FORMATTERS_HPP___
- #include <boost/date_time/gregorian/gregorian.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/iso_format.hpp>
- #include <boost/date_time/date_format_simple.hpp>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <boost/date_time/time_formatting_streams.hpp>
- #include <boost/date_time/time_resolution_traits.hpp> // absolute_value
- #include <boost/date_time/time_parsing.hpp>
- namespace boost {
- namespace posix_time {
-
-
- template<class charT>
- inline std::basic_string<charT> to_simple_string_type(time_duration td) {
- std::basic_ostringstream<charT> ss;
- if(td.is_special()) {
-
-
- switch(td.get_rep().as_special())
- {
- case not_a_date_time:
-
- ss << "not-a-date-time";
- break;
- case pos_infin:
- ss << "+infinity";
- break;
- case neg_infin:
- ss << "-infinity";
- break;
- default:
- ss << "";
- }
- }
- else {
- charT fill_char = '0';
- if(td.is_negative()) {
- ss << '-';
- }
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.hours()) << ":";
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.minutes()) << ":";
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.seconds());
-
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
- boost::int64_t frac_sec =
- date_time::absolute_value(td.fractional_seconds());
-
- charT buff[32];
- _i64toa(frac_sec, buff, 10);
- #else
- time_duration::fractional_seconds_type frac_sec =
- date_time::absolute_value(td.fractional_seconds());
- #endif
- if (frac_sec != 0) {
- ss << "." << std::setw(time_duration::num_fractional_digits())
- << std::setfill(fill_char)
-
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
- << buff;
- #else
- << frac_sec;
- #endif
- }
- }
- return ss.str();
- }
-
-
- inline std::string to_simple_string(time_duration td) {
- return to_simple_string_type<char>(td);
- }
-
-
- template<class charT>
- inline std::basic_string<charT> to_iso_string_type(time_duration td)
- {
- std::basic_ostringstream<charT> ss;
- if(td.is_special()) {
-
-
- switch(td.get_rep().as_special()) {
- case not_a_date_time:
-
- ss << "not-a-date-time";
- break;
- case pos_infin:
- ss << "+infinity";
- break;
- case neg_infin:
- ss << "-infinity";
- break;
- default:
- ss << "";
- }
- }
- else {
- charT fill_char = '0';
- if(td.is_negative()) {
- ss << '-';
- }
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.hours());
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.minutes());
- ss << std::setw(2) << std::setfill(fill_char)
- << date_time::absolute_value(td.seconds());
-
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
- boost::int64_t frac_sec =
- date_time::absolute_value(td.fractional_seconds());
-
- charT buff[32];
- _i64toa(frac_sec, buff, 10);
- #else
- time_duration::fractional_seconds_type frac_sec =
- date_time::absolute_value(td.fractional_seconds());
- #endif
- if (frac_sec != 0) {
- ss << "." << std::setw(time_duration::num_fractional_digits())
- << std::setfill(fill_char)
-
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
- << buff;
- #else
- << frac_sec;
- #endif
- }
- }
- return ss.str();
- }
-
-
- inline std::string to_iso_string(time_duration td){
- return to_iso_string_type<char>(td);
- }
-
-
- template<class charT>
- inline std::basic_string<charT> to_simple_string_type(ptime t)
- {
-
- std::basic_string<charT> ts = gregorian::to_simple_string_type<charT>(t.date());
- if(!t.time_of_day().is_special()) {
- charT space = ' ';
- return ts + space + to_simple_string_type<charT>(t.time_of_day());
- }
- else {
- return ts;
- }
- }
- inline std::string to_simple_string(ptime t){
- return to_simple_string_type<char>(t);
- }
-
-
- template<class charT>
- inline std::basic_string<charT> to_simple_string_type(time_period tp)
- {
- charT beg = '[', mid = '/', end = ']';
- std::basic_string<charT> d1(to_simple_string_type<charT>(tp.begin()));
- std::basic_string<charT> d2(to_simple_string_type<charT>(tp.last()));
- return std::basic_string<charT>(beg + d1 + mid + d2 + end);
- }
-
-
- inline std::string to_simple_string(time_period tp){
- return to_simple_string_type<char>(tp);
- }
-
-
- template<class charT>
- inline std::basic_string<charT> to_iso_string_type(ptime t)
- {
- std::basic_string<charT> ts = gregorian::to_iso_string_type<charT>(t.date());
- if(!t.time_of_day().is_special()) {
- charT sep = 'T';
- return ts + sep + to_iso_string_type<charT>(t.time_of_day());
- }
- else {
- return ts;
- }
- }
-
-
- inline std::string to_iso_string(ptime t){
- return to_iso_string_type<char>(t);
- }
-
-
- template<class charT>
- inline std::basic_string<charT> to_iso_extended_string_type(ptime t)
- {
- std::basic_string<charT> ts = gregorian::to_iso_extended_string_type<charT>(t.date());
- if(!t.time_of_day().is_special()) {
- charT sep = 'T';
- return ts + sep + to_simple_string_type<charT>(t.time_of_day());
- }
- else {
- return ts;
- }
- }
-
-
- inline std::string to_iso_extended_string(ptime t){
- return to_iso_extended_string_type<char>(t);
- }
- #if !defined(BOOST_NO_STD_WSTRING)
-
-
- inline std::wstring to_simple_wstring(time_duration td) {
- return to_simple_string_type<wchar_t>(td);
- }
-
-
- inline std::wstring to_iso_wstring(time_duration td){
- return to_iso_string_type<wchar_t>(td);
- }
- inline std::wstring to_simple_wstring(ptime t){
- return to_simple_string_type<wchar_t>(t);
- }
-
-
- inline std::wstring to_simple_wstring(time_period tp){
- return to_simple_string_type<wchar_t>(tp);
- }
-
-
- inline std::wstring to_iso_wstring(ptime t){
- return to_iso_string_type<wchar_t>(t);
- }
-
-
- inline std::wstring to_iso_extended_wstring(ptime t){
- return to_iso_extended_string_type<wchar_t>(t);
- }
- #endif
- } }
- #endif
|