boost::date_time 库与 boost::serialization 库的文本和 XML 存档兼容。 可序列化的类列表如下:
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 |
无需额外步骤即可构建用于序列化的 date_time 库。
注意:由于序列化库接口的更改,现在要求所有可流式对象在写入存档之前必须是 const。 以下模板函数将允许这样做(并在 date_time 测试中使用)。 目前,从存档读取无需特殊步骤。
template<class archive_type, class temporal_type> void save_to(archive_type& ar, const temporal_type& tt) { ar << tt; }
示例 text_archive 用法
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
示例 xml_archive 用法
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
要使用 date_time 序列化代码,必须显式包含正确的头文件。 头文件是
boost/date_time/gregorian/greg_serialize.hpp
and
boost/date_time/posix_time/time_serialize.hpp