时间2 boost:date 年月日

// 忽略警告
// 调试日期:2019-03-13 星期3
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <assert.h>
#include <iostream>
#include <boost/integer.hpp>
#include <time.h>
#include <boost/timer.hpp>        // timer
#include <boost/progress.hpp>    // progress_timer、progress_display
#include <sstream>                // stringstream
#include <boost/date_time/gregorian/gregorian.hpp>// date_time
using namespace boost::gregorian;
using namespace boost;
using namespace std;
#include <Windows.h>// Sleep

int main()
{

    
    ///
    // date_time库范围[1400-01-01,9999,12,31]
    // 构造
    date d1;
    date d2(2019, 3, 13);
    date d3(2019, Mar, 13);
    date d4(d2);
    assert(d1 == date(not_a_date_time));// 无效
    assert(d2 == d4);assert(d3 == d2);// 时间一致
    
    date d5 = from_string("2019-3-13");
    date d6(from_string("2019/03/13"));
    date d7 = from_undelimited_string("20190313");// 不能写成2019313

    // 同一个时间assert断言均为真
    assert(d5 == d6);assert(d5 == d7);assert(d5 == d2);
    
    // 返回当天的日期对象
    cout << day_clock::local_day() << endl;// 2019-Mar-13            // local_day
    cout << day_clock::universal_day() << endl;// 2019-Mar-13

    date dd1(pos_infin);        // 表示正无限
    date dd2(neg_infin);        // 表示负无线
    date dd3(not_a_date_time);    // 无效时间
    date dd4(min_date_time);    // 可表示的最小日期活时间
    date dd5(max_date_time);    // 可表示的最大日期活时间
    cout << dd1 << endl;// +infinity
    cout << dd2 << endl;// -infinity
    cout << dd3 << endl;// not-a-date-time
    cout << dd4 << endl;// 1400-Jan-01
    cout << dd5 << endl;// 9999-Dec-31

    // 异常
    try
    {
        date bd(1499, 12, 32);
    }
    catch (std::exception& e)
    {
        cout << e.what() << endl;// Day of month value is out of range 1..31
    }

    // 访问成员
    cout << d2 << endl;// 2019-Mar-13
    assert(d2.year() == 2019);assert(d2.month() == 3);assert(d2.day() == 13);
    // 返回date::ymd_type类型
    date::ymd_type ymd = d2.year_month_day();
    assert(ymd.year == 2019);assert(ymd.month == 3);assert(ymd.day == 13);
    
    assert(d2.day_of_week() == 3);    // 星期数,0为周日,1周一
    assert(d2.day_of_year() == 72);    // 当年的第几天
    assert(d2.week_number() == 11);    // 这天在当年的第几周里[0,53]
    
    // date转字符串
    cout << to_simple_string(d2) << endl;        // 2019-Mar-13    YYYY-mmm-DD格式(mmm为3字符英文),cout默认形式
    cout << to_iso_string(d2) << endl;            // 20190313        YYYYMMDD格式
    cout << to_iso_extended_string(d2) << endl;    // 2019-03-13    YYYY--MM-DD格式
    
    // date to tm(tm_hour、tm_min、tm_sec置为0,夏时令标志tm_isdst置为-1)
    tm t = to_tm(d2);
    cout << t.tm_year + 1900 << "-" << t.tm_mon + 1 << "-" << t.tm_mday << endl;// 2019-3-13
    // tm to date(tm_year、tm_mon、tm_mday有效)
    date d8 = date_from_tm(t);
    assert(d2 == d8);
    // date to time_t(由于时间只有年月日,所以需要date转换为ptime(时间点),再调用to_time_t())

    // 计量单位
    // (date_duration)  typedef:days 天:对象之间不能 */操作
    days day1(10), day2(-100), day3(1255);
    assert(day1 > day2); assert(day1 + day2 == days(-90)); assert((day1 + day3).days() == 1265); assert((day3 / 5) == days(251));
    
    // weeks 周:以7为单位
    weeks w(12);        // 12周
    w -= weeks(5);        // 7周
    assert(w.days() == 49);    

    // months 月
    months m(10);
    m += months(3);
    assert(m.number_of_months() == 13);

    // years 年:以12为单位
    years y(3);    // 3年
    y *= 3;        // 9年
    assert(y.number_of_years() == 9);

    m += y;    // 10年又1月
    assert(m.number_of_months() == 121);

    // date运算
    {
        date ddd1(2019, 2, 20);
        date ddd2(2019, 2, 25);
        cout << ddd2 - ddd1 << endl;// 两天的时间差 5
        cout << ddd1 - ddd2 << endl;// 两天的时间差 -5

        ddd1 += days(5);assert(ddd1 == ddd2);//(2019, 2, 25)

        ddd1 += weeks(2);assert(ddd1 == date(2019, 03, 11));

        ddd1 -= months(2);assert(ddd1 == date(2019, 01, 11));

        ddd1 -= years(50);assert(ddd1 == date(1969, 01, 11));

        // 特殊月份2月(2019年2月28天)
        date ddd3(2019, 3, 30);
        ddd3 -= months(1);assert(ddd3 == date(2019, 02, 28));    // 转到月末
        ddd3 -= months(1); assert(ddd3 == date(2019, 01, 31));
        ddd3 += months(1); assert(ddd3 == date(2019, 02, 28));
        date ddd4 = ddd3 - days(1);// (2019, 02, 27)
        ddd3 += months(1); assert(ddd3 == date(2019, 03, 31));
        ddd4 += months(1); assert(ddd4 == date(2019, 03, 27));
    }


    // 日期区间 date_period [,) 左闭右开
    date_period dp1(date(2019, 3, 13), days(10));
    assert(!dp1.is_null());
    assert(dp1.begin().day() == 13);// .begin()返回区间首date
    assert(dp1.last().day() == 22);
    assert(dp1.length() == days(10) && dp1.length().days() == 10);// .length()返回days
    cout << dp1 << endl;// [YYYY-mmm-DD/YYYY-mmm-DD]格式,左闭右闭 [2019-Mar-13/2019-Mar-22]
    // 区间变动
    dp1.shift(days(5));// 向后延5天
    cout << dp1 << endl;// [2019-Mar-18/2019-Mar-27]
    // 负号'-'写在days前面也是可以的
    dp1.shift(days(-5));// 向前进5天 
    cout << dp1 << endl;// [2019-Mar-13/2019-Mar-22]
    dp1.shift(weeks(-2));// 向前进14天
    cout << dp1 << endl;// [2019-Feb-27/2019-Mar-08]

    dp1.expand(days(10));// 向两边延伸10天
    cout << dp1 << endl;// [2019-Feb-17/2019-Mar-18]
    dp1.expand(days(-5));// 向两边缩短5天
    cout << dp1 << endl;// [2019-Feb-22/2019-Mar-13]

    // 成员函数
    /*
    .is_before(date);// 区间是否在date前
    .is_after(date);    // 区间是否在date后
    .contains(date / date_period); // 区间是否包含另一个区间或日期
    .intersects(date_period);// 两区间是否存在交集
    .intersection(date_period);// 返回两区间的交集,不存在返回无效区间,date_period.is_null()判断
    .is_adjacent();// 两个区间相邻[2019,1,11,2019,1,20] 与 [2019,1,21,2019,1,25] 相邻
    
    .merge(date_period);// 返回两区间的并集,区间无效或不相邻则返回无效区间
    .span(date_period);    // 合并两区间及之间的间隔
    */
    
    // 日期迭代器
    day_iterator d_iter(d2);//2019-03-13 默认步长为1天    
    ++ d_iter;// d2还是不变
    assert(d_iter == date(2019, 3, 14) && d2 == date(2019, 3, 13));
    year_iterator y_iter(*d_iter, 2);// 从3-14天开始,步长为2年
    ++y_iter;
    assert(y_iter == date(2021, 3, 14) && d_iter == date(2019, 3, 14));

    // 平、闰年判断
    typedef gregorian_calendar gre_cal;
    int year = 2019;
    cout << year << "年是"; (gre_cal::is_leap_year(year) ? (cout << "闰年" << endl) : (cout << "平年" << endl));
    int month = 2;
    cout << year << "年" << month << "月有" << gre_cal::end_of_month_day(2019,2) << "天" << endl;
    getchar();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值