1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" |
---|
3 | "../../../tools/boostbook/dtd/boostbook.dtd"> |
---|
4 | |
---|
5 | <!-- Copyright (c) 2001-2005 CrystalClear Software, Inc. |
---|
6 | Subject to the Boost Software License, Version 1.0. |
---|
7 | (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | --> |
---|
9 | |
---|
10 | <section id="date_time.serialization"> |
---|
11 | <title>Serialization</title> |
---|
12 | |
---|
13 | <para> |
---|
14 | The boost::date_time library is compatible with the boost::serialization library's text and xml archives. The list of classes that are serializable are: |
---|
15 | </para> |
---|
16 | |
---|
17 | <bridgehead renderas="sect3">boost::gregorian</bridgehead> |
---|
18 | |
---|
19 | <informaltable frame="all" pgwide="1"> |
---|
20 | <tgroup cols="3"> |
---|
21 | <tbody> |
---|
22 | <row> |
---|
23 | <entry><link linkend="date_time.gregorian.date_class">date</link></entry> |
---|
24 | <entry><link linkend="date_time.gregorian.date_duration">date_duration</link></entry> |
---|
25 | <entry><link linkend="date_time.gregorian.date_period">date_period</link></entry> |
---|
26 | </row> |
---|
27 | <row> |
---|
28 | <entry><link linkend="date_time.gregorian.date_algorithms">partial_date</link></entry> |
---|
29 | <entry><link linkend="date_time.gregorian.date_algorithms">nth_day_of_week_in_month</link></entry> |
---|
30 | <entry><link linkend="date_time.gregorian.date_algorithms">first_day_of_week_in_month</link></entry> |
---|
31 | </row> |
---|
32 | <row> |
---|
33 | <entry><link linkend="date_time.gregorian.date_algorithms">last_day_of_week_in_month</link></entry> |
---|
34 | <entry><link linkend="date_time.gregorian.date_algorithms">first_day_of_week_before</link></entry> |
---|
35 | <entry><link linkend="date_time.gregorian.date_algorithms">first_day_of_week_after</link></entry> |
---|
36 | </row> |
---|
37 | <row> |
---|
38 | <entry>greg_month</entry> <!-- no docs to link to for these three --> |
---|
39 | <entry>greg_day</entry> |
---|
40 | <entry>greg_weekday</entry> |
---|
41 | </row> |
---|
42 | </tbody> |
---|
43 | </tgroup> |
---|
44 | </informaltable> |
---|
45 | |
---|
46 | <bridgehead renderas="sect3">boost::posix_time</bridgehead> |
---|
47 | |
---|
48 | <informaltable frame="all" pgwide="1"> |
---|
49 | <tgroup cols="3"> |
---|
50 | <tbody> |
---|
51 | <row> |
---|
52 | <entry><link linkend="date_time.posix_time.ptime_class">ptime</link></entry> |
---|
53 | <entry><link linkend="date_time.posix_time.time_duration">time_duration</link></entry> |
---|
54 | <entry><link linkend="date_time.posix_time.time_period">time_period</link></entry> |
---|
55 | </row> |
---|
56 | </tbody> |
---|
57 | </tgroup> |
---|
58 | </informaltable> |
---|
59 | |
---|
60 | <para> |
---|
61 | No extra steps are required to build the date_time library for serialization use. |
---|
62 | </para> |
---|
63 | |
---|
64 | <para>NOTE: due to a change in the serialization library interface, it is now required that all streamable objects be const prior to writing to the archive. The following template function will allow for this (and is used in the date_time tests). At this time no special steps are necessary to read from an archive. |
---|
65 | <programlisting> |
---|
66 | template<class archive_type, class temporal_type> |
---|
67 | void save_to(archive_type& ar, const temporal_type& tt) |
---|
68 | { |
---|
69 | ar << tt; |
---|
70 | } |
---|
71 | </programlisting> |
---|
72 | </para> |
---|
73 | |
---|
74 | <para> |
---|
75 | Example text_archive usage: |
---|
76 | <programlisting> |
---|
77 | using namespace boost::posix_time; |
---|
78 | using namespace boost::gregorian; |
---|
79 | ptime pt(date(2002, Feb, 14)), hours(10)), pt2(not_a_date_time); |
---|
80 | std::ofstream ofs("tmp_file"); |
---|
81 | archive::test_oarchive oa(ofs); |
---|
82 | save_to(oa, pt); // NOTE: no macro |
---|
83 | ofs.close(); |
---|
84 | std::ifstream ifs("tmp_file"); |
---|
85 | archive::text_iarchive ia(ifs); |
---|
86 | ia >> pt2; // NOTE: no macro |
---|
87 | ifs.close(); |
---|
88 | pt == pt2; // true</programlisting> |
---|
89 | </para> |
---|
90 | |
---|
91 | <para> |
---|
92 | Example xml_archive usage: |
---|
93 | <programlisting> |
---|
94 | using namespace boost::gregorian; |
---|
95 | date d(2002, Feb, 14), d2(not_a_date_time); |
---|
96 | std::ofstream ofs("tmp_file"); |
---|
97 | archive::xml_oarchive oa(ofs); |
---|
98 | save_to(oa, BOOST_SERIALIZATION_NVP(d)); // macro required for xml_archive |
---|
99 | ofs.close(); |
---|
100 | std::ifstream ifs("tmp_file"); |
---|
101 | archive::xml_iarchive ia(ifs); |
---|
102 | ia >> BOOST_SERIALIZATION_NVP(d2); // macro required for xml_archive |
---|
103 | ifs.close(); |
---|
104 | d == d2; // true</programlisting> |
---|
105 | </para> |
---|
106 | |
---|
107 | <para> |
---|
108 | To use the date_time serialization code, the proper header files must be explicitly included. The header files are: |
---|
109 | <programlisting> |
---|
110 | boost/date_time/gregorian/greg_serialize.hpp</programlisting> |
---|
111 | and |
---|
112 | <programlisting> |
---|
113 | boost/date_time/posix_time/time_serialize.hpp</programlisting> |
---|
114 | </para> |
---|
115 | |
---|
116 | </section> |
---|