1 獲取本地時(shí)間:
boost::posix_time::second_clock::local_time(); //當前時(shí)間
date tod = boost::gregorian::day_clock::local_day(); //當前日期
tod += years(1) // 加1年.
ptime p(boost::gregorian::date(2012,11,01),hours(1)); // 2012年11月1日 凌晨1點(diǎn)整.
2 把字符串轉換為boost 時(shí)間類(lèi)對象:
ptime p1 = from_iso_string("20121101T202020");
ptime p2 = time_from_string("2012-3-5 01:00:00");
3 轉換為時(shí)間字串:
to_simple_string(ptime);
to_iso_string(ptime);
to_iso_extended_string(ptime);
4 boost時(shí)間和其它結構的時(shí)間之間的轉換
boost::gregorian
std::tm to_tm(const date& d)
date date_from_tm(const std::tm& datetm)
boost::posix_time
ptime from_time_t(std::time_t t)
std::tm to_tm(const boost::posix_time::ptime& t)
std::tm to_tm(const boost::posix_time::time_duration& td)
ptime ptime_from_tm(const std::tm& timetm)
TimeT from_ftime(const FileTimeT& ft) ///這個(gè)要模版
c_local_adjustor static time_type utc_to_local(const time_type& t)//這個(gè)要模版
5 關(guān)于時(shí)間于時(shí)區
boost::gregorian::date today = boost::gregorian::day_clock::universal_day();
boost::local_time::tz_database tz_db;
tz_db.load_from_file("D:\Library\boost_1_51_0\libs\date_time\data\date_time_zonespec.csv");
boost::local_time::time_zone_ptr shz =tz_db.time_zone_from_region("Asia/Shanghai"); //東八區
boost::local_time::time_zone_ptr cst(new boost::local_time::posix_time_zone("CST+08"));//東八區
boost::local_time::local_date_time dt_bj(today,boost::posix_time::hours(12),shz,false);
//ptime 與地區無(wú)關(guān). local_time 這個(gè)才與地區有關(guān).
//把 東八區上海 時(shí)間轉換為 utc時(shí)間
boost::posix_time::ptime local_to_utc(const boost::posix_time::ptime & local_tm)
{
typedef boost::date_time::local_adjustor< boost::posix_time::ptime, +8, boost::posix_time::no_dst> sct_shz;// 上海 東八區
return sct_shz::local_to_utc(local_tm);
}
聯(lián)系客服