首页 > 代码库 > boost日期用法
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 = http://www.mamicode.com/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 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 = http://www.mamicode.com/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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。