File indexing completed on 2025-01-30 09:35:21
0001 #ifndef POSIX_TIME_SERIALIZE_HPP___
0002 #define POSIX_TIME_SERIALIZE_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "boost/date_time/posix_time/posix_time.hpp"
0013 #include "boost/date_time/gregorian/greg_serialize.hpp"
0014 #include "boost/core/nvp.hpp"
0015 #include "boost/numeric/conversion/cast.hpp"
0016 #include "boost/type_traits/integral_constant.hpp"
0017
0018
0019
0020 #ifndef BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION
0021 #define BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION 1
0022 #endif
0023
0024 namespace boost {
0025 namespace serialization {
0026
0027 template<typename T>
0028 struct version;
0029
0030 template<>
0031 struct version<boost::posix_time::time_duration>
0032 : integral_constant<int, BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION>
0033 {
0034 };
0035
0036
0037
0038
0039 #define BOOST_DATE_TIME_SPLIT_FREE(T) \
0040 template<class Archive> \
0041 inline void serialize(Archive & ar, \
0042 T & t, \
0043 const unsigned int file_version) \
0044 { \
0045 split_free(ar, t, file_version); \
0046 }
0047
0048 BOOST_DATE_TIME_SPLIT_FREE(boost::posix_time::ptime)
0049 BOOST_DATE_TIME_SPLIT_FREE(boost::posix_time::time_duration)
0050 BOOST_DATE_TIME_SPLIT_FREE(boost::posix_time::time_period)
0051
0052 #undef BOOST_DATE_TIME_SPLIT_FREE
0053
0054
0055
0056
0057
0058
0059
0060
0061 template<class TimeResTraitsSize, class Archive>
0062 void save_td(Archive& ar, const posix_time::time_duration& td)
0063 {
0064 TimeResTraitsSize h = boost::numeric_cast<TimeResTraitsSize>(td.hours());
0065 TimeResTraitsSize m = boost::numeric_cast<TimeResTraitsSize>(td.minutes());
0066 TimeResTraitsSize s = boost::numeric_cast<TimeResTraitsSize>(td.seconds());
0067 posix_time::time_duration::fractional_seconds_type fs = td.fractional_seconds();
0068 ar & make_nvp("time_duration_hours", h);
0069 ar & make_nvp("time_duration_minutes", m);
0070 ar & make_nvp("time_duration_seconds", s);
0071 ar & make_nvp("time_duration_fractional_seconds", fs);
0072 }
0073
0074 template<class Archive>
0075 void save(Archive & ar,
0076 const posix_time::time_duration& td,
0077 unsigned int version)
0078 {
0079
0080 bool is_special = td.is_special();
0081 ar & make_nvp("is_special", is_special);
0082 if(is_special) {
0083 std::string s = to_simple_string(td);
0084 ar & make_nvp("sv_time_duration", s);
0085 }
0086 else {
0087
0088
0089 if (version == 0) {
0090 save_td<int32_t>(ar, td);
0091 } else {
0092 save_td<int64_t>(ar, td);
0093 }
0094 }
0095 }
0096
0097
0098
0099
0100
0101
0102 template<class TimeResTraitsSize, class Archive>
0103 void load_td(Archive& ar, posix_time::time_duration& td)
0104 {
0105 TimeResTraitsSize h(0);
0106 TimeResTraitsSize m(0);
0107 TimeResTraitsSize s(0);
0108 posix_time::time_duration::fractional_seconds_type fs(0);
0109 ar & make_nvp("time_duration_hours", h);
0110 ar & make_nvp("time_duration_minutes", m);
0111 ar & make_nvp("time_duration_seconds", s);
0112 ar & make_nvp("time_duration_fractional_seconds", fs);
0113 td = posix_time::time_duration(h, m, s, fs);
0114 }
0115
0116 template<class Archive>
0117 void load(Archive & ar,
0118 posix_time::time_duration & td,
0119 unsigned int version)
0120 {
0121 bool is_special = false;
0122 ar & make_nvp("is_special", is_special);
0123 if(is_special) {
0124 std::string s;
0125 ar & make_nvp("sv_time_duration", s);
0126 posix_time::special_values sv = gregorian::special_value_from_string(s);
0127 td = posix_time::time_duration(sv);
0128 }
0129 else {
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 BOOST_STATIC_ASSERT(sizeof(posix_time::time_duration::hour_type) == sizeof(boost::int64_t));
0140 BOOST_STATIC_ASSERT(sizeof(posix_time::time_duration::min_type) == sizeof(boost::int64_t));
0141 BOOST_STATIC_ASSERT(sizeof(posix_time::time_duration::sec_type) == sizeof(boost::int64_t));
0142 BOOST_STATIC_ASSERT(sizeof(posix_time::time_duration::fractional_seconds_type) == sizeof(boost::int64_t));
0143
0144 if (version == 0) {
0145 load_td<int32_t>(ar, td);
0146 } else {
0147 load_td<int64_t>(ar, td);
0148 }
0149 }
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 template<class Archive>
0162 void save(Archive & ar,
0163 const posix_time::ptime& pt,
0164 unsigned int )
0165 {
0166
0167
0168 posix_time::ptime::date_type d = pt.date();
0169 ar & make_nvp("ptime_date", d);
0170 if(!pt.is_special()) {
0171 posix_time::ptime::time_duration_type td = pt.time_of_day();
0172 ar & make_nvp("ptime_time_duration", td);
0173 }
0174 }
0175
0176
0177
0178
0179
0180 template<class Archive>
0181 void load(Archive & ar,
0182 posix_time::ptime & pt,
0183 unsigned int )
0184 {
0185
0186
0187 posix_time::ptime::date_type d(posix_time::not_a_date_time);
0188 posix_time::ptime::time_duration_type td;
0189 ar & make_nvp("ptime_date", d);
0190 if(!d.is_special()) {
0191 ar & make_nvp("ptime_time_duration", td);
0192 pt = boost::posix_time::ptime(d,td);
0193 }
0194 else {
0195 pt = boost::posix_time::ptime(d.as_special());
0196 }
0197
0198 }
0199
0200
0201 template<class Archive>
0202 inline void load_construct_data(Archive & ,
0203 posix_time::ptime* pt,
0204 const unsigned int )
0205 {
0206
0207
0208 new(pt) boost::posix_time::ptime(boost::posix_time::not_a_date_time);
0209 }
0210
0211
0212
0213
0214
0215
0216
0217 template<class Archive>
0218 void save(Archive & ar,
0219 const posix_time::time_period& tp,
0220 unsigned int )
0221 {
0222 posix_time::ptime beg(tp.begin().date(), tp.begin().time_of_day());
0223 posix_time::ptime end(tp.end().date(), tp.end().time_of_day());
0224 ar & make_nvp("time_period_begin", beg);
0225 ar & make_nvp("time_period_end", end);
0226 }
0227
0228
0229
0230
0231
0232 template<class Archive>
0233 void load(Archive & ar,
0234 boost::posix_time::time_period & tp,
0235 unsigned int )
0236 {
0237 posix_time::time_duration td(1,0,0);
0238 gregorian::date d(gregorian::not_a_date_time);
0239 posix_time::ptime beg(d,td);
0240 posix_time::ptime end(d,td);
0241 ar & make_nvp("time_period_begin", beg);
0242 ar & make_nvp("time_period_end", end);
0243 tp = boost::posix_time::time_period(beg, end);
0244 }
0245
0246
0247 template<class Archive>
0248 inline void load_construct_data(Archive & ,
0249 boost::posix_time::time_period* tp,
0250 const unsigned int )
0251 {
0252 posix_time::time_duration td(1,0,0);
0253 gregorian::date d(gregorian::not_a_date_time);
0254 posix_time::ptime beg(d,td);
0255 posix_time::ptime end(d,td);
0256 new(tp) boost::posix_time::time_period(beg,end);
0257 }
0258
0259 }
0260 }
0261
0262 #endif