BOOST 日期时间库 之 date 1/3

来源:互联网 发布:医疗器械软件分类 编辑:程序博客网 时间:2024/06/06 11:14
1.摘要:
基于格里高利历,支持1400-01-01到9999-12-31之间的日期计算.
使用一个32位的整数作为内部存储,以天为单位表示时间点概念.


2.包含头文件
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;


3.构造
boost::gregorian::date d1( 2010, 1, 1 );
boost::gregorian::date d2( 2000, boost::gregorian::Jan, 1 );
boost::gregorian::date d3( d2 );
boost::gregorian::date d4; //空的构造会创建一个 not_a_date_time的无效日期;


4、比较操作
assert( d2 == d3 );
assert( d1 > d2 );
assert( d4 == boost::gregorian::date( boost::gregorian::not_a_date_time) );


5、使用字符串赋值
//from_string->使用分隔符(斜杠或连字符)分割年月日格式的字符串
boost::gregorian::date d51 = boost::gregorian::from_string( "1988-12-31" );
boost::gregorian::date d52( boost::gregorian::from_string("2008/1/1") );


//from_undelimited_string->使用无分隔符的纯字符串格式
boost::gregorian::date d53 = boost::gregorian::from_undelimited_string( "20110508" );


6;、时钟day_clock
boost::gregorian::day_clock::local_day(); //获得本地日期(date类型),依赖于系统的时区设置
boost::gregorian::day_clock::universal_day();  //UTC日期(date类型)

7、日期转为字符串
boost::gregorian::date date7( 2008, 11, 20 );


//to_simple_string->转为YYYY-mmm-DD格式,其中mmm为三个字符的英文月份名
//输出结果:2008-Nov-20
std::cout << boost::gregorian::to_simple_string( date7 ) << std::endl;


//to_iso_string->转为YYYYMMDD格式
//输出结果:20081120
std::cout << boost::gregorian::to_iso_string( date7 ) << std::endl;


//to_iso_extended_string->转为YYYY-MM-DD格式
//输出结果:2008-11-20
std::cout << boost::gregorian::to_iso_extended_string( date7 ) << std::endl;