利用boost获取时间并格式化
利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。
1. 输出YYYYMMDD
- #include <boost/date_time/gregorian/gregorian.hpp>
- #define BOOST_DATE_TIME_SOURCE
- std::string strTime = boost::gregorian::to_iso_string(\
- boost::gregorian::day_clock::local_day());
- std::cout << strTime.c_str() << std::endl;
- #include <boost/date_time/gregorian/gregorian.hpp>
- #define BOOST_DATE_TIME_SOURCE
- std::string strTime = boost::gregorian::to_iso_string(\
- boost::gregorian::day_clock::local_day());
- std::cout << strTime.c_str() << std::endl;
2. 输出YYYYMMDD-HH:MM:SS
- #include <boost/date_time/posix_time/posix_time.hpp>
- #define BOOST_DATE_TIME_SOURCE
- std::string strTime = boost::posix_time::to_iso_string(\
- boost::posix_time::second_clock::local_time());
- // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
- int pos = strTime.find('T');
- strTime.replace(pos,1,std::string("-"));
- strTime.replace(pos + 3,0,std::string(":"));
- strTime.replace(pos + 6,0,std::string(":"));
- std::cout << strTime.c_str() << std::endl;
- #include <boost/date_time/posix_time/posix_time.hpp>
- #define BOOST_DATE_TIME_SOURCE
- std::string strTime = boost::posix_time::to_iso_string(\
- boost::posix_time::second_clock::local_time());
- // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
- int pos = strTime.find('T');
- strTime.replace(pos,1,std::string("-"));
- strTime.replace(pos + 3,0,std::string(":"));
- strTime.replace(pos + 6,0,std::string(":"));
- std::cout << strTime.c_str() << std::endl;
3. 计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/thread.hpp>
- #define BOOST_DATE_TIME_SOURCE
- boost::posix_time::ptime time_now,time_now1;
- boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
- // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
- time_now = boost::posix_time::microsec_clock::universal_time();
- // sleep 100毫秒;
- boost::this_thread::sleep(boost::posix_time::millisec(100));
- time_now1 = boost::posix_time::microsec_clock::universal_time();
- time_elapse = time_now1 - time_now;
- // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
- int ticks = time_elapse.ticks();
- // 得到两个时间间隔的秒数;
- int sec = time_elapse.total_seconds();
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/thread.hpp>
- #define BOOST_DATE_TIME_SOURCE
- boost::posix_time::ptime time_now,time_now1;
- boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
- // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
- time_now = boost::posix_time::microsec_clock::universal_time();
- // sleep 100毫秒;
- boost::this_thread::sleep(boost::posix_time::millisec(100));
- time_now1 = boost::posix_time::microsec_clock::universal_time();
- time_elapse = time_now1 - time_now;
- // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
- int ticks = time_elapse.ticks();
- // 得到两个时间间隔的秒
利用boost获取时间并格式化的更多相关文章
- js Date()获取时间,格式化输出,时间比较大小
1.获取时间并且格式化输出 new Date().toLocaleString('cn',{hour12:false}) //2018/12/6 17:57:15 new Date().toLocal ...
- 利用js获取时间并输出值
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式
要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
- 单位换算(格式化十进制数-B),获取时间工具类CommenUtil
package com.example.administrator.filemanager.utils;import java.text.DecimalFormat;import java.text. ...
- java 获取系统当前时间并格式化
java 获取系统当前时间并格式化 CreateTime--2018年5月9日11:41:00 Author:Marydon 实现方式有三种 updateTime--2018年7月23日09点32 ...
- JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss"
先来看下JS中的日期操作: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年 ...
- js 获取当前时间并格式化
js 获取当前时间并格式化 CreateTime--2018年2月7日11:04:16 Author:Marydon 方式一 /** * 获取系统当前时间并格式化 * @returns yyyy- ...
- thymeleaf获取当前时间并格式化输出
有时候会需要在模板中直接打印时间的需求,如果输出一个时间还需要在java类中去获取model的话,那未免也太麻烦了,以下为thymeleaf在模板中直接获取时间戳并格式化输的代码 获取时间戳 < ...
- js获取当前时间的年月日时分秒以及时间的格式化
1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...
随机推荐
- asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料
asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料 NET Pet Shop .NET Pet Shop是一个电子商务的实例, ...
- vi 快捷键【转】【weber整理必出精品】
光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...
- silverlight visifire控件图表制作——silverlight 后台方法画图
1.调用wcf 获取信息 private void svc_GetSingleChartDataCompleted(object sender, GetSingleChartDataCompleted ...
- mysql忘记密码的处理方式(整理非原创)
方案1.通过跳过授权的方式 1.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的中加上:skip-grant-tables . 2.重新启动mysqld # ubun ...
- BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...
- Stata和Matlab联合处理金融数据
Stata是统计学专业软件,可以很方便的对数据处理,但几乎只能按照整行整列进行,而且每次只能加载一个矩阵(dta文件),如果要用到多个矩阵数据进行操作或进行复杂的循环控制,就力不从心了. 而Matla ...
- 修改mysql的默认字符集
\s 查看mysql的字符集 把server characterset和db characterset修改成utf8 在my.ini的mysqld下面添加两行代码,重启mysql [mysql] de ...
- Go and JSON
Go and JSON 在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding ...
- scons构建自己的一个简单的程序
我在我的D盘下,新建一个文件夹,命名为try.在这个文件夹下新建两个文件,一个文件是test.c .里面的程序很简单: #include<stdio.h>#include<stdli ...
- sgu Ice-cream Tycoon
题意:供应商提供n块价格为c的冰淇淋,一个学生想买n块冰淇淋,手中的钱数总共有t元,为了不让买n块冰淇淋所花费的钱数不超过t元,先尽可能卖给这个学生便宜的冰淇淋. 如果这个学生不能买到所需要的冰淇淋则 ...