栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Boost日期时间(date

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Boost日期时间(date

目录

一、基本介绍

二、日期对象的创建

三、日期与字符串之间的转换 

四、日期运算

五、日期区间运用

六、日历方面应用 


一、基本介绍
  • date_time库包含两部分:处理日期的gregorian、处理时间的posix_time
  • 处理日期:#include
  • 处理时间:#include

二、日期对象的创建
#include 
#include 
#include 

int main(int argc, char **argv) {
	// date对象初始化
	boost::gregorian::date d1;									// 无效的日期
	boost::gregorian::date d2(2021, 11, 23);					// 数字构造的日期
	boost::gregorian::date d3(2021, boost::gregorian::Nov, 23);	// 英文指定月份,并不是输入字符串
	boost::gregorian::date d4(d2);
	
	// 当天日期
	d1 = boost::gregorian::day_clock().local_day();

	return 0;
}

三、日期与字符串之间的转换 
#include 
#include 
#include 

int main(int argc, char **argv) {
	// date对象初始化
	boost::gregorian::date d1;									// 无效的日期
	// 字符串 转 date
	d1 = boost::gregorian::from_string("2021-11-23");
	d1 = boost::gregorian::from_string("2021/11/23");
	d1 = boost::gregorian::from_undelimited_string("20211102");

	// date 转 字符串
	tm t = boost::gregorian::to_tm(d1);
	printf("to_tm: %4d-%02d-%02dn", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
	
	// d1.year()返回的值类型应该是无符号数值类型,比如unsigned long等,所以这里强制转换为int输出
	printf("date_func: %4d-%02d-%02dn", (int)d1.year(), (int)d1.month(), (int)d1.day());
	printf("Day Of Week: %dn", (int)d1.day_of_week());
	printf("Day Of Year: %dn", (int)d1.day_of_year());
	
	// 使用内置方法转换字符串
	printf("to_simple_string: %sn", boost::gregorian::to_simple_string(d1).c_str());
	printf("to_iso_string: %sn", boost::gregorian::to_iso_string(d1).c_str());
	printf("to_iso_extended_string: %sn", boost::gregorian::to_iso_extended_string(d1).c_str());

	return 0;
}

运行结果:

四、日期运算
#include 
#include 
#include 

int main(int argc, char **argv) {
	boost::gregorian::date d1(2021, 1, 1), d2(2021, 11, 23);

	printf("当前日期: %sn", boost::gregorian::to_iso_extended_string(d2).c_str());
	printf("与[%s]相隔天数: %ldn", boost::gregorian::to_iso_extended_string(d1).c_str(), (d2 - d1).days());
	
	boost::gregorian::date dt = d2 + boost::gregorian::days(10);
	printf("10天后日期是: %sn", boost::gregorian::to_iso_extended_string(dt).c_str());

	dt = d2 + boost::gregorian::months(2);
	printf("2个月后的日期是: %sn", boost::gregorian::to_iso_extended_string(dt).c_str());

	dt = d2 + boost::gregorian::weeks(2);
	printf("2周后的日期是: %sn", boost::gregorian::to_iso_extended_string(dt).c_str());

	dt = d2 + boost::gregorian::years(2);
	printf("2年后的日期是: %sn", boost::gregorian::to_iso_extended_string(dt).c_str());

	return 0;
}

运行结果:

 

五、日期区间运用
#include 
#include 
#include 

int main(int argc, char **argv) {
	boost::gregorian::date_period dp1(boost::gregorian::date(2021, 11, 01), boost::gregorian::days(7));
	boost::gregorian::date_period dp2(boost::gregorian::date(2021, 11, 01), boost::gregorian::date(2021, 11, 7));

	printf("遍历dp1>>>n");
	boost::gregorian::date dt = dp1.begin();
	while (dt < dp1.end()) {
		printf("%sn", boost::gregorian::to_iso_extended_string(dt).c_str());
		dt += boost::gregorian::days(1);
	}

	printf("n遍历dp2>>>n");
	dt = dp2.begin();
	while (dt < dp2.end()) {
		printf("%sn", boost::gregorian::to_iso_extended_string(dt).c_str());
		dt += boost::gregorian::days(1);
	}

	printf("n通过日期迭代器来遍历日期>>>n");
	dt = boost::gregorian::date(2021, 11, 1);
	boost::gregorian::day_iterator d_iter(dt, 2);	// 可以设置递增天数
	while (d_iter < boost::gregorian::date(2021, 11, 16)) {
		printf("%sn", boost::gregorian::to_iso_extended_string(*d_iter).c_str());
		++d_iter;
	}
	// 另外可以按周(week_iterator)、月(month_iterator)、年(year_iterator)进行迭代
	return 0;
}

运行结果:

六、日历方面应用 

计算指定年份是否是闰年,指定年月的最后一天是几号等等

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/587999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号