BOOST 日期时间库 之 date 1/3

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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值