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