time_formatters.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #ifndef POSIXTIME_FORMATTERS_HPP___
  2. #define POSIXTIME_FORMATTERS_HPP___
  3. /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date: 2008-02-27 15:00:24 -0500 (Wed, 27 Feb 2008) $
  9. */
  10. #include "boost/date_time/gregorian/gregorian.hpp"
  11. #include "boost/date_time/compiler_config.hpp"
  12. #include "boost/date_time/iso_format.hpp"
  13. #include "boost/date_time/date_format_simple.hpp"
  14. #include "boost/date_time/posix_time/posix_time_types.hpp"
  15. #include "boost/date_time/time_formatting_streams.hpp"
  16. #include "boost/date_time/time_parsing.hpp"
  17. /* NOTE: The "to_*_string" code for older compilers, ones that define
  18. * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
  19. * formatters_limited.hpp
  20. */
  21. namespace boost {
  22. namespace posix_time {
  23. // template function called by wrapper functions:
  24. // to_*_string(time_duration) & to_*_wstring(time_duration)
  25. template<class charT>
  26. inline std::basic_string<charT> to_simple_string_type(time_duration td) {
  27. std::basic_ostringstream<charT> 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. charT fill_char = '0';
  50. if(td.is_negative()) {
  51. ss << '-';
  52. }
  53. ss << std::setw(2) << std::setfill(fill_char)
  54. << date_time::absolute_value(td.hours()) << ":";
  55. ss << std::setw(2) << std::setfill(fill_char)
  56. << date_time::absolute_value(td.minutes()) << ":";
  57. ss << std::setw(2) << std::setfill(fill_char)
  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. charT 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(fill_char)
  73. // JDG [7/6/02 VC++ compatibility]
  74. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  75. << buff;
  76. #else
  77. << frac_sec;
  78. #endif
  79. }
  80. }// else
  81. return ss.str();
  82. }
  83. //! Time duration to string -hh::mm::ss.fffffff. Example: 10:09:03.0123456
  84. /*!\ingroup time_format
  85. */
  86. inline std::string to_simple_string(time_duration td) {
  87. return to_simple_string_type<char>(td);
  88. }
  89. // template function called by wrapper functions:
  90. // to_*_string(time_duration) & to_*_wstring(time_duration)
  91. template<class charT>
  92. inline std::basic_string<charT> to_iso_string_type(time_duration td)
  93. {
  94. std::basic_ostringstream<charT> ss;
  95. if(td.is_special()) {
  96. /* simply using 'ss << td.get_rep()' won't work on compilers
  97. * that don't support locales. This way does. */
  98. // switch copied from date_names_put.hpp
  99. switch(td.get_rep().as_special()) {
  100. case not_a_date_time:
  101. //ss << "not-a-number";
  102. ss << "not-a-date-time";
  103. break;
  104. case pos_infin:
  105. ss << "+infinity";
  106. break;
  107. case neg_infin:
  108. ss << "-infinity";
  109. break;
  110. default:
  111. ss << "";
  112. }
  113. }
  114. else {
  115. charT fill_char = '0';
  116. if(td.is_negative()) {
  117. ss << '-';
  118. }
  119. ss << std::setw(2) << std::setfill(fill_char)
  120. << date_time::absolute_value(td.hours());
  121. ss << std::setw(2) << std::setfill(fill_char)
  122. << date_time::absolute_value(td.minutes());
  123. ss << std::setw(2) << std::setfill(fill_char)
  124. << date_time::absolute_value(td.seconds());
  125. //TODO the following is totally non-generic, yelling FIXME
  126. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  127. boost::int64_t frac_sec =
  128. date_time::absolute_value(td.fractional_seconds());
  129. // JDG [7/6/02 VC++ compatibility]
  130. charT buff[32];
  131. _i64toa(frac_sec, buff, 10);
  132. #else
  133. time_duration::fractional_seconds_type frac_sec =
  134. date_time::absolute_value(td.fractional_seconds());
  135. #endif
  136. if (frac_sec != 0) {
  137. ss << "." << std::setw(time_duration::num_fractional_digits())
  138. << std::setfill(fill_char)
  139. // JDG [7/6/02 VC++ compatibility]
  140. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  141. << buff;
  142. #else
  143. << frac_sec;
  144. #endif
  145. }
  146. }// else
  147. return ss.str();
  148. }
  149. //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456
  150. /*!\ingroup time_format
  151. */
  152. inline std::string to_iso_string(time_duration td){
  153. return to_iso_string_type<char>(td);
  154. }
  155. //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff
  156. /*!\ingroup time_format
  157. */
  158. template<class charT>
  159. inline std::basic_string<charT> to_simple_string_type(ptime t)
  160. {
  161. // can't use this w/gcc295, no to_simple_string_type<>(td) available
  162. std::basic_string<charT> ts = gregorian::to_simple_string_type<charT>(t.date());// + " ";
  163. if(!t.time_of_day().is_special()) {
  164. charT space = ' ';
  165. return ts + space + to_simple_string_type<charT>(t.time_of_day());
  166. }
  167. else {
  168. return ts;
  169. }
  170. }
  171. inline std::string to_simple_string(ptime t){
  172. return to_simple_string_type<char>(t);
  173. }
  174. // function called by wrapper functions to_*_string(time_period)
  175. // & to_*_wstring(time_period)
  176. template<class charT>
  177. inline std::basic_string<charT> to_simple_string_type(time_period tp)
  178. {
  179. charT beg = '[', mid = '/', end = ']';
  180. std::basic_string<charT> d1(to_simple_string_type<charT>(tp.begin()));
  181. std::basic_string<charT> d2(to_simple_string_type<charT>(tp.last()));
  182. return std::basic_string<charT>(beg + d1 + mid + d2 + end);
  183. }
  184. //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
  185. /*!\ingroup time_format
  186. */
  187. inline std::string to_simple_string(time_period tp){
  188. return to_simple_string_type<char>(tp);
  189. }
  190. // function called by wrapper functions to_*_string(time_period)
  191. // & to_*_wstring(time_period)
  192. template<class charT>
  193. inline std::basic_string<charT> to_iso_string_type(ptime t)
  194. {
  195. std::basic_string<charT> ts = gregorian::to_iso_string_type<charT>(t.date());// + "T";
  196. if(!t.time_of_day().is_special()) {
  197. charT sep = 'T';
  198. return ts + sep + to_iso_string_type<charT>(t.time_of_day());
  199. }
  200. else {
  201. return ts;
  202. }
  203. }
  204. //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator
  205. /*!\ingroup time_format
  206. */
  207. inline std::string to_iso_string(ptime t){
  208. return to_iso_string_type<char>(t);
  209. }
  210. // function called by wrapper functions to_*_string(time_period)
  211. // & to_*_wstring(time_period)
  212. template<class charT>
  213. inline std::basic_string<charT> to_iso_extended_string_type(ptime t)
  214. {
  215. std::basic_string<charT> ts = gregorian::to_iso_extended_string_type<charT>(t.date());// + "T";
  216. if(!t.time_of_day().is_special()) {
  217. charT sep = 'T';
  218. return ts + sep + to_simple_string_type<charT>(t.time_of_day());
  219. }
  220. else {
  221. return ts;
  222. }
  223. }
  224. //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
  225. /*!\ingroup time_format
  226. */
  227. inline std::string to_iso_extended_string(ptime t){
  228. return to_iso_extended_string_type<char>(t);
  229. }
  230. #if !defined(BOOST_NO_STD_WSTRING)
  231. //! Time duration to wstring -hh::mm::ss.fffffff. Example: 10:09:03.0123456
  232. /*!\ingroup time_format
  233. */
  234. inline std::wstring to_simple_wstring(time_duration td) {
  235. return to_simple_string_type<wchar_t>(td);
  236. }
  237. //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456
  238. /*!\ingroup time_format
  239. */
  240. inline std::wstring to_iso_wstring(time_duration td){
  241. return to_iso_string_type<wchar_t>(td);
  242. }
  243. inline std::wstring to_simple_wstring(ptime t){
  244. return to_simple_string_type<wchar_t>(t);
  245. }
  246. //! Convert to wstring of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
  247. /*!\ingroup time_format
  248. */
  249. inline std::wstring to_simple_wstring(time_period tp){
  250. return to_simple_string_type<wchar_t>(tp);
  251. }
  252. //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator
  253. /*!\ingroup time_format
  254. */
  255. inline std::wstring to_iso_wstring(ptime t){
  256. return to_iso_string_type<wchar_t>(t);
  257. }
  258. //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
  259. /*!\ingroup time_format
  260. */
  261. inline std::wstring to_iso_extended_wstring(ptime t){
  262. return to_iso_extended_string_type<wchar_t>(t);
  263. }
  264. #endif // BOOST_NO_STD_WSTRING
  265. } } //namespace posix_time
  266. #endif