boost date_time

简介:
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using namespace std;
using namespace boost::gregorian;


int _tmain(int argc, _TCHAR* argv[])
{	
	date d1;
	date d2(2012,12,12);
	date d3(2012, Jan, 1);
	date d4(d2);

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

	d1=from_string("2012-12-10");
	d2=(from_string("2012/12/10"));
	d3=from_undelimited_string("20121210");
	cout<<d1<<endl<<d2<<endl<<d3<<endl<<d4<<endl;

	cout<<day_clock::local_day()<<endl;
	cout<<day_clock::universal_day()<<endl;
	return 0;
}
image
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/static_assert.hpp>
using namespace std;
using namespace boost::gregorian;

int _tmain(int argc, _TCHAR* argv[])
{	
	date d(2012,3,26);
	assert(d.year()==2012);
	assert(d.month()==3);
	assert(d.day()==26);

	//一次性的获得年月日数据
	date::ymd_type ymd=d.year_month_day();
	assert(ymd.year==2012);
	assert(ymd.month==3);
	assert(ymd.day==26);
	
	cout<<"今天是"<<d.day_of_week()<<endl;  //输出date的星期数,0表示星期日
	cout<<"今天是今年的第"<<d.day_of_year()<<"天"<<endl;
	cout<<"本周是今年的第"<<d.week_number()<<"周"<<endl;
	cout<<"本月的最后一天是:"<<d.end_of_month()<<endl;

	//几种输出格式的比较
	cout<<to_simple_string(d)<<endl;
	cout<<to_iso_string(d)<<endl;
	cout<<to_iso_extended_string(d)<<endl;
	cout<<d<<endl;
	return 0;
}
image

 

日期长度:

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

int _tmain(int argc, _TCHAR* argv[])
{	
	days d1(100);
	days d2(-100);

	assert(d1+d2==days(0));
	assert((d1+d2).days()==0);

	weeks w(3);    //三个星期
	assert(w.days()==21);

	months m(5);  //5个月
	years y(2);  //2年

	months m2=y+m;   //2年零5个月
	assert(m2.number_of_months()==29);
	assert(y.number_of_years()*2==4);
	return 0;
}

一些日期的运算:

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

//一些日期的运算

int _tmain(int argc, _TCHAR* argv[])
{	
	date d1(2012,3,26);
	date d2(1991,2,10);
	cout<<"我已经出生了"<<d1-d2<<"天了"<<endl;

	d1+=days(10); 
	cout<<to_iso_extended_string(d1)<<endl;

	d1+=months(2);
	cout<<to_iso_extended_string(d1)<<endl;

	d1+=weeks(1);
	cout<<to_iso_extended_string(d1)<<endl;

	d1-=years(2);
	cout<<to_iso_extended_string(d1)<<endl;

	return 0;
}

image

 

日期区间:

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

//日期区间

int _tmain(int argc, _TCHAR* argv[])
{	
	date_period dp(date(2012,1,1),days(10));
	assert(!dp.is_null());
	assert(dp.begin().day()==1);
	assert(dp.last().day()==10);
	assert(dp.end().day()==11);
	assert(dp.length().days()==10);

	cout<<dp<<endl;
	return 0;
}
image

日期区间的运算

 

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

//日期区间的运算

int _tmain(int argc, _TCHAR* argv[])
{	
	date_period dp(date(2012,1,1),days(10));
	dp.shift(days(3));
	assert(dp.begin().day()==4);
	assert(dp.length().days()==10);

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

	assert(dp.is_after(date(2000,12,12)));
	assert(dp.is_before(date(2013,12,12)));
	assert(dp.contains(date(2012,1,2)));

	return 0;
}
目录
相关文章
|
12天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1273 5
|
2天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
11天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1290 87
|
12天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1825 13