利用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( ...
随机推荐
- VCS仿真 Dump Memory
VCS仿真 Dump Memory 两种方法 vcs联合verdi生成fsdb文件 vcs生成vpd文件 VCS联合verdi生成fsdb文件 1.testbench中加入如下语句: initial ...
- Linux目录和权限
1. rmdir -p 用来删除一串目录,是否可以成功删除? rmdir -p 删除一个不存在的目录时是否报错呢?rmdir -p 不能成功删除非空目录,rmdir -p 删除一个不存在的目录 ...
- 需求管理(REQM,Requirements Management)工具(转)
需求管理(REQM,Requirements Management)属于成熟度2级(受管理级)的过程域,是其他许多过程域实施的前提.对于暂未实施CMMI的企业,同样也可以借鉴CMMI的原则,实施和优化 ...
- rsync+inotify实时同步方案
rsync+inotify实时同步,inotify可以实时监控本地文件或目录变化,当检测到本地文件变化,执行rsync同步命令,将变化的文件同步到其他服务器节点. 1.配置环境 3.在服务节点1.服务 ...
- JAVA与JSON的序列化、反序列化
package com.linkage.app.memcache; import java.util.HashMap;import java.util.Map.Entry; import net.sf ...
- js的水仙花数的输出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- mysql对GIS空间数据的支持,包括创建空间索引
CREATE TABLE tb_geo( id INT PRIMARY KEY AUTO_INCREMENT, NAME ) NOT NULL, pnt POINT NOT NULL, SPATIAL ...
- 慕课linux学习笔记(五)常用命令(2)
链接命令 Ln [原文件] [目标文件] -s 表示创建软链接 硬链接特征: 拥有相同的i节点和存储block块,可以看做是同一个文件 通过i节点识别 不能跨分区 不能针对目录用 软链接特征: 不同的 ...
- 在js中使用json
在js中使用json var obj = { "1" : "value1", "2" : "value2" ...
- mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
从字面的意思上说:这个函数要被弃用,请使用 mysqlli 或者是 PDO 代替 然后就查手册发现没说,大家一定要查官方最新的手册