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