boost::gregorian日期

 

#include <cstdio>
#include <iostream>
#include <sstream>

#include <boost/date_time/gregorian/gregorian.hpp>

using namespace std;
using namespace boost;

int main()
{
    //使用构造函数创建date
    gregorian::date gdt0(2020, gregorian::Jan, 1);
    //使用day_clock创建date
    gregorian::date gdt1 = gregorian::day_clock::local_day();
    gregorian::date gdt2 = gregorian::day_clock::universal_day();
    gregorian::date gdt3(gregorian::not_a_date_time);//无效的日期
    gregorian::date gdt4(gregorian::max_date_time);//最大日期
    gregorian::date gdt5(gregorian::min_date_time);//最小日期
    cout << gdt0 << endl;
    gregorian::day_iterator dayIter(gdt0);
    gregorian::month_iterator monthIter(gdt0);
    gregorian::year_iterator yearIter(gdt0);
    
    //日期长度
    gregorian::days d(1);//
    gregorian::weeks w(1);//星期
    gregorian::months m(1);//
    gregorian::years y(1);//

    gdt0 += d;
    gdt0 += w;
    gdt0 += m;
    gdt0 += y;
    cout << gdt0 << endl;

    gregorian::date::ymd_type ymd = gdt0.year_month_day();
    cout << ymd.year << "-" << ymd.month << "-" << ymd.day << endl;

    cout << "星期几:" << gdt0.day_of_week() << endl;
    cout << "一年中第几天:" << gdt0.day_of_year() << endl;
    cout << "一年中的第几个星期:" << gdt0.week_number() << endl;
    cout << "当月最后一天的日期:" << gdt0.end_of_month() << endl;

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

 

posted @ 2021-03-06 23:04  osbreak  阅读(282)  评论(0编辑  收藏  举报