time_formatting_streams.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef DATE_TIME_TIME_FORMATTING_STREAMS_HPP___
  2. #define DATE_TIME_TIME_FORMATTING_STREAMS_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/compiler_config.hpp>
  11. #ifndef BOOST_DATE_TIME_NO_LOCALE
  12. #include <locale>
  13. #include <iomanip>
  14. #include <iostream>
  15. #include <boost/date_time/date_formatting_locales.hpp>
  16. #include <boost/date_time/time_resolution_traits.hpp>
  17. namespace boost {
  18. namespace date_time {
  19. //! Put a time type into a stream using appropriate facets
  20. template<class time_duration_type,
  21. class charT = char>
  22. class ostream_time_duration_formatter
  23. {
  24. public:
  25. typedef std::basic_ostream<charT> ostream_type;
  26. typedef typename time_duration_type::fractional_seconds_type fractional_seconds_type;
  27. //! Put time into an ostream
  28. static void duration_put(const time_duration_type& td,
  29. ostream_type& os)
  30. {
  31. if(td.is_special()) {
  32. os << td.get_rep();
  33. }
  34. else {
  35. charT fill_char = '0';
  36. if(td.is_negative()) {
  37. os << '-';
  38. }
  39. os << std::setw(2) << std::setfill(fill_char)
  40. << absolute_value(td.hours()) << ":";
  41. os << std::setw(2) << std::setfill(fill_char)
  42. << absolute_value(td.minutes()) << ":";
  43. os << std::setw(2) << std::setfill(fill_char)
  44. << absolute_value(td.seconds());
  45. fractional_seconds_type frac_sec =
  46. absolute_value(td.fractional_seconds());
  47. if (frac_sec != 0) {
  48. os << "."
  49. << std::setw(time_duration_type::num_fractional_digits())
  50. << std::setfill(fill_char)
  51. << frac_sec;
  52. }
  53. } // else
  54. } // duration_put
  55. }; //class ostream_time_duration_formatter
  56. //! Put a time type into a stream using appropriate facets
  57. template<class time_type,
  58. class charT = char>
  59. class ostream_time_formatter
  60. {
  61. public:
  62. typedef std::basic_ostream<charT> ostream_type;
  63. typedef typename time_type::date_type date_type;
  64. typedef typename time_type::time_duration_type time_duration_type;
  65. typedef ostream_time_duration_formatter<time_duration_type, charT> duration_formatter;
  66. //! Put time into an ostream
  67. static void time_put(const time_type& t,
  68. ostream_type& os)
  69. {
  70. date_type d = t.date();
  71. os << d;
  72. if(!d.is_infinity() && !d.is_not_a_date())
  73. {
  74. os << " "; //TODO: fix the separator here.
  75. duration_formatter::duration_put(t.time_of_day(), os);
  76. }
  77. } // time_to_ostream
  78. }; //class ostream_time_formatter
  79. //! Put a time period into a stream using appropriate facets
  80. template<class time_period_type,
  81. class charT = char>
  82. class ostream_time_period_formatter
  83. {
  84. public:
  85. typedef std::basic_ostream<charT> ostream_type;
  86. typedef typename time_period_type::point_type time_type;
  87. typedef ostream_time_formatter<time_type, charT> time_formatter;
  88. //! Put time into an ostream
  89. static void period_put(const time_period_type& tp,
  90. ostream_type& os)
  91. {
  92. os << '['; //TODO: facet or manipulator for periods?
  93. time_formatter::time_put(tp.begin(), os);
  94. os << '/'; //TODO: facet or manipulator for periods?
  95. time_formatter::time_put(tp.last(), os);
  96. os << ']';
  97. } // period_put
  98. }; //class ostream_time_period_formatter
  99. } } //namespace date_time
  100. #endif //BOOST_DATE_TIME_NO_LOCALE
  101. #endif