boost 日期时间计算
示例代码如下:
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::gregorian;
using namespace boost::posix_time;
#include <iostream>
using namespace std;
#include <string> void TimerTest()
{
{
// 例1. 日期计算:打印今天日期;今年感恩节(11月的第4个星期四)的日期;如果还没有到今年感恩节,打印今天到感恩节还有多少天;
typedef nth_day_of_the_week_in_month nth_dow;
date today = day_clock::local_day(); // today; // 获取今天日期;
std::cout << "today is: " << today << std::endl;
nth_dow fourth_thur_in_nov(nth_dow::fourth, Thursday, Nov); // 4th thursday in Nov;
date thanksgiving = fourth_thur_in_nov.get_date(today.year()); // get the date this year;
std::cout << "Thanksgiving day this year is: " << thanksgiving << std::endl;
if(today < thanksgiving)
{
date_duration dd = thanksgiving - today; // date duration; // 时间差;
std::cout << "has " << dd.days() << " days to thanksgiving."<< std::endl;
}
} {
// 日期操作;
std::cout << std::endl << std::endl;
date today = day_clock::local_day(); // today; // 获取今天日期;
std::cout << today.year() << std::endl; // 打印年;
std::cout << today.month() << std::endl; // 打印月;
std::cout << today.day() << std::endl; // 打印日;
std::cout << today.week_number() << std::endl; // 今年的第几周;
std::cout << today.day_of_week() << std::endl; // 打印星期几;
std::cout << today.day_of_year() << std::endl; // 打印一年中的第几天;
std::cout << today.end_of_month() << std::endl; // 打印本月的最后一天是,闰月的2月,可以试试;
std::cout << today.modjulian_day() << std::endl;
std::cout << today.julian_day() << std::endl;
std::cout << (today.day_of_week() == ) << std::endl; // 判断今天是星期日吗,周日为0;
std::cout << (today.end_of_month() == today) << std::endl; // 判断今天是当月的最后一天吗;
date dateTemp = from_string("2016-11-26");
date_duration dd = dateTemp - today; // 时间差;
std::cout << "subDate = " << dd.days() << std::endl; // 相差多少天;
// 获取指定(11月的第4个星期四)日期;
typedef nth_day_of_the_week_in_month nth_dow;
nth_dow fourth_thur_in_nov(nth_dow::fourth, Thursday, Nov); // 4th thursday in Nov;
date thanksgiving = fourth_thur_in_nov.get_date(today.year()); // get the date this year;
} {
// 时间操作;
ptime timeTemp = second_clock::local_time(); // 获取当前时间,秒级别;
std::cout << timeTemp.time_of_day() << std::endl; // 当前时间;
std::cout << timeTemp.date() << std::endl; // 当前时间对应的日期;
ptime destTime = time_from_string("2016-06-15 17:00:00");
time_duration tt = timeTemp - destTime; // 时间差;
std::cout << "subTime = " << (tt.seconds() >= ) << std::endl; // 相差多少秒;
}
}
boost 日期时间计算的更多相关文章
- js日期时间计算天数
var stime = new Date('2016-03-14'); //开始时间 var etime = new Date('2016-03-20'); //结束时间 var times = et ...
- mysql与oracle的日期/时间函数小结
前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...
- MySQL 日期时间函数
目录 datetime和timestamp区别: timestamp类型字段特殊性: Mysql获取日期时间函数: now() curdate() curtime() Extract() last_d ...
- mysql日期时间类型总结
MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 ------------ ---- ...
- MySQL 日期时间 专题
1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp() curr ...
- Java 之 JDK1.8之前日期时间类
一.JDK1.8之前日期时间类 二. java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1 ...
- MySQL日期时间函数大全
1.获取当前时间+日期 函数now() mysql> select now(); +---------------------+ | now() | +--------------------- ...
- 11、Java 日期时间 日期工具类
一.简介 在Java8之前,日期时间API一直被开发者诟病,包括:java.util.Date是可变类型,SimpleDateFormat非线程安全等问题.故此,Java8引入了一套全新的日期时间处理 ...
- [Boost]boost的时间和日期处理-(2)时间的操作
<开篇> 本篇紧接着boost上篇叙述Boost::DateTime的时间处理.在C++中,常见的时间有time_t, FILETIME和tm,而boost中用ptime. 构造ptime ...
随机推荐
- Android开发 View的UI刷新Invalidate和postInvalidate
Invalidate 正常刷新 /** * 使整个视图无效.如果视图可见, * {@link #onDraw(android.graphics.Canvas)} 调用此方法后将在后续的UI刷新里调用o ...
- Android开发 ImageView开发记录
改变图片的着色 默认是这个方法 /** * 为图像设置着色选项. Assumes * {@link PorterDuff.Mode#SRC_ATOP} blending mode. * * @para ...
- What is the difference between HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR
What is the difference between HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR? it is impossible to say. Dif ...
- .net下MVC中使用Tuple分页查询数据
主要是在DAL层写查询分页的代码. 例如DAL层上代码: public Tuple<List<WxBindDto>, int> GetMbersInfo(int start, ...
- excel破解工作簿与工作表保护
1.工作簿保护 1.1.使用压缩文件打开文件
- 玩转gulp之gulp编译less
用好gulp grunt webpack让前端编程走向自动化,是作为一个前端开发必须学会的技能,不然逼格怎么提升的上去呢... 然后教大家如何用gulp装逼.一点点的学,都是相通的嘛 1. 安装nod ...
- Android问题集锦An error occurred while automatically activating bundle com.android.ide.eclipse.adt .
这是Eclipse的问题,重启之后问题依旧. 在坛子里找到这个办法,果然好用. 命令行到eclipse路径运行:eclipse.exe -clean
- icon 的前生今世 & iconfont 的晋级之路
布吉岛为啥起了个这么文(dou)艺(bi)的名字,话不多说,开始总结
- Java 面试题经典 77 问(含答案)!
金三银四了,3月底,4月初,找工作换单位的黄金时期.4月初将会有有一大批职场人士流动... 作为Java开发码农的你是不是也在蠢蠢欲动,或者已经搞了几轮车轮战了? 我们为大家准备了 77 道经典 Ja ...
- ASP.Net调整允许上传文件的大小
1.用户上传视频文件注意:调整允许上传文件的大小:ASP.Net为了防止过大的http恶意请求阻塞网站,所以限定了每次上传文件最大4M,asp.net1.1中把用户上传的文件先放到内存中,2.0后如果 ...