Boost學習筆記(2) Date

2021-06-09 12:14:44 字數 1955 閱讀 6953

1.標頭檔案

#inlcude

using namespace boost::gregorian;

2.日期初始化方式

date d1;

date d2(2012,09,13);

date d3(2000,jan,2);

date d4(d2);

date d5=from_string("2012-12-12");

date d6=from_string("2012/12/12");

date d3=from_undelimited_string("20121212");

特殊初始化

date d1(neg_infin);//負無限日期

date d2(pos_infin);//正無限日期

date d3(not_a_date_time);//無效日期

date d4(max_date_time);//最大日期

date d5(min_date_time);//最小日期

日期訪問

date d(2010,3,2);

d.year();

d.month();

d.day();

date::ymd_type ymd=d.year_month_day();

ymd.year;

ymd.month;

ymd.day;

d.day_of_week()

d.day_of_year();

d.end_of_month();//返回當月的最後一天

d.week_number();//日期為當年的第幾周

日期檢測

is_infinity();

is_neg_infinity();

is_pos_infinity();

is_not_a_date();

is_special();

時間輸出

to_******_string(date d)//輸出 yyyy-mmm-dd,其中mmm為三個字元的英文月份名

to_iso_string(date d)//轉換成yyyymmdd

to_iso_extended_string(date d)//轉換為 yyyy-mm-dd

tm轉換

to_tm(date) date轉換到tm,tm時分秒(tm_hjour,tm_min,tm_sec)均為0,夏令時tm_isdst為-1;

date_from_tm(tm date),tm轉化為年月日,其他被忽略

日期區間(date_period)

period(date,date);

period(date,days);

date begin() const;

date end() const;

date last() const;

days length() const;

bool is_null() const;

void shift(const days&);

void expand(const days&);

bool contains(const date&)const;

bool contains(const period&)const;

date日期迭代器

day_iterator

week_iterator

month_iterator

year_iterator

使用方式

date d(2006,10,23);

day_iterator d_iter(d);

++d_iter;

year_iterator y_iter(*d_iter,3);

date是需要編譯的庫,所以程式在編譯時需要帶上靜態庫

g++ -o datetest datetest.cpp -l $boost_lib -l boost_date_time-gcc44-mt-1_51

boost 學習筆記

先來看看如何賦值把 include include include include include include include using namespace std int tmain int argc,tchar ar include include include include incl...

Boost學習筆記 operators

如果乙個類要實現operator operator 意味著可能也要實現operator operator 等其它操作符。只要提供了operator operator 就可以根據這兩個操作符實現operator operator 等其它操作符。而且這種實現是單調的 operator const t x...

Boost學習筆記 filesystem

檔案操作是程式設計中非常重要的乙個部分,filesystem庫是乙個可移植的檔案系統操作庫,它使用posix標準檔案系統的路徑,介面很類似標準庫的容器和迭代器,使c 具有了類似指令碼餘姚的功能,可以跨平台操作目錄 檔案,寫出通用的指令碼程式。filesystem庫的核心類是path,它遮蔽了不同檔案...