boost学习笔记(日期date)

date

日期就是所谓的年月日,boost中的date提供了年月日的表示方法。首先我们看下date的创建。需要包含gregorian.hpp头文件。

#include "stdafx.h"
#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>

using namespace std;
using namespace boost;

int main() {
    //使用构造函数创建date
    gregorian::date gdt0(2018,gregorian::Jan,1);
    cout << gdt0 << endl;

    //使用day_clock创建date
    gregorian::date gdt1 = gregorian::day_clock::local_day();
    cout << gregorian::to_iso_extended_string(gdt1) << endl;

    //使用字符串创建date
    gregorian::date gdt2 = gregorian::from_string("2018-12-12");
    cout << gdt2 << endl;
    return 0;
}
2018-Jan-01
2018-06-21
2018-Dec-12

还可以创建一些特殊的日期:

gregorian::date g0(gregorian::neg_infin);//负的无限日期
gregorian::date g1(gregorian::pos_infin);//正的无限日期
gregorian::date g2(gregorian::not_a_date_time);//无效的日期
gregorian::date g3(gregorian::max_date_time);//最大日期
gregorian::date g4(gregorian::min_date_time);//最小日期

cout << g0 << endl;
cout << g1 << endl;
cout << g2 << endl;
cout << g3 << endl;
cout << g4 << endl;
-infinity
+infinity
not-a-date-time
9999-Dec-31
1400-Jan-01

下面是一些date常见的方法的用法:

#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>

using namespace std;
using namespace boost;

int main() {

    //使用day_clock创建date
    gregorian::date gdt = gregorian::day_clock::local_day();
    cout << gregorian::to_iso_extended_string(gdt) << endl;
    //年月日结构
    gregorian::date::ymd_type ymd = gdt.year_month_day();
    cout << ymd.year << "-" << ymd.month << "-" << ymd.day << endl;

    cout << "day_of_week:" << gdt.day_of_week() << endl;//星期几
    cout << "day_of_year:" << gdt.day_of_year() << endl;//一年中第几天
    cout << "week num:" << gdt.week_number() << endl;//一年中的第几个星期
    cout << "end of month:" << gdt.end_of_month() << endl;//当月最后一天的日期

    //和tm结构的转换
    tm t = gregorian::to_tm(gdt);
    cout << (t.tm_year + 1900) << "-" << (t.tm_mon + 1) << "-" << t.tm_mday << " " << t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec << endl;

    gregorian::date gdt1 = gregorian::date_from_tm(t);
    assert(gdt == gdt1);

    return 0;
}
2018-06-21
2018-Jun-21
day_of_week:Thu
day_of_year:172
week num:25
end of month:2018-Jun-30
2018-6-21 0:0:0

日期和日期长度的运算:

#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>

using namespace std;
using namespace boost;

int main() {

    //使用day_clock创建date
    gregorian::date gdt = gregorian::day_clock::local_day();
    //日期长度
    gregorian::days d(1);//天
    gregorian::weeks w(1);//星期
    gregorian::months m(1);//月
    gregorian::years y(1);//年

    cout << d.days() << endl;
    cout << w.days() << endl;
    cout << m.number_of_months() << endl;
    cout << y.number_of_years() << endl;

    gregorian::days dayNum = d + w;
    cout << dayNum.days() << endl;
    gregorian::months monthNum = m + y;
    cout << monthNum.number_of_months() << endl;

    gdt += d;
    gdt += w;
    gdt += m;
    gdt += y;
    cout << gdt << endl;
    return 0;
}
1
7
1
1
8
13
2019-Jul-29

日期迭代器的用法:

#include <iostream>
#include <boost\date_time\gregorian\gregorian.hpp>

using namespace std;
using namespace boost;

int main() {

    //使用day_clock创建date
    gregorian::date gdt = gregorian::day_clock::local_day();
    cout << gdt << endl;

    gregorian::day_iterator dayIter(gdt);
    ++dayIter;
    cout << *dayIter << endl;


    gregorian::year_iterator yearIter(gdt);
    ++yearIter;
    cout << *yearIter << endl;
    return 0;
}
2018-Jun-21
2018-Jun-22
2019-Jun-21

猜你喜欢

转载自blog.csdn.net/maosijunzi/article/details/80757911