Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Serialization

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:

boost::gregorian

date date_duration date_period
partial_date nth_day_of_week_in_month first_day_of_week_in_month
last_day_of_week_in_month first_day_of_week_before first_day_of_week_after
greg_month greg_day greg_weekday

boost::posix_time

ptime time_duration time_period

No extra steps are required to build the date_time library for serialization use.

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.

      template<class archive_type, class temporal_type>
      void save_to(archive_type& ar, const temporal_type& tt)
      {
        ar << tt;
      }
    

Example text_archive usage:

      using namespace boost::posix_time;
      using namespace boost::gregorian;
      ptime pt(date(2002, Feb, 14)), hours(10)), pt2(not_a_date_time);
      std::ofstream ofs("tmp_file");
      archive::test_oarchive oa(ofs);
      save_to(oa, pt);                 // NOTE: no macro
      ofs.close();
      std::ifstream ifs("tmp_file");
      archive::text_iarchive ia(ifs);
      ia >> pt2;                       // NOTE: no macro
      ifs.close();
      pt == pt2; // true

Example xml_archive usage:

      using namespace boost::gregorian;
      date d(2002, Feb, 14), d2(not_a_date_time);
      std::ofstream ofs("tmp_file");
      archive::xml_oarchive oa(ofs);
      save_to(oa, BOOST_SERIALIZATION_NVP(d)); // macro required for xml_archive
      ofs.close();
      std::ifstream ifs("tmp_file");
      archive::xml_iarchive ia(ifs);
      ia >> BOOST_SERIALIZATION_NVP(d2);       // macro required for xml_archive
      ifs.close();
      d == d2; // true

To use the date_time serialization code, the proper header files must be explicitly included. The header files are:

      boost/date_time/gregorian/greg_serialize.hpp

and

      boost/date_time/posix_time/time_serialize.hpp

Copyright 2001-2005 CrystalClear Software, Inc

PrevUpHomeNext