time_serialize.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef POSIX_TIME_SERIALIZE_HPP___
  2. #define POSIX_TIME_SERIALIZE_HPP___
  3. /* Copyright (c) 2004-2005 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: 2012-09-30 16:25:22 -0700 (Sun, 30 Sep 2012) $
  9. */
  10. #include "boost/date_time/posix_time/posix_time.hpp"
  11. #include "boost/date_time/gregorian/greg_serialize.hpp"
  12. #include "boost/serialization/split_free.hpp"
  13. #include "boost/serialization/nvp.hpp"
  14. // macros to split serialize functions into save & load functions
  15. // NOTE: these macros define template functions in the boost::serialization namespace.
  16. // They must be expanded *outside* of any namespace
  17. BOOST_SERIALIZATION_SPLIT_FREE(boost::posix_time::ptime)
  18. BOOST_SERIALIZATION_SPLIT_FREE(boost::posix_time::time_duration)
  19. BOOST_SERIALIZATION_SPLIT_FREE(boost::posix_time::time_period)
  20. namespace boost {
  21. namespace serialization {
  22. /*** time_duration ***/
  23. //! Function to save posix_time::time_duration objects using serialization lib
  24. /*! time_duration objects are broken down into 4 parts for serialization:
  25. * types are hour_type, min_type, sec_type, and fractional_seconds_type
  26. * as defined in the time_duration class
  27. */
  28. template<class Archive>
  29. void save(Archive & ar,
  30. const posix_time::time_duration& td,
  31. unsigned int /*version*/)
  32. {
  33. // serialize a bool so we know how to read this back in later
  34. bool is_special = td.is_special();
  35. ar & make_nvp("is_special", is_special);
  36. if(is_special) {
  37. std::string s = to_simple_string(td);
  38. ar & make_nvp("sv_time_duration", s);
  39. }
  40. else {
  41. posix_time::time_duration::hour_type h = td.hours();
  42. posix_time::time_duration::min_type m = td.minutes();
  43. posix_time::time_duration::sec_type s = td.seconds();
  44. posix_time::time_duration::fractional_seconds_type fs = td.fractional_seconds();
  45. ar & make_nvp("time_duration_hours", h);
  46. ar & make_nvp("time_duration_minutes", m);
  47. ar & make_nvp("time_duration_seconds", s);
  48. ar & make_nvp("time_duration_fractional_seconds", fs);
  49. }
  50. }
  51. //! Function to load posix_time::time_duration objects using serialization lib
  52. /*! time_duration objects are broken down into 4 parts for serialization:
  53. * types are hour_type, min_type, sec_type, and fractional_seconds_type
  54. * as defined in the time_duration class
  55. */
  56. template<class Archive>
  57. void load(Archive & ar,
  58. posix_time::time_duration & td,
  59. unsigned int /*version*/)
  60. {
  61. bool is_special = false;
  62. ar & make_nvp("is_special", is_special);
  63. if(is_special) {
  64. std::string s;
  65. ar & make_nvp("sv_time_duration", s);
  66. posix_time::special_values sv = gregorian::special_value_from_string(s);
  67. td = posix_time::time_duration(sv);
  68. }
  69. else {
  70. posix_time::time_duration::hour_type h(0);
  71. posix_time::time_duration::min_type m(0);
  72. posix_time::time_duration::sec_type s(0);
  73. posix_time::time_duration::fractional_seconds_type fs(0);
  74. ar & make_nvp("time_duration_hours", h);
  75. ar & make_nvp("time_duration_minutes", m);
  76. ar & make_nvp("time_duration_seconds", s);
  77. ar & make_nvp("time_duration_fractional_seconds", fs);
  78. td = posix_time::time_duration(h,m,s,fs);
  79. }
  80. }
  81. // no load_construct_data function provided as time_duration provides a
  82. // default constructor
  83. /*** ptime ***/
  84. //! Function to save posix_time::ptime objects using serialization lib
  85. /*! ptime objects are broken down into 2 parts for serialization:
  86. * a date object and a time_duration onject
  87. */
  88. template<class Archive>
  89. void save(Archive & ar,
  90. const posix_time::ptime& pt,
  91. unsigned int /*version*/)
  92. {
  93. // from_iso_string does not include fractional seconds
  94. // therefore date and time_duration are used
  95. posix_time::ptime::date_type d = pt.date();
  96. ar & make_nvp("ptime_date", d);
  97. if(!pt.is_special()) {
  98. posix_time::ptime::time_duration_type td = pt.time_of_day();
  99. ar & make_nvp("ptime_time_duration", td);
  100. }
  101. }
  102. //! Function to load posix_time::ptime objects using serialization lib
  103. /*! ptime objects are broken down into 2 parts for serialization:
  104. * a date object and a time_duration onject
  105. */
  106. template<class Archive>
  107. void load(Archive & ar,
  108. posix_time::ptime & pt,
  109. unsigned int /*version*/)
  110. {
  111. // from_iso_string does not include fractional seconds
  112. // therefore date and time_duration are used
  113. posix_time::ptime::date_type d(posix_time::not_a_date_time);
  114. posix_time::ptime::time_duration_type td;
  115. ar & make_nvp("ptime_date", d);
  116. if(!d.is_special()) {
  117. ar & make_nvp("ptime_time_duration", td);
  118. pt = boost::posix_time::ptime(d,td);
  119. }
  120. else {
  121. pt = boost::posix_time::ptime(d.as_special());
  122. }
  123. }
  124. //!override needed b/c no default constructor
  125. template<class Archive>
  126. inline void load_construct_data(Archive & /*ar*/,
  127. posix_time::ptime* pt,
  128. const unsigned int /*file_version*/)
  129. {
  130. // retrieve data from archive required to construct new
  131. // invoke inplace constructor to initialize instance of date
  132. new(pt) boost::posix_time::ptime(boost::posix_time::not_a_date_time);
  133. }
  134. /*** time_period ***/
  135. //! Function to save posix_time::time_period objects using serialization lib
  136. /*! time_period objects are broken down into 2 parts for serialization:
  137. * a begining ptime object and an ending ptime object
  138. */
  139. template<class Archive>
  140. void save(Archive & ar,
  141. const posix_time::time_period& tp,
  142. unsigned int /*version*/)
  143. {
  144. posix_time::ptime beg(tp.begin().date(), tp.begin().time_of_day());
  145. posix_time::ptime end(tp.end().date(), tp.end().time_of_day());
  146. ar & make_nvp("time_period_begin", beg);
  147. ar & make_nvp("time_period_end", end);
  148. }
  149. //! Function to load posix_time::time_period objects using serialization lib
  150. /*! time_period objects are broken down into 2 parts for serialization:
  151. * a begining ptime object and an ending ptime object
  152. */
  153. template<class Archive>
  154. void load(Archive & ar,
  155. boost::posix_time::time_period & tp,
  156. unsigned int /*version*/)
  157. {
  158. posix_time::time_duration td(1,0,0);
  159. gregorian::date d(gregorian::not_a_date_time);
  160. posix_time::ptime beg(d,td);
  161. posix_time::ptime end(d,td);
  162. ar & make_nvp("time_period_begin", beg);
  163. ar & make_nvp("time_period_end", end);
  164. tp = boost::posix_time::time_period(beg, end);
  165. }
  166. //!override needed b/c no default constructor
  167. template<class Archive>
  168. inline void load_construct_data(Archive & /*ar*/,
  169. boost::posix_time::time_period* tp,
  170. const unsigned int /*file_version*/)
  171. {
  172. posix_time::time_duration td(1,0,0);
  173. gregorian::date d(gregorian::not_a_date_time);
  174. posix_time::ptime beg(d,td);
  175. posix_time::ptime end(d,td);
  176. new(tp) boost::posix_time::time_period(beg,end);
  177. }
  178. } // namespace serialization
  179. } // namespace boost
  180. #endif