利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。

1. 输出YYYYMMDD

  1. #include <boost/date_time/gregorian/gregorian.hpp>
  2. #define BOOST_DATE_TIME_SOURCE
  3. std::string strTime = boost::gregorian::to_iso_string(\
  4. boost::gregorian::day_clock::local_day());
  5. std::cout << strTime.c_str() << std::endl;
  1. #include <boost/date_time/gregorian/gregorian.hpp>
  2. #define BOOST_DATE_TIME_SOURCE
  3. std::string strTime = boost::gregorian::to_iso_string(\
  4. boost::gregorian::day_clock::local_day());
  5. std::cout << strTime.c_str() << std::endl;

2. 输出YYYYMMDD-HH:MM:SS

  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #define BOOST_DATE_TIME_SOURCE
  3. std::string strTime = boost::posix_time::to_iso_string(\
  4. boost::posix_time::second_clock::local_time());
  5. // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
  6. int pos = strTime.find('T');
  7. strTime.replace(pos,1,std::string("-"));
  8. strTime.replace(pos + 3,0,std::string(":"));
  9. strTime.replace(pos + 6,0,std::string(":"));
  10. std::cout << strTime.c_str() << std::endl;
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #define BOOST_DATE_TIME_SOURCE
  3. std::string strTime = boost::posix_time::to_iso_string(\
  4. boost::posix_time::second_clock::local_time());
  5. // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
  6. int pos = strTime.find('T');
  7. strTime.replace(pos,1,std::string("-"));
  8. strTime.replace(pos + 3,0,std::string(":"));
  9. strTime.replace(pos + 6,0,std::string(":"));
  10. std::cout << strTime.c_str() << std::endl;

3. 计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。

  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #include <boost/thread.hpp>
  3. #define BOOST_DATE_TIME_SOURCE
  4. boost::posix_time::ptime time_now,time_now1;
  5. boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
  6. // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
  7. time_now = boost::posix_time::microsec_clock::universal_time();
  8. // sleep 100毫秒;
  9. boost::this_thread::sleep(boost::posix_time::millisec(100));
  10. time_now1 = boost::posix_time::microsec_clock::universal_time();
  11. time_elapse = time_now1 - time_now;
  12. // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
  13. int ticks = time_elapse.ticks();
  14. // 得到两个时间间隔的秒数;
  15. int sec = time_elapse.total_seconds();
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #include <boost/thread.hpp>
  3. #define BOOST_DATE_TIME_SOURCE
  4. boost::posix_time::ptime time_now,time_now1;
  5. boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
  6. // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
  7. time_now = boost::posix_time::microsec_clock::universal_time();
  8. // sleep 100毫秒;
  9. boost::this_thread::sleep(boost::posix_time::millisec(100));
  10. time_now1 = boost::posix_time::microsec_clock::universal_time();
  11. time_elapse = time_now1 - time_now;
  12. // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
  13. int ticks = time_elapse.ticks();
  14. // 得到两个时间间隔的秒

利用boost获取时间并格式化的更多相关文章

  1. js Date()获取时间,格式化输出,时间比较大小

    1.获取时间并且格式化输出 new Date().toLocaleString('cn',{hour12:false}) //2018/12/6 17:57:15 new Date().toLocal ...

  2. 利用js获取时间并输出值

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式

    要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...

  4. 单位换算(格式化十进制数-B),获取时间工具类CommenUtil

    package com.example.administrator.filemanager.utils;import java.text.DecimalFormat;import java.text. ...

  5. java 获取系统当前时间并格式化

      java 获取系统当前时间并格式化 CreateTime--2018年5月9日11:41:00 Author:Marydon 实现方式有三种 updateTime--2018年7月23日09点32 ...

  6. JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss"

    先来看下JS中的日期操作: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年 ...

  7. js 获取当前时间并格式化

      js 获取当前时间并格式化 CreateTime--2018年2月7日11:04:16 Author:Marydon 方式一 /** * 获取系统当前时间并格式化 * @returns yyyy- ...

  8. thymeleaf获取当前时间并格式化输出

    有时候会需要在模板中直接打印时间的需求,如果输出一个时间还需要在java类中去获取model的话,那未免也太麻烦了,以下为thymeleaf在模板中直接获取时间戳并格式化输的代码 获取时间戳 < ...

  9. js获取当前时间的年月日时分秒以及时间的格式化

    1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...

随机推荐

  1. storm启动流程

    email:chenguibin2004@126.com storm: 是一个分布式的实时流式计算框架,具有低延迟.高可用.分布式.可扩展.数据不丢失的特点, storm包含四个核心组件: Nimbu ...

  2. 解决linux不能使用chmod更改权限的问题

    本人安装的是win10和ubuntu的双系统,发现在ubuntu下挂载windows硬盘不用命令chmod更改文件的权限,解决方法记录如下: 对于使用命令$ chmod 777 dirname更改不了 ...

  3. nagios和zabbix自定义监控脚本

    一. 自定义nagios监控脚本1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.shvim  /usr/local/nagios/libexec/ch ...

  4. Web App 前端构建(纯净版)

    asp.net 母版页: <!DOCTYPE html> <html> <head> <meta charset="utf-8" name ...

  5. Java 日期与字符串的转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  6. window.dialogArguments的使用

    <HTML> <HEAD> <TITLE>showModelessDialogEX.htm</TITLE> <SCRIPT> var sUs ...

  7. iOS横竖屏

    现在开发的APP大部分界面是竖屏的,只有视频播放的界面和webview阅读文字的界面是可以横屏操作的. 那么就进行如下处理: 1.首先确保APP支持横屏旋转 2.我的App里面都是走UINavigat ...

  8. Mysql学习(慕课学习笔记3)数据类型

    数据类型 数据类型是指.存储过程参数.表达式和局部变量的数据特征, 它决定了数据的存储格式,代表了不同的信息类型. 整型 Tinyint      有符号位 -128到127   无符号位 0到255 ...

  9. 【译】神经网络与深度学习 Ch1-Section0

    用神经网络识别手写数字 人类的视觉系统是是大自然的奇迹.考虑下面手写数字序列: 大多数人能够轻易地是识别出是504192.在我们大脑的每个半球都有一个基础的皮质,这就是我们熟知的V1区,它包含了14亿 ...

  10. 使用ES6进行开发的思考

    ECMAScript6已经于近日进入了RC阶段,而早在其处于社区讨论时,我就开始一直在尝试使用ES6进行开发的方案.在Babel推出后,基于ES6的开发也有了具体可执行的解决方案,无论是Build还是 ...