![]() |
Home | Libraries | People | FAQ | More |
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 庫並不需要額外的步驟。
注意:由於 serialization 庫的接口變化,現在要求所有可流操作的對象在寫出到存檔之前都必須是 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); // 注意:沒有宏
ofs.close();
std::ifstream ifs("tmp_file");
archive::text_iarchive ia(ifs);
ia >> pt2; // 注意:沒有宏
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)); // 對於 xml_archive 需要用宏
ofs.close();
std::ifstream ifs("tmp_file");
archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(d2); // 對於 xml_archive 需要用宏
ifs.close();
d == d2; // true
要使用 date_time 的序列化代碼,必須顯式包含正確的頭文件。頭文件分別為:
boost/date_time/gregorian/greg_serialize.hpp
和
boost/date_time/posix_time/time_serialize.hpp
| Copyright © 2001-2005 CrystalClear Software, Inc |