• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Serialization</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../date_time.html" title="Chapter 13. Boost.Date_Time">
10<link rel="prev" href="date_time_io.html" title="Date Time Input/Output">
11<link rel="next" href="details.html" title="Details">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="date_time_io.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="details.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="date_time.serialization"></a>Serialization</h2></div></div></div>
29<p>
30    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:
31  </p>
32<h4>
33<a name="id-1.3.14.12.3"></a>boost::gregorian</h4>
34<div class="informaltable"><table class="table" width="100%">
35<colgroup>
36<col>
37<col>
38<col>
39</colgroup>
40<tbody>
41<tr>
42<td><a class="link" href="gregorian.html#date_time.gregorian.date_class" title="Date">date</a></td>
43<td><a class="link" href="gregorian.html#date_time.gregorian.date_duration" title="Date Duration (aka Days)">date_duration</a></td>
44<td><a class="link" href="gregorian.html#date_time.gregorian.date_period" title="Date Period">date_period</a></td>
45</tr>
46<tr>
47<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">partial_date</a></td>
48<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">nth_day_of_week_in_month</a></td>
49<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">first_day_of_week_in_month</a></td>
50</tr>
51<tr>
52<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">last_day_of_week_in_month</a></td>
53<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">first_day_of_week_before</a></td>
54<td><a class="link" href="gregorian.html#date_time.gregorian.date_algorithms" title="Date Generators/Algorithms">first_day_of_week_after</a></td>
55</tr>
56<tr>
57<td>greg_month</td>
58<td>greg_day</td>
59<td>greg_weekday</td>
60</tr>
61</tbody>
62</table></div>
63<h4>
64<a name="id-1.3.14.12.5"></a>boost::posix_time</h4>
65<div class="informaltable"><table class="table" width="100%">
66<colgroup>
67<col>
68<col>
69<col>
70</colgroup>
71<tbody><tr>
72<td><a class="link" href="posix_time.html#date_time.posix_time.ptime_class" title="Ptime">ptime</a></td>
73<td><a class="link" href="posix_time.html#date_time.posix_time.time_duration" title="Time Duration">time_duration</a></td>
74<td><a class="link" href="posix_time.html#date_time.posix_time.time_period" title="Time Period">time_period</a></td>
75</tr></tbody>
76</table></div>
77<p>
78    No extra steps are required to build the date_time library for serialization use.
79  </p>
80<p>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.
81    </p>
82<pre class="programlisting">
83      template&lt;class archive_type, class temporal_type&gt;
84      void save_to(archive_type&amp; ar, const temporal_type&amp; tt)
85      {
86        ar &lt;&lt; tt;
87      }
88    </pre>
89<p>
90  </p>
91<p>
92    Example text_archive usage:
93    </p>
94<pre class="programlisting">
95      using namespace boost::posix_time;
96      using namespace boost::gregorian;
97      ptime pt(date(2002, Feb, 14)), hours(10)), pt2(not_a_date_time);
98      std::ofstream ofs("tmp_file");
99      archive::test_oarchive oa(ofs);
100      save_to(oa, pt);                 // NOTE: no macro
101      ofs.close();
102      std::ifstream ifs("tmp_file");
103      archive::text_iarchive ia(ifs);
104      ia &gt;&gt; pt2;                       // NOTE: no macro
105      ifs.close();
106      pt == pt2; // true</pre>
107<p>
108  </p>
109<p>
110    Example xml_archive usage:
111    </p>
112<pre class="programlisting">
113      using namespace boost::gregorian;
114      date d(2002, Feb, 14), d2(not_a_date_time);
115      std::ofstream ofs("tmp_file");
116      archive::xml_oarchive oa(ofs);
117      save_to(oa, BOOST_SERIALIZATION_NVP(d)); // macro required for xml_archive
118      ofs.close();
119      std::ifstream ifs("tmp_file");
120      archive::xml_iarchive ia(ifs);
121      ia &gt;&gt; BOOST_SERIALIZATION_NVP(d2);       // macro required for xml_archive
122      ifs.close();
123      d == d2; // true</pre>
124<p>
125  </p>
126<p>
127    To use the date_time serialization code, the proper header files must be explicitly included. The header files are:
128    </p>
129<pre class="programlisting">
130      boost/date_time/gregorian/greg_serialize.hpp</pre>
131<p>
132    and
133    </p>
134<pre class="programlisting">
135      boost/date_time/posix_time/time_serialize.hpp</pre>
136<p>
137  </p>
138</div>
139<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
140<td align="left"></td>
141<td align="right"><div class="copyright-footer">Copyright © 2001-2005 CrystalClear Software, Inc<p>Subject to the Boost Software License, Version 1.0. (See accompanying file
142    <code class="filename">LICENSE_1_0.txt</code> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</p>
143</div></td>
144</tr></table>
145<hr>
146<div class="spirit-nav">
147<a accesskey="p" href="date_time_io.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="details.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
148</div>
149</body>
150</html>
151