time_formatters_limited.hpp 5.8 KB

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