]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/date_time/gregorian/greg_serialize.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / date_time / gregorian / greg_serialize.hpp
CommitLineData
7c673cae
FG
1#ifndef GREGORIAN_SERIALIZE_HPP___
2#define GREGORIAN_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$
10 */
11
12#include "boost/date_time/gregorian/gregorian_types.hpp"
13#include "boost/date_time/gregorian/parsers.hpp"
20effc67
TL
14#include "boost/core/nvp.hpp"
15
7c673cae
FG
16
17namespace boost {
b32b8144
FG
18
19 namespace gregorian {
20 std::string to_iso_string(const date&);
21 }
22
7c673cae
FG
23namespace serialization {
24
20effc67
TL
25// A macro to split serialize functions into save & load functions.
26// It is here to avoid dependency on Boost.Serialization just for the
27// BOOST_SERIALIZATION_SPLIT_FREE macro
28#define BOOST_DATE_TIME_SPLIT_FREE(T) \
29template<class Archive> \
30inline void serialize(Archive & ar, \
31 T & t, \
32 const unsigned int file_version) \
33{ \
34 split_free(ar, t, file_version); \
35}
36
7c673cae
FG
37/*! Method that does serialization for gregorian::date -- splits to load/save
38 */
20effc67
TL
39BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date)
40BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_duration)
41BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
42BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_period)
43BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_year)
44BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_month)
45BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_day)
46BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_weekday)
47BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::partial_date)
48BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
49BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
50BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
51BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_before)
52BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_after)
53
54#undef BOOST_DATE_TIME_SPLIT_FREE
7c673cae
FG
55
56//! Function to save gregorian::date objects using serialization lib
57/*! Dates are serialized into a string for transport and storage.
58 * While it would be more efficient to store the internal
59 * integer used to manipulate the dates, it is an unstable solution.
60 */
61template<class Archive>
62void save(Archive & ar,
63 const ::boost::gregorian::date & d,
64 unsigned int /* version */)
65{
66 std::string ds = to_iso_string(d);
67 ar & make_nvp("date", ds);
68}
69
70//! Function to load gregorian::date objects using serialization lib
71/*! Dates are serialized into a string for transport and storage.
72 * While it would be more efficient to store the internal
73 * integer used to manipulate the dates, it is an unstable solution.
74 */
75template<class Archive>
76void load(Archive & ar,
77 ::boost::gregorian::date & d,
78 unsigned int /*version*/)
79{
80 std::string ds;
81 ar & make_nvp("date", ds);
82 try{
83 d = ::boost::gregorian::from_undelimited_string(ds);
84 }catch(bad_lexical_cast&) {
85 gregorian::special_values sv = gregorian::special_value_from_string(ds);
86 if(sv == gregorian::not_special) {
87 throw; // no match found, rethrow original exception
88 }
89 else {
90 d = gregorian::date(sv);
91 }
92 }
93}
94
95
96//!override needed b/c no default constructor
97template<class Archive>
98inline void load_construct_data(Archive & /*ar*/,
99 ::boost::gregorian::date* dp,
100 const unsigned int /*file_version*/)
101{
102 // retrieve data from archive required to construct new
103 // invoke inplace constructor to initialize instance of date
104 ::new(dp) ::boost::gregorian::date(::boost::gregorian::not_a_date_time);
105}
106
107/**** date_duration ****/
108
109//! Function to save gregorian::date_duration objects using serialization lib
110template<class Archive>
111void save(Archive & ar, const gregorian::date_duration & dd,
112 unsigned int /*version*/)
113{
114 typename gregorian::date_duration::duration_rep dr = dd.get_rep();
115 ar & make_nvp("date_duration", dr);
116}
117//! Function to load gregorian::date_duration objects using serialization lib
118template<class Archive>
119void load(Archive & ar, gregorian::date_duration & dd, unsigned int /*version*/)
120{
121 typename gregorian::date_duration::duration_rep dr(0);
122 ar & make_nvp("date_duration", dr);
123 dd = gregorian::date_duration(dr);
124}
125//!override needed b/c no default constructor
126template<class Archive>
127inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration* dd,
128 const unsigned int /*file_version*/)
129{
130 ::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
131}
132
133/**** date_duration::duration_rep (most likely int_adapter) ****/
134
135//! helper unction to save date_duration objects using serialization lib
136template<class Archive>
137void save(Archive & ar, const gregorian::date_duration::duration_rep & dr,
138 unsigned int /*version*/)
139{
140 typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
141 ar & make_nvp("date_duration_duration_rep", it);
142}
143//! helper function to load date_duration objects using serialization lib
144template<class Archive>
145void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int /*version*/)
146{
147 typename gregorian::date_duration::duration_rep::int_type it(0);
148 ar & make_nvp("date_duration_duration_rep", it);
149 dr = gregorian::date_duration::duration_rep::int_type(it);
150}
151//!override needed b/c no default constructor
152template<class Archive>
153inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration::duration_rep* dr,
154 const unsigned int /*file_version*/)
155{
156 ::new(dr) gregorian::date_duration::duration_rep(0);
157}
158
159/**** date_period ****/
160
161//! Function to save gregorian::date_period objects using serialization lib
162/*! date_period objects are broken down into 2 parts for serialization:
163 * the begining date object and the end date object
164 */
165template<class Archive>
166void save(Archive & ar, const gregorian::date_period& dp,
167 unsigned int /*version*/)
168{
169 gregorian::date d1 = dp.begin();
170 gregorian::date d2 = dp.end();
171 ar & make_nvp("date_period_begin_date", d1);
172 ar & make_nvp("date_period_end_date", d2);
173}
174//! Function to load gregorian::date_period objects using serialization lib
175/*! date_period objects are broken down into 2 parts for serialization:
176 * the begining date object and the end date object
177 */
178template<class Archive>
179void load(Archive & ar, gregorian::date_period& dp, unsigned int /*version*/)
180{
181 gregorian::date d1(gregorian::not_a_date_time);
182 gregorian::date d2(gregorian::not_a_date_time);
183 ar & make_nvp("date_period_begin_date", d1);
184 ar & make_nvp("date_period_end_date", d2);
185 dp = gregorian::date_period(d1,d2);
186}
187//!override needed b/c no default constructor
188template<class Archive>
189inline void load_construct_data(Archive & /*ar*/, gregorian::date_period* dp,
190 const unsigned int /*file_version*/)
191{
192 gregorian::date d(gregorian::not_a_date_time);
193 gregorian::date_duration dd(1);
194 ::new(dp) gregorian::date_period(d,dd);
195}
196
197/**** greg_year ****/
198
199//! Function to save gregorian::greg_year objects using serialization lib
200template<class Archive>
201void save(Archive & ar, const gregorian::greg_year& gy,
202 unsigned int /*version*/)
203{
204 unsigned short us = gy;
205 ar & make_nvp("greg_year", us);
206}
207//! Function to load gregorian::greg_year objects using serialization lib
208template<class Archive>
209void load(Archive & ar, gregorian::greg_year& gy, unsigned int /*version*/)
210{
211 unsigned short us;
212 ar & make_nvp("greg_year", us);
213 gy = gregorian::greg_year(us);
214}
215//!override needed b/c no default constructor
216template<class Archive>
217inline void load_construct_data(Archive & /*ar*/, gregorian::greg_year* gy,
218 const unsigned int /*file_version*/)
219{
220 ::new(gy) gregorian::greg_year(1900);
221}
222
223/**** greg_month ****/
224
225//! Function to save gregorian::greg_month objects using serialization lib
226template<class Archive>
227void save(Archive & ar, const gregorian::greg_month& gm,
228 unsigned int /*version*/)
229{
230 unsigned short us = gm.as_number();
231 ar & make_nvp("greg_month", us);
232}
233//! Function to load gregorian::greg_month objects using serialization lib
234template<class Archive>
235void load(Archive & ar, gregorian::greg_month& gm, unsigned int /*version*/)
236{
237 unsigned short us;
238 ar & make_nvp("greg_month", us);
239 gm = gregorian::greg_month(us);
240}
241//!override needed b/c no default constructor
242template<class Archive>
243inline void load_construct_data(Archive & /*ar*/, gregorian::greg_month* gm,
244 const unsigned int /*file_version*/)
245{
246 ::new(gm) gregorian::greg_month(1);
247}
248
249/**** greg_day ****/
250
251//! Function to save gregorian::greg_day objects using serialization lib
252template<class Archive>
253void save(Archive & ar, const gregorian::greg_day& gd,
254 unsigned int /*version*/)
255{
256 unsigned short us = gd.as_number();
257 ar & make_nvp("greg_day", us);
258}
259//! Function to load gregorian::greg_day objects using serialization lib
260template<class Archive>
261void load(Archive & ar, gregorian::greg_day& gd, unsigned int /*version*/)
262{
263 unsigned short us;
264 ar & make_nvp("greg_day", us);
265 gd = gregorian::greg_day(us);
266}
267//!override needed b/c no default constructor
268template<class Archive>
269inline void load_construct_data(Archive & /*ar*/, gregorian::greg_day* gd,
270 const unsigned int /*file_version*/)
271{
272 ::new(gd) gregorian::greg_day(1);
273}
274
275/**** greg_weekday ****/
276
277//! Function to save gregorian::greg_weekday objects using serialization lib
278template<class Archive>
279void save(Archive & ar, const gregorian::greg_weekday& gd,
280 unsigned int /*version*/)
281{
282 unsigned short us = gd.as_number();
283 ar & make_nvp("greg_weekday", us);
284}
285//! Function to load gregorian::greg_weekday objects using serialization lib
286template<class Archive>
287void load(Archive & ar, gregorian::greg_weekday& gd, unsigned int /*version*/)
288{
289 unsigned short us;
290 ar & make_nvp("greg_weekday", us);
291 gd = gregorian::greg_weekday(us);
292}
293//!override needed b/c no default constructor
294template<class Archive>
295inline void load_construct_data(Archive & /*ar*/, gregorian::greg_weekday* gd,
296 const unsigned int /*file_version*/)
297{
298 ::new(gd) gregorian::greg_weekday(1);
299}
300
301/**** date_generators ****/
302
303/**** partial_date ****/
304
305//! Function to save gregorian::partial_date objects using serialization lib
306/*! partial_date objects are broken down into 2 parts for serialization:
307 * the day (typically greg_day) and month (typically greg_month) objects
308 */
309template<class Archive>
310void save(Archive & ar, const gregorian::partial_date& pd,
311 unsigned int /*version*/)
312{
313 gregorian::greg_day gd(pd.day());
314 gregorian::greg_month gm(pd.month().as_number());
315 ar & make_nvp("partial_date_day", gd);
316 ar & make_nvp("partial_date_month", gm);
317}
318//! Function to load gregorian::partial_date objects using serialization lib
319/*! partial_date objects are broken down into 2 parts for serialization:
320 * the day (greg_day) and month (greg_month) objects
321 */
322template<class Archive>
323void load(Archive & ar, gregorian::partial_date& pd, unsigned int /*version*/)
324{
325 gregorian::greg_day gd(1);
326 gregorian::greg_month gm(1);
327 ar & make_nvp("partial_date_day", gd);
328 ar & make_nvp("partial_date_month", gm);
329 pd = gregorian::partial_date(gd,gm);
330}
331//!override needed b/c no default constructor
332template<class Archive>
333inline void load_construct_data(Archive & /*ar*/, gregorian::partial_date* pd,
334 const unsigned int /*file_version*/)
335{
336 gregorian::greg_month gm(1);
337 gregorian::greg_day gd(1);
338 ::new(pd) gregorian::partial_date(gd,gm);
339}
340
341/**** nth_kday_of_month ****/
342
343//! Function to save nth_day_of_the_week_in_month objects using serialization lib
344/*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
345 * serialization: the week number, the day of the week, and the month
346 */
347template<class Archive>
348void save(Archive & ar, const gregorian::nth_kday_of_month& nkd,
349 unsigned int /*version*/)
350{
351 typename gregorian::nth_kday_of_month::week_num wn(nkd.nth_week());
352 typename gregorian::nth_kday_of_month::day_of_week_type d(nkd.day_of_week().as_number());
353 typename gregorian::nth_kday_of_month::month_type m(nkd.month().as_number());
354 ar & make_nvp("nth_kday_of_month_week_num", wn);
355 ar & make_nvp("nth_kday_of_month_day_of_week", d);
356 ar & make_nvp("nth_kday_of_month_month", m);
357}
358//! Function to load nth_day_of_the_week_in_month objects using serialization lib
359/*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
360 * serialization: the week number, the day of the week, and the month
361 */
362template<class Archive>
363void load(Archive & ar, gregorian::nth_kday_of_month& nkd, unsigned int /*version*/)
364{
365 typename gregorian::nth_kday_of_month::week_num wn(gregorian::nth_kday_of_month::first);
366 typename gregorian::nth_kday_of_month::day_of_week_type d(gregorian::Monday);
367 typename gregorian::nth_kday_of_month::month_type m(gregorian::Jan);
368 ar & make_nvp("nth_kday_of_month_week_num", wn);
369 ar & make_nvp("nth_kday_of_month_day_of_week", d);
370 ar & make_nvp("nth_kday_of_month_month", m);
371
372 nkd = gregorian::nth_kday_of_month(wn,d,m);
373}
374//!override needed b/c no default constructor
375template<class Archive>
376inline void load_construct_data(Archive & /*ar*/,
377 gregorian::nth_kday_of_month* nkd,
378 const unsigned int /*file_version*/)
379{
380 // values used are not significant
381 ::new(nkd) gregorian::nth_kday_of_month(gregorian::nth_kday_of_month::first,
382 gregorian::Monday,gregorian::Jan);
383}
384
385/**** first_kday_of_month ****/
386
387//! Function to save first_day_of_the_week_in_month objects using serialization lib
388/*! first_day_of_the_week_in_month objects are broken down into 2 parts for
389 * serialization: the day of the week, and the month
390 */
391template<class Archive>
392void save(Archive & ar, const gregorian::first_kday_of_month& fkd,
393 unsigned int /*version*/)
394{
395 typename gregorian::first_kday_of_month::day_of_week_type d(fkd.day_of_week().as_number());
396 typename gregorian::first_kday_of_month::month_type m(fkd.month().as_number());
397 ar & make_nvp("first_kday_of_month_day_of_week", d);
398 ar & make_nvp("first_kday_of_month_month", m);
399}
400//! Function to load first_day_of_the_week_in_month objects using serialization lib
401/*! first_day_of_the_week_in_month objects are broken down into 2 parts for
402 * serialization: the day of the week, and the month
403 */
404template<class Archive>
405void load(Archive & ar, gregorian::first_kday_of_month& fkd, unsigned int /*version*/)
406{
407 typename gregorian::first_kday_of_month::day_of_week_type d(gregorian::Monday);
408 typename gregorian::first_kday_of_month::month_type m(gregorian::Jan);
409 ar & make_nvp("first_kday_of_month_day_of_week", d);
410 ar & make_nvp("first_kday_of_month_month", m);
411
412 fkd = gregorian::first_kday_of_month(d,m);
413}
414//!override needed b/c no default constructor
415template<class Archive>
416inline void load_construct_data(Archive & /*ar*/,
417 gregorian::first_kday_of_month* fkd,
418 const unsigned int /*file_version*/)
419{
420 // values used are not significant
421 ::new(fkd) gregorian::first_kday_of_month(gregorian::Monday,gregorian::Jan);
422}
423
424/**** last_kday_of_month ****/
425
426//! Function to save last_day_of_the_week_in_month objects using serialization lib
427/*! last_day_of_the_week_in_month objects are broken down into 2 parts for
428 * serialization: the day of the week, and the month
429 */
430template<class Archive>
431void save(Archive & ar, const gregorian::last_kday_of_month& lkd,
432 unsigned int /*version*/)
433{
434 typename gregorian::last_kday_of_month::day_of_week_type d(lkd.day_of_week().as_number());
435 typename gregorian::last_kday_of_month::month_type m(lkd.month().as_number());
436 ar & make_nvp("last_kday_of_month_day_of_week", d);
437 ar & make_nvp("last_kday_of_month_month", m);
438}
439//! Function to load last_day_of_the_week_in_month objects using serialization lib
440/*! last_day_of_the_week_in_month objects are broken down into 2 parts for
441 * serialization: the day of the week, and the month
442 */
443template<class Archive>
444void load(Archive & ar, gregorian::last_kday_of_month& lkd, unsigned int /*version*/)
445{
446 typename gregorian::last_kday_of_month::day_of_week_type d(gregorian::Monday);
447 typename gregorian::last_kday_of_month::month_type m(gregorian::Jan);
448 ar & make_nvp("last_kday_of_month_day_of_week", d);
449 ar & make_nvp("last_kday_of_month_month", m);
450
451 lkd = gregorian::last_kday_of_month(d,m);
452}
453//!override needed b/c no default constructor
454template<class Archive>
455inline void load_construct_data(Archive & /*ar*/,
456 gregorian::last_kday_of_month* lkd,
457 const unsigned int /*file_version*/)
458{
459 // values used are not significant
460 ::new(lkd) gregorian::last_kday_of_month(gregorian::Monday,gregorian::Jan);
461}
462
463/**** first_kday_before ****/
464
465//! Function to save first_day_of_the_week_before objects using serialization lib
466template<class Archive>
467void save(Archive & ar, const gregorian::first_kday_before& fkdb,
468 unsigned int /*version*/)
469{
470 typename gregorian::first_kday_before::day_of_week_type d(fkdb.day_of_week().as_number());
471 ar & make_nvp("first_kday_before_day_of_week", d);
472}
473//! Function to load first_day_of_the_week_before objects using serialization lib
474template<class Archive>
475void load(Archive & ar, gregorian::first_kday_before& fkdb, unsigned int /*version*/)
476{
477 typename gregorian::first_kday_before::day_of_week_type d(gregorian::Monday);
478 ar & make_nvp("first_kday_before_day_of_week", d);
479
480 fkdb = gregorian::first_kday_before(d);
481}
482//!override needed b/c no default constructor
483template<class Archive>
484inline void load_construct_data(Archive & /*ar*/,
485 gregorian::first_kday_before* fkdb,
486 const unsigned int /*file_version*/)
487{
488 // values used are not significant
489 ::new(fkdb) gregorian::first_kday_before(gregorian::Monday);
490}
491
492/**** first_kday_after ****/
493
494//! Function to save first_day_of_the_week_after objects using serialization lib
495template<class Archive>
496void save(Archive & ar, const gregorian::first_kday_after& fkda,
497 unsigned int /*version*/)
498{
499 typename gregorian::first_kday_after::day_of_week_type d(fkda.day_of_week().as_number());
500 ar & make_nvp("first_kday_after_day_of_week", d);
501}
502//! Function to load first_day_of_the_week_after objects using serialization lib
503template<class Archive>
504void load(Archive & ar, gregorian::first_kday_after& fkda, unsigned int /*version*/)
505{
506 typename gregorian::first_kday_after::day_of_week_type d(gregorian::Monday);
507 ar & make_nvp("first_kday_after_day_of_week", d);
508
509 fkda = gregorian::first_kday_after(d);
510}
511//!override needed b/c no default constructor
512template<class Archive>
513inline void load_construct_data(Archive & /*ar*/,
514 gregorian::first_kday_after* fkda,
515 const unsigned int /*file_version*/)
516{
517 // values used are not significant
518 ::new(fkda) gregorian::first_kday_after(gregorian::Monday);
519}
520
521} // namespace serialization
522} // namespace boost
523
524#endif