Boost之日期時間處理 date time庫

2021-07-13 23:25:55 字數 4326 閱讀 8062

使用date_time庫需要在編譯時加上"-lboost_date_time",而且需要包含以下標頭檔案:

處理日期的元件:#include

處理時間的元件:#include

date是date_time庫處理日期的核心類,使用乙個32位的整數作為內部儲存,以天為單位表示時間點概念。date也全面支援比較操作和輸入輸入出,我們可以完全把它當成乙個像int、string那樣的基本型別來使用。

#include #include using namespace std;

using namespace boost::gregorian;

int main()

輸出:本地日期:2016-jun-24

utc日期:2016-jun-24

負無限日期:1

正無限日期:2

無效日期:0

最大可能日期:4

最小可能日期:3

對於2001-01-01:

年:2001

月:jan

日:1對於2002-02-02:

星期數:(0表示星期天)sat

當年的第幾天:33

當年的第幾周:5

當月的最後一天:2002-feb-28

對於2003-03-03:

直接列印日期:2003-mar-03

使用to_******_string列印日期:2003-mar-03

使用to_iso_string列印日期:20030303

使用to_iso_extended_string列印日期:2003-03-03

date_duration表示日期長度,是以天為單位的時長,值可以是任意的整數,可正可負。date_time庫為date_duration定義了乙個常用的typedef: days。此外,date_time庫還提供了months、years、weeks等另外三個時長類,分別用來表示月,年和星期,它們的含義與days型別,但行為不太相同。

#include #include using namespace std;

using namespace boost::gregorian;

int main()

輸出:dd1 + dd2 = 5

3個星期的天數:21

3年5個月的月數:41

d2 - d1 = 365

d1 + days(10)後:2001-jan-11

d1 + months(2)後:2001-mar-11

d1 - weeks(1)後:2001-mar-04

d1 - years(10)後:1991-mar-04

date_period類用來表示日期區間的概念,它是時間軸上乙個左閉右開區間,端點是兩個date物件

#include #include using namespace std;

using namespace boost::gregorian;

int main()

輸出:dp1.begin() = 2000-jan-01

dp1.last() = 2000-jan-20

dp1.end() = 2000-jan-21

dp1.length() = 20

****f(): [2000-jan-04/2000-jan-23]

expand(): [2000-jan-01/2000-jan-26]

dp1在1999-12-12後

dp1在2000-01-20前

2000-01-10在dp1中

dp1包含dp2

dp1和dp2存在交集,交集為:[2000-jan-05/2000-jan-14]

dp1和dp2不相鄰

dp1.merge(dp3): [2000-jan-01/1999-dec-31]

dp1.span(dp3): [2000-jan-01/2000-feb-10]

通過日期迭代器可以用簡單的遞增遞減操作符連續訪問日期,這些迭代器包括:day_iterator、week_iterator、month_iterator和year_iterator。需要注意的是它們並不符合標準迭代器的定義,沒有difference_type、pointer、reference等內部型別定義,不能使用std::advance()或者operator+=來前進或者後退。

#include #include using namespace std;

using namespace boost::gregorian;

int main()

輸出:

d_iter == date(2000, 1, 2)

增長了8年

與date_duration類似,date_time庫使用time_duration度量時間長度。time_duration有幾個子類,可以度量不同的時間解析度,它們分別是:hours、minutes、seconds、millisec/millisecnods、microsec/microseconds和nanosec/nanoseconds。

#include #include using namespace std;

using namespace boost::posix_time;

int main()

輸出:td1: 01:10:30.001000

td2: 01:10:30.001000

hours: 1

minutes: 10

total_seconds: 4230

total_milliseconds: 4230001

to_******_string: 01:10:30.001000

to_iso_string: 011030.001000

ptime類表示時間點,它相當於乙個日期再加上乙個小於一天的時間長度。

#include #include #include using namespace std;

using namespace boost::posix_time;

using namespace boost::gregorian;

int main()

輸出:2001-jan-01 01:00:00

2002-feb-02 02:00:00

2003-mar-03 03:00:00

2016-jun-26 15:35:23

2016-jun-26 07:35:23.240536

2001-jan-01, 01:00:00

2002-feb-02 02:00:00

20020202t020000

2002-02-02t02:00:00

與日期區間date_period對應,date_time庫也有時間區間的概念,類time_period使用ptime作為區間的兩個端點,同樣是左閉右開區間,它與date_period用法基本相同。

#include #include #include using namespace std;

using namespace boost::posix_time;

using namespace boost::gregorian;

int main()

輸出:兩個區間相鄰

兩個區間不相交

tp1在中午之後

兩個區間現在相交

tp2包含p且包含tp1

不同於日期迭代器,時間迭代器只有乙個time_iterator。它在構造時傳入乙個起始時間點ptime物件和乙個步長time_duration物件,然後就同日期迭代器一樣使用前置式operator++、operator--來遞增或遞減時間,解引用操作符返回乙個ptime物件。

#include #include #include using namespace std;

using namespace boost::posix_time;

using namespace boost::gregorian;

int main()

輸出:2014-nov-03 10:00:00

2014-nov-03 10:10:00

2014-nov-03 10:20:00

2014-nov-03 10:30:00

2014-nov-03 10:40:00

2014-nov-03 10:50:00

Qt之日期時間

獲取系統當前時間並設定顯示格式 qdatetime current date time qdatetime currentdatetime qstring current date current date time.tostring yyyy mm dd hh mm ss ddd 例 2013 0...

c 之日期時間

c 標準庫沒有提供所謂的日期型別。c 繼承了 c 語言用於日期和時間操作的結構和函式。為了使用日期和時間相關的函式和結構,需要在 c 程式中引用 標頭檔案。有四個與時間相關的型別 clock t time t size t和tm。型別 clock t size t 和 time t 能夠把系統時間和...

boost 時間和日期

include boost posix time microsec clock universal time 格林威治時間 和上面的應該是精度不一樣 date tod boost gregorian day clock local day 當前日期 tod years 1 加1年.ptime p b...