利用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( ...
随机推荐
- Linux查看系统状态及备份
1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数?cat /proc/cpuinfo将CPU的总核数除以物理CPU的个数,得到每颗CPU的核数.2. 查看系统负载有两个常用的命令,是哪 ...
- Linux 统计代码行数命令
wc -l `find . -name '*.js'` wc -l `find . -regex ".*\.js"`
- compass安装
修改ruby软件包的sources 国外服务器不给力,经常链接失败,换成国内淘宝的:https://ruby.taobao.org/ 先移除本有的sources gem sources --remov ...
- 黑马程序员——String类
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...
- SQL数据库注入防范 ASP.NET Globle警告
在项目中的Global.asax页面代码中加下面的代码,就可以有效的防范简单的SQL注入. protected void Application_BeginRequest(Object sender, ...
- arc engine - IName
IName对象是一个代表性对象.通过使用IName对象,可以访问它所代表的对象的一些基本属性,而不用将整个对象调入内存.我们用IWorkspace 获得一个Workspace,那可是会调入内存的,而I ...
- sunday算法实现
这个算法比其他的kmp bm 好理解的太多,而且速度还很快. sunday思路是: 1,Sunday算法是Daniel M.Sunday于1990年提出的一种比BM算法搜索速度更快的算法. 2,S ...
- Session与Cookie间不得不说的一些事
在很久很久以前,刚有浏览器和网页的时候,web开发者发现了一个问题,我必须要在客户端这边保存一些东西才能实现某些功能,比如大家喜闻乐见的购物车.用户登录.自动登陆等.但是客户端只有一个浏览器,怎么在用 ...
- [C入门 - 游戏编程系列] 贪吃蛇篇(二) - 食物定义
游戏中的食物没有那么多复杂属性,特别是贪吃蛇游戏中,我把食物看待的很简单: 1. 它必须属于世界,才能出现在世界.不可能一个不属于世界的食物,出现在世界中:但是可能存在着一个食物,它属于世界,但是却没 ...
- Linux - How To Set Up an NFS Mount on CentOS 6
About NFS (Network File System) Mounts NFS mounts work to share a directory between several servers. ...