boost 時間和日期

2021-06-18 17:56:58 字數 1745 閱讀 7226

#include

boost::posix_time::microsec_clock::universal_time(); //格林威治時間 和上面的應該是精度不一樣

date tod = boost::gregorian::day_clock::local_day(); //當前日期

tod += years(1)  // 加1年.

ptime p(boost::gregorian::date(2012,11,01),hours(1)); // 2023年11月1日 凌晨1點整.

2 把字串轉換為boost 時間類物件:

ptime p1 = from_iso_string("20121101t202020");

ptime p2  = time_from_string("2012-3-5 01:00:00");

3 轉換為時間字串:

to_******_string(ptime);

to_iso_string(ptime);

to_iso_extended_string(ptime);

4 boost時間和其它結構的時間之間的轉換

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) ///這個要模版

c_local_adjustor static time_type utc_to_local(const time_type& t)//這個要模版

5 關於時間於時區

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 與地區無關. local_time 這個才與地區有關.

//把 東八區上海 時間轉換為 utc時間

boost::posix_time::ptime local_to_utc(const boost::posix_time::ptime & local_tm)

boost 時間與日期

timer include using namespace boost progress timer p36 繼承自timer,析構時自動輸出時間 include using namespace boost 將progress timer的輸出轉移到時stringstream,轉換為其他字串 str...

Boost庫時間日期學習

學習內容見 boost程式庫完全開發指南 第2章 時間與日期 學習三個類的使用timer progress timer date。boost庫的配置可參照 開發環境 winxp vs2005 boost 1 49 0。以下所有示例程式均為控制台程式。1 timer類 timer類是乙個小型的計時器,...

boost 日期與時間相關庫

1.timer類 include stdafx.h include include using namespace std using namespace boost int tmain int argc,tchar argv 2.boost progress timer t類 自動列印輸出流逝的時...