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