boost初探-日期与时间_day of month value is out of range linux系统boost(2)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

using namespace boost::gregorian;

//

void case1()
{
date d1;
date d2(2010,1,1);
date d3(2000, Jan , 1);
date d4(d2);

assert(d1 == date(not_a_date_time));
assert(d2 == d4);
assert(d3 <  d4);

}

//

void case2()
{
date d1 = from_string(“1999-12-31”);
date d2 ( from_string(“2015/1/1”) );
date d3 = from_undelimited_string(“20011118”) ;

cout << d1 << d2 << d3 << endl;

cout << day_clock::local\_day()    << endl;
cout << day_clock::universal\_day() << endl;

}

//
void case3()
{
date d1(neg_infin);
date d2(pos_infin);
date d3(not_a_date_time);
date d4(max_date_time);
date d5(min_date_time);

cout << d1 << d2 << d3 << d4 << d5 << endl;

try
{
    //date d1(1399,12,1);
    //date d2(10000,1,1);
    date d3(2017,2,29);
}
catch(std::exception& e)
{
    cout << e.what() << endl;
}

}

//
void case4()
{
date d(2017,6,1);
assert(d.year() == 2017);
assert(d.month() == 6);
assert(d.day() == 1);

date::ymd_type ymd =  d.year\_month\_day();
assert(ymd.year    == 2017);
assert(ymd.month   == 6);
assert(ymd.day     == 1);

cout << d.day\_of\_week() << endl;
cout << d.day\_of\_year() << endl;
assert(d.end\_of\_month() == date(2017,6,30));

cout << date(2015,1,10).week\_number() << endl;
cout << date(2016,1,10).week\_number()  << endl;
cout << date(2017,1,10).week\_number()  << endl;

assert(date(pos_infin).is\_infinity()  );
assert(date(pos_infin).is\_pos\_infinity() );
assert(date(neg_infin).is\_neg\_infinity() );
assert(date(not_a_date_time).is\_not\_a\_date() );
assert(date(not_a_date_time).is\_special() );
assert(!date(2017,5,31).is\_special() );

}

//
void case5()
{
date d(2017,1,23);

cout << to\_simple\_string(d) << endl;
cout << to\_iso\_string(d) << endl;
cout << to\_iso\_extended\_string(d) << endl;
cout << d << endl;

//cout << "input date:";
//cin >>d;
//cout << d;

}

//
void case6()
{
date d(2017,5,20);
tm t = to_tm(d);
assert(t.tm_hour == 0 && t.tm_min == 0);
assert(t.tm_year == 117 && t.tm_mday == 20);

date d2 = date\_from\_tm(t);
assert(d == d2);

}

//
void case7()
{
days dd1(10), dd2(-100), dd3(255);

assert( dd1 > dd2 && dd1 < dd3);
assert( dd1 + dd2 == days(-90));
assert((dd1 + dd3).days() == 265);
assert( dd3 / 5 == days(51));

weeks w(3);
assert(w.days() == 21);

months m(5);
years y(2);

months m2 = y + m;
assert(m2.number\_of\_months() == 29);
assert((y \* 2).number\_of\_years() == 4);

}

//
void case8()
{
date d1(2000,1,1),d2(2017,11,18);
cout << d2 - d1 << endl;
assert(d1 + (d2 - d1) == d2);

d1 += days(10);
assert(d1.day() == 11);
d1 += months(2);
assert(d1.month() == 3 && d1.day() == 11);
d1 -= weeks(1);
assert(d1.day() == 4);

d2 -= years(10);
assert(d2.year() == d1.year() + 7);

{
    date d1(2017,1,1);

    date d2 = d1 + days(pos_infin);
    assert(d2.is\_pos\_infinity());

    d2 = d1 + days(not_a_date_time);
    assert(d2.is\_not\_a\_date());
    d2 = date(neg_infin);
    days dd = d1 - d2;
    assert(dd.is\_special() && !dd.is\_negative());
}

{
    date d(2017,3,30);
    d -= months(1);
    d -= months(1);
    d += months(2);
    assert(d.day() == 31);
}

}

//
void case9()
{
date_period dp1(date(2017,1,1), days(20));
date_period dp2(date(2017,1,1), date(2016,1,1));
date_period dp3(date(2017,3,1), days(-20));

date_period dp(date(2017,1,1), days(20));

assert(!dp.is\_null());
assert(dp.begin().day() == 1);
assert(dp.last().day() == 20);
assert(dp.end().day() == 21);
assert(dp.length().days() == 20);

{
    date_period dp1(date(2017,1,1), days(20));
    date_period dp2(date(2017,2,19), days(10));

    cout << dp1;                        //[2010-Jan-01/2010-Jan-20]
    assert(dp1 < dp2);
}

}

//

int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
case9();
}


##### 3.2date\_period


data\_time库使用date\_period来表示日期区间的概念,它是时间轴上的一个左闭右开的区间,其端点是两个date对象。日期区间的左边界必须小于右边界,否则date\_period将表示一个无效的日期区间。


date\_period的类摘要如下:



class date_period
{
public:
period(date, date);
period(date, days);

date begin() const;
date end() const;
date last() const;
days length() const;
bool is\_null() const;

bool operator==(const period &) const;
bool operator<(const period &) const;

void shift(const days &);
void expand(const days &);

bool contains(const date &) const;
bool contains(const period &) const;
bool intersects(const period &) const;
bool is\_adjacent(const period &) const;
bool is\_before(const date &) const;
bool is\_after(const date  &) const;
period intersection(const date &) const;
period merge(const period &) const;
period span(const period &) const;

};


用一段代码来测试一下



#include
using namespace std;

#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;

//
void case1()
{
date_period dp(date(2017,1,1), days(20));

dp.shift(days(3));
assert(dp.begin().day() == 4);
assert(dp.length().days() == 20);

dp.expand(days(3));
assert(dp.begin().day() == 1);
assert(dp.length().days() == 26);

}

//

void case2()
{
date_period dp(date(2010,1,1), days(20));

assert(dp.is\_after(date(2009,12,1)));
assert(dp.is\_before(date(2010,2,1)));
assert(dp.contains(date(2010,1,10)));

date_period dp2(date(2010,1,5), days(10));
assert(dp.contains(dp2));

assert(dp.intersects(dp2));
assert(dp.intersection(dp2) == dp2);

date_period dp3(date(2010,1,21), days(5));
assert(!dp3.intersects(dp2));
assert(dp3.intersection(dp2).is\_null());

assert(dp.is\_adjacent(dp3));
assert(!dp.intersects(dp3));

}

//
void case3()
{
date_period dp1(date(2010,1,1), days(20));
date_period dp2(date(2010,1,5), days(10));
date_period dp3(date(2010,2,1), days(5));
date_period dp4(date(2010,1,15), days(10));

assert( dp1.contains(dp2) && dp1.merge(dp2) == dp1);
assert(!dp1.intersects(dp3) && dp1.merge(dp3).is\_null());
assert( dp1.intersects(dp2) && dp1.merge(dp4).end() == dp4.end());
assert( dp1.span(dp3).end() == dp3.end());

}

//
void case4()
{
date d(2007,9,28);
day_iterator d_iter(d);

assert(d_iter == d);
++d_iter;
assert(d_iter == date(2007,9,29));

year_iterator y\_iter(\*d_iter, 10);
assert(y_iter == d + days(1));
++y_iter;
assert(y_iter->year() == 2017);

day_iterator iter(day_clock::local\_day());
++iter;

//iter += 5;
//std::advance(iter, 5);

}

//
void case5()
{
typedef gregorian_calendar gre_cal;
cout << “Y2017 is "
<< (gre_cal::is_leap_year(2017)?”“:“not”)
<< " a leap year.” << endl;
assert(gre_cal::end_of_month_day(2017, 2) == 28);
}

//
void case6()
{
date d(2017,1,23);

date d\_start(d.year(), d.month(), 1);
date d_end = d.end\_of\_month();

for(day_iterator d\_iter(d_start);
    d_iter <= d_end; ++d_iter)
{
        cout << \*d_iter << " " <<
                d_iter->day\_of\_week()<< endl;
}

}

//
void case7()
{
date d(2017,1,23);

date d18years = d + years(18);
cout << d18years << " is "
    << d18years.day\_of\_week()<< endl;

int count = 0;
for (day_iterator d\_iter(date(d18years.year(),1,1));
        d_iter <= d18years.end\_of\_month(); ++d_iter)
{
    if (d_iter->day\_of\_week() == Sunday)
    {
        ++count;
    }
}
cout << "total " << count << " Sundays." << endl;

count = 0;
for (month_iterator m\_iter(date(d18years.year(),1,1));
        m_iter < date(d18years.year() + 1 ,1, 1); ++m_iter)
{
    count += m_iter->end\_of\_month().day();
}
cout << "total " << count << " days of year." << endl;

}

//
class credit_card
{
public:
string bank_name;
int bill_day_no;

credit\_card(const char\* bname, int no):
    bank\_name(bname), bill\_day\_no(no){}

int calc\_free\_days(date consume_day = day_clock::local\_day()) const
{
    date bill\_day(consume_day.year(), consume_day.month(), bill_day_no);
    if (consume_day > bill_day)
    {
        bill_day += months(1);
    }

    return (bill_day - consume_day).days() + 20;
}

friend bool operator<(const credit_card& l, const credit_card& r)
{

    return l.calc\_free\_days() < r.calc\_free\_days();
}

};

void case8()
{
credit_card a(“A bank”, 25);
credit_card b(“B bank”, 12);

credit_card tmp = std::max(a, b);
cout << "You should use " << tmp.bank_name 
    << ", free days = " << tmp.calc\_free\_days() << endl;

}

int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
}


#### 4.posix\_time


date\_time库的时间功能位于命名空间boost::posix\_time,需要包含的头文件如下



#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;


从概念上来说,时间是日期的进一步细化,相当于在日期”天“的量级之下增加了”时分秒“的概念


直接上测试代码



#include
using namespace std;

//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG

#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;

//

void case1()
{
{
time_duration td = duration_from_string(“1:10:30:001”);
cout << td << endl;

    time_duration td1(1,10,30,1000);
    time_duration td2(1,60,60,1000\*1000\* 6 + 1000);
}

hours h(1);
minutes m(10);
seconds s(30);
millisec ms(1);

time_duration td = h + m + s + ms;
time_duration td2 = hours(2) + seconds(10);

cout << td << td2 << endl;

}

//
void case2()
{
time_duration td(1,10,30,1000);
assert(td.hours() == 1 && td.minutes() == 10 && td.seconds() == 30);
assert(td.total_seconds() == 1*3600+ 10*60 + 30);

#ifndef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
assert(td.total_milliseconds() == td.total_seconds()*1000 + 1);
assert(td.fractional_seconds() == 1000);
#endif

hours h(-10);
assert(h.is\_negative());

time_duration h2 = h.invert\_sign();
assert(!h2.is\_negative() && h2.hours() == 10);

time_duration td1(not_a_date_time);
assert(td1.is\_special() && td1.is\_not\_a\_date\_time());

time_duration td2(neg_infin);
assert(td2.is\_negative() && td2.is\_neg\_infinity());

}

//
void case3()
{
time_duration td1 = hours(1);
time_duration td2 = hours(2) + minutes(30);
assert(td1 < td2);
assert((td1+td2).hours() == 3);
assert((td1-td2).is_negative());
assert(td1 * 5 == td2 * 2);
assert((td1/2).minutes() == td2.minutes());

time_duration td(1,10,30,1000);
cout << to\_simple\_string(td) << endl;
cout << to\_iso\_string(td) << endl;

}

//
void case4()
{
#ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
time_duration td(1,10,30,1000);
cout << td;
assert(td.total_milliseconds() ==
td.total_seconds()*1000);

assert(td.fractional\_seconds() ==1000);
assert(time_duration::unit()\*1000\*1000\*1000 == seconds(1));

assert(td.resolution() == boost::date_time::nano);
assert(td.num\_fractional\_digits() == 9);

#endif
}

//
void case5()
{
ptime p(date(2017,7,7), hours(1));
ptime p1 = time_from_string(“2017-7-7 01:00:00”);
ptime p2 = from_iso_string(“20170707T010000”);

cout << p1 << endl << p2;
{
    ptime p1 = second_clock::local\_time();
    ptime p2 = microsec_clock::universal\_time();
    cout << p1 << endl << p2;

}

}

//
void case6()
{
ptime p(date(2010,3,20), hours(12)+minutes(30));

date d = p.date();
time_duration td = p.time\_of\_day();
assert(d.month() == 3 && d.day() == 20);
assert(td.total\_seconds() == 12\*3600 + 30\*60);

ptime p1(date(2010,3,20), hours(12)+minutes(30));
ptime p2 = p1 + hours(3);

assert(p1 < p2);
assert(p2 - p1 == hours(3));
p2 += months(1);
assert(p2.date().month() == 4);

cout << endl;
{
    ptime p(date(2017,2,14), hours(20));
    cout << to\_simple\_string(p) << endl;
    cout << to\_iso\_string(p) << endl;
    cout << to\_iso\_extended\_string(p) << endl;
}

}

//
void case7()
{
ptime p(date(2017,5,20), hours(14));
tm t = to_tm§;
assert(t.tm_year == 117 && t.tm_hour == 14);
assert(ptime_from_tm(t) == p);

ptime p2 = from\_time\_t(std::time(0));
assert(p2.date() == day_clock::local\_day());
cout << to\_time\_t(p2) << endl;

}

//
void case8()
{
ptime p(date(2017,1,1),hours(12)) ;
time_period tp1(p, hours(8));
time_period tp2(p + hours(8), hours(1));
assert(tp1.end() == tp2.begin() && tp1.is_adjacent(tp2));
assert(!tp1.intersects(tp2));

tp1.shift(hours(1));
assert(tp1.is\_after(p));
assert(tp1.intersects(tp2));

tp2.expand(hours(10));
assert(tp2.contains(p) && tp2.contains(tp1));

}

//
void case9()
{
ptime p(date(2017,5,31),hours(10)) ;
for (time_iterator t_iter(p, minutes(10));
t_iter < p + hours(1); ++ t_iter)
{
cout << *t_iter << endl;
}

}

//
template
class basic_ptimer
{
public:
basic_ptimer()
{ restart();}
void restart()
{ _start_time = Clock::local_time(); }
void elapsed() const
{ cout << Clock::local_time() - _start_time; }
~basic_ptimer()
{ elapsed(); }
private:
ptime _start_time;
};
typedef basic_ptimer<microsec_clock> ptimer;
typedef basic_ptimer<second_clock> sptimer;

class work_time
{
public:
typedef map<time_period, string> map_t;
private:
map_t map_ts;
void init()
{
ptime p(day_clock::local_day());

    map_ts[time\_period(p, hours(9))] = "It's too early, just relax.\n";
    p += hours(9);
    map_ts[time\_period(p, hours(3)+ minutes(30))] = "It's AM, please work hard.\n";
    p += hours(3)+ minutes(30);
    map_ts[time\_period(p, hours(1))] = "It's lunch time, are you hungry?\n";
    p += hours(1);
    map_ts[time\_period(p, hours(4)+minutes(30))] = "It's PM, ready to go home.\n";
    p += hours(4)+ minutes(30);

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

l_day());

    map_ts[time\_period(p, hours(9))] = "It's too early, just relax.\n";
    p += hours(9);
    map_ts[time\_period(p, hours(3)+ minutes(30))] = "It's AM, please work hard.\n";
    p += hours(3)+ minutes(30);
    map_ts[time\_period(p, hours(1))] = "It's lunch time, are you hungry?\n";
    p += hours(1);
    map_ts[time\_period(p, hours(4)+minutes(30))] = "It's PM, ready to go home.\n";
    p += hours(4)+ minutes(30);

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值