1/* Copyright (c) 2002-2005 CrystalClear Software, Inc.
2 * Use, modification and distribution is subject to the
3 * Boost Software License, Version 1.0. (See accompanying
4 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
5 * Author: Jeff Garland, Bart Garst
6 */
7
8#include <boost/archive/text_oarchive.hpp>
9#include <boost/archive/text_iarchive.hpp>
10#include <boost/archive/xml_oarchive.hpp>
11#include <boost/archive/xml_iarchive.hpp>
12#include <boost/date_time/posix_time/posix_time.hpp>
13#include <boost/date_time/posix_time/time_serialize.hpp>
14#include "../testfrmwk.hpp"
15#include <sstream>
16
17using namespace boost;
18using namespace posix_time;
19using namespace gregorian;
20
21template<class archive_type, class temporal_type>
22void save_to(archive_type& ar, const temporal_type& tt)
23{
24 ar << tt;
25}
26
27int main(){
28 // originals
29 date d(2002, Feb, 14);
30#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
31 time_duration td(12,13,52,123456789);
32#else
33 time_duration td(12,13,52,123456);
34#endif
35 ptime pt(d, td);
36 time_period tp(pt, ptime(date(2002, Oct, 31), hours(19)));
37 ptime sv_pt1(not_a_date_time);
38 ptime sv_pt2(pos_infin);
39 time_duration sv_td(neg_infin);
40
41 // for loading in from archive
42 date d2(not_a_date_time);
43 time_duration td2(1,0,0);
44 ptime pt2(d2, td2);
45 time_period tp2(pt2, hours(1));
46 ptime sv_pt3(min_date_time);
47 ptime sv_pt4(min_date_time);
48 time_duration sv_td2(0,0,0);
49
50 std::ostringstream oss;
51
52 // NOTE: DATE_TIME_XML_SERIALIZE is only used in testing and is
53 // defined in the testing Jamfile
54#if defined(DATE_TIME_XML_SERIALIZE)
55 std::cout << "Running xml archive tests" << std::endl;
56 archive::xml_oarchive oa(oss);
57#else
58 std::cout << "Running text archive tests" << std::endl;
59 archive::text_oarchive oa(oss);
60#endif // DATE_TIME_XML_SERIALIZE
61
62 try{
63#if defined(DATE_TIME_XML_SERIALIZE)
64 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(pt));
65 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(sv_pt1));
66 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(sv_pt2));
67 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(tp));
68 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(td));
69 save_to(ar&: oa, BOOST_SERIALIZATION_NVP(sv_td));
70#else
71 save_to(oa, pt);
72 save_to(oa, sv_pt1);
73 save_to(oa, sv_pt2);
74 save_to(oa, tp);
75 save_to(oa, td);
76 save_to(oa, sv_td);
77#endif // DATE_TIME_XML_SERIALIZE
78 }catch(archive::archive_exception& ae){
79 std::string s(ae.what());
80 check(testname: "Error writing to archive: " + s + "\nWritten data: \"" + oss.str() + "\"", testcond: false);
81 return printTestStats();
82 }
83
84 std::istringstream iss(oss.str());
85#if defined(DATE_TIME_XML_SERIALIZE)
86 archive::xml_iarchive ia(iss);
87#else
88 archive::text_iarchive ia(iss);
89#endif // DATE_TIME_XML_SERIALIZE
90
91 try{
92#if defined(DATE_TIME_XML_SERIALIZE)
93 ia >> BOOST_SERIALIZATION_NVP(pt2);
94 ia >> BOOST_SERIALIZATION_NVP(sv_pt3);
95 ia >> BOOST_SERIALIZATION_NVP(sv_pt4);
96 ia >> BOOST_SERIALIZATION_NVP(tp2);
97 ia >> BOOST_SERIALIZATION_NVP(td2);
98 ia >> BOOST_SERIALIZATION_NVP(sv_td2);
99#else
100 ia >> pt2;
101 ia >> sv_pt3;
102 ia >> sv_pt4;
103 ia >> tp2;
104 ia >> td2;
105 ia >> sv_td2;
106#endif // DATE_TIME_XML_SERIALIZE
107 }catch(archive::archive_exception& ae){
108 std::string s(ae.what());
109 check(testname: "Error readng from archive: " + s + "\nWritten data: \"" + oss.str() + "\"", testcond: false);
110 return printTestStats();
111 }
112
113 check(testname: "ptime", testcond: pt == pt2);
114 check(testname: "special_values ptime (nadt)", testcond: sv_pt1 == sv_pt3);
115 check(testname: "special_values ptime (pos_infin)", testcond: sv_pt2 == sv_pt4);
116 check(testname: "time_period", testcond: tp == tp2);
117 check(testname: "time_duration", testcond: td == td2);
118 check(testname: "special_values time_duration (neg_infin)", testcond: sv_td == sv_td2);
119
120 return printTestStats();
121}
122

source code of boost/libs/date_time/test/posix_time/testtime_serialize.cpp