time_serialize.hpp 6.9 KB

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