码迷,mamicode.com
首页 > 其他好文 > 详细

boost日期用法

时间:2014-05-11 03:46:57      阅读:814      评论:0      收藏:0      [点我收藏+]

标签:boost   日期   

// boost2.cpp : 定义控制台应用程序的入口点。
//boost gregorian_date usage
//made by davidsu33 2014-5-10


#include "stdafx.h"
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian_calendar.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>


#include <iostream>
#include <cassert>


using namespace std;


void date_calc()
{
cout<<"date_calc"<<endl;


//日历计算
typedef boost::gregorian::gregorian_calendar GregorianCalendar;
GregorianCalendar grecal;


boost::gregorian::date d(2000, 2, 1);
boost::gregorian::date d2(2014, 5, 10);


boost::gregorian::days ddays = d2-d;
assert( (d + ddays) == d2);


//格林威治时间的年、月、日
//greg_year
//greg_month
//greg_day
boost::gregorian::greg_year dy = d.year();
bool leapYear = grecal.is_leap_year(dy);
cout<<"dy="<<dy<<endl;


boost::gregorian::greg_month dm = d.month();
cout<<"dm="<<dm<<endl;


boost::gregorian::greg_day dd = d.day();
cout<<"dd="<<dd<<endl;


boost::gregorian::date d3 = 
d + boost::gregorian::months(1);
int theDays = (leapYear)?29:28;
boost::gregorian::days d3days =
d3 - d;


int d3value = d3days.days();
assert(theDays == d3value);


std::cout<<d2-d<<std::endl;


//月底日期计算
cout<<"end of month 2010-2 is:"<<grecal.end_of_month_day(2010, 2)<<endl;


cout<<"-----------------------"<<endl;
}


//day_iterator 日迭代器
//month_iterator 月迭代器
//year_iterator 年迭代器


void date_iterator()
{
cout<<"date_iterator"<<endl;


boost::gregorian::date d(2014, 5, 10);
boost::gregorian::day_iterator di(d);
++di;


assert( (*di) == boost::gregorian::date(2014, 5, 11));
cout<<"d="<<d<<endl;
cout<<"*di="<<*di<<endl;


boost::gregorian::month_iterator mi(d);
++mi;


assert( (*mi) == boost::gregorian::date(2014, 6, 10));
cout<<"d="<<d<<endl;
cout<<"*yi="<<*mi<<endl;


boost::gregorian::year_iterator yi(d);
++yi;

assert( (*yi) == boost::gregorian::date(2015, 5, 10));


cout<<"--------------------------"<<endl;
}


void date_period()
{
cout<<"date_period"<<endl;


using namespace boost::gregorian;
boost::gregorian::date_period dp(date(2014, 1, 16), date(2014, 5, 10));

int olddays = dp.length().days();


cout<<"dp="<<dp<<endl;
cout<<"dp_days="<<dp.length().days()<<endl;
cout<<"contains(date(2014, 1, 15))="<<dp.contains(date(2014, 1, 15))<<endl;
cout<<"contains(date(2014, 2, 15))="<<dp.contains(date(2014, 2, 15))<<endl;

boost::gregorian::date i = dp.begin(), iend = dp.end();
boost::gregorian::date_period olddp = dp;
for (; i != iend; )
{
cout<<"i="<<i<<endl;
dp.shift(date_duration(1)); //将开始和结束时间进行平行移动,其时间间隔不变
i = dp.begin();
}


int dy = dp.length().days();
assert(olddays == dy);


boost::gregorian::days spandays = dp.end() - dp.begin();
int span = spandays.days();
assert(span == olddays);


cout<<"dp.begin()="<<dp.begin()<<endl;
cout<<"dp.end()="<<dp.end()<<endl;


cout<<"--------------------------------"<<endl;
}


void show_month_calendar()
{
boost::gregorian::date d(2014, 5, 1);
boost::gregorian::day_iterator i(d);
  boost::gregorian::date iend =  d.end_of_month(); //the end of the month


boost::gregorian::greg_weekday wd = d.day_of_week();
cout<<"d.day_of_week()="<<d.day_of_week()<<endl;
cout<<"wd.as_number()="<<wd.as_number()<<endl;

cout<<endl;
for (; *i != iend; ++i)
{
cout<<(*i).day_of_week()<<" ";
if ((*i).day_of_week().as_number() == 0)
{
cout<<"\n";
}
}
}


int _tmain(int argc, _TCHAR* argv[])
{
// date_calc();
// date_iterator();
// date_period();
show_month_calendar();


getchar();
return 0;
}

boost日期用法,布布扣,bubuko.com

boost日期用法

标签:boost   日期   

原文地址:http://blog.csdn.net/davidsu33/article/details/25510405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!