利用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. Win32多线程编程(1) — 基础概念篇

      内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...

  2. VS2015 添加DNX SDK

    第一次运行VS2015,添加第一个ASP.NET 5程序时会报一个错误“DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装. 解决办法: 打开CMD :输入 @po ...

  3. 解决Android AVD启动报错问题

    好不容易从ADT Bundle转为Android Studio的开发环境,一路荆棘,现在又遇到了模拟器的问题,本来直接用真机调试程序会更快些,但是为了模拟多种系统不得不开启AVD. 废话不说,问题和解 ...

  4. (转)web.config详解之在文件中配置网站默认页面

    在<configuration></configuration>中添加下面的配置 <system.webServer>        <defaultDocu ...

  5. Android原生Calendar代码阅读(一)

    原生Calendar代码: 5.0Calendar源码.rar 提取的JavaDoc: Calendar的javadoc.rar 1. AsyncQueryService和AsyncQueryServ ...

  6. 临时解决linux下time wait问题

     通过 netstat  -anp | grepTIME_WAIT | wc -l 命令查看数量,发现TIME_WAIT的连接数量超过了阈值   1.初步怀疑是程序没有关闭连接,codereview了 ...

  7. tomcat免安装版注册为系统服务

    环境: OS:windows7_64bit JDK:jdk1.6_64bit tomcat:apache-tomcat-7.0.61-windows-x64 1.修改tomcat/bin/servic ...

  8. Private Members in JavaScript

    Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...

  9. java Eclipse debug技巧

    摘要:调试不仅可以查找到应用程序缺陷所在,还可以解决缺陷.对于Java程序员来说,他们不仅要学会如何在Eclipse里面开发像样的程序,更需要学会如何调试程序.本文介绍了Java程序员必知的10个调试 ...

  10. push方法的页面间跳转--

    一,自定义动画写push方法-- 添加coreGraphics.framework框架 在CATransitionAnimation.h文件里面引入-- #import <QuartzCore/ ...