posix_time_legacy_io.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef POSIX_TIME_PRE133_OPERATORS_HPP___
  2. #define POSIX_TIME_PRE133_OPERATORS_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. /*! @file posix_time_pre133_operators.hpp
  11. * These input and output operators are for use with the
  12. * pre 1.33 version of the date_time libraries io facet code.
  13. * The operators used in version 1.33 and later can be found
  14. * in posix_time_io.hpp */
  15. #include <iostream>
  16. #include <string>
  17. #include <sstream>
  18. #include "boost/date_time/compiler_config.hpp"
  19. #include "boost/date_time/gregorian/gregorian.hpp"
  20. #include "boost/date_time/posix_time/posix_time_duration.hpp"
  21. #include "boost/date_time/posix_time/ptime.hpp"
  22. #include "boost/date_time/posix_time/time_period.hpp"
  23. #include "boost/date_time/time_parsing.hpp"
  24. namespace boost {
  25. namespace posix_time {
  26. //The following code is removed for configurations with poor std::locale support (eg: MSVC6, gcc 2.9x)
  27. #ifndef BOOST_DATE_TIME_NO_LOCALE
  28. #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
  29. //! ostream operator for posix_time::time_duration
  30. template <class charT, class traits>
  31. inline
  32. std::basic_ostream<charT, traits>&
  33. operator<<(std::basic_ostream<charT, traits>& os, const time_duration& td)
  34. {
  35. typedef boost::date_time::ostream_time_duration_formatter<time_duration, charT> duration_formatter;
  36. duration_formatter::duration_put(td, os);
  37. return os;
  38. }
  39. //! ostream operator for posix_time::ptime
  40. template <class charT, class traits>
  41. inline
  42. std::basic_ostream<charT, traits>&
  43. operator<<(std::basic_ostream<charT, traits>& os, const ptime& t)
  44. {
  45. typedef boost::date_time::ostream_time_formatter<ptime, charT> time_formatter;
  46. time_formatter::time_put(t, os);
  47. return os;
  48. }
  49. //! ostream operator for posix_time::time_period
  50. template <class charT, class traits>
  51. inline
  52. std::basic_ostream<charT, traits>&
  53. operator<<(std::basic_ostream<charT, traits>& os, const time_period& tp)
  54. {
  55. typedef boost::date_time::ostream_time_period_formatter<time_period, charT> period_formatter;
  56. period_formatter::period_put(tp, os);
  57. return os;
  58. }
  59. #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
  60. /******** input streaming ********/
  61. template<class charT>
  62. inline
  63. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, time_duration& td)
  64. {
  65. // need to create a std::string and parse it
  66. std::basic_string<charT> inp_s;
  67. std::stringstream out_ss;
  68. is >> inp_s;
  69. typename std::basic_string<charT>::iterator b = inp_s.begin();
  70. // need to use both iterators because there is no requirement
  71. // for the data held by a std::basic_string<> be terminated with
  72. // any marker (such as '\0').
  73. typename std::basic_string<charT>::iterator e = inp_s.end();
  74. while(b != e){
  75. out_ss << is.narrow(*b, 0);
  76. ++b;
  77. }
  78. td = date_time::parse_delimited_time_duration<time_duration>(out_ss.str());
  79. return is;
  80. }
  81. template<class charT>
  82. inline
  83. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, ptime& pt)
  84. {
  85. gregorian::date d(not_a_date_time);
  86. time_duration td(0,0,0);
  87. is >> d >> td;
  88. pt = ptime(d, td);
  89. return is;
  90. }
  91. /** operator>> for time_period. time_period must be in
  92. * "[date time_duration/date time_duration]" format. */
  93. template<class charT>
  94. inline
  95. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, time_period& tp)
  96. {
  97. gregorian::date d(not_a_date_time);
  98. time_duration td(0,0,0);
  99. ptime beg(d, td);
  100. ptime end(beg);
  101. std::basic_string<charT> s;
  102. // get first date string and remove leading '['
  103. is >> s;
  104. {
  105. std::basic_stringstream<charT> ss;
  106. ss << s.substr(s.find('[')+1);
  107. ss >> d;
  108. }
  109. // get first time_duration & second date string, remove the '/'
  110. // and split into 2 strings
  111. is >> s;
  112. {
  113. std::basic_stringstream<charT> ss;
  114. ss << s.substr(0, s.find('/'));
  115. ss >> td;
  116. }
  117. beg = ptime(d, td);
  118. {
  119. std::basic_stringstream<charT> ss;
  120. ss << s.substr(s.find('/')+1);
  121. ss >> d;
  122. }
  123. // get last time_duration and remove the trailing ']'
  124. is >> s;
  125. {
  126. std::basic_stringstream<charT> ss;
  127. ss << s.substr(0, s.find(']'));
  128. ss >> td;
  129. }
  130. end = ptime(d, td);
  131. tp = time_period(beg,end);
  132. return is;
  133. }
  134. #endif //BOOST_DATE_TIME_NO_LOCALE
  135. } } // namespaces
  136. #endif // POSIX_TIME_PRE133_OPERATORS_HPP___