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

1. 输出YYYYMMDD

[cpp] view plaincopy

#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

[cpp] view plaincopy

  1. #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里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。

[cpp] view plaincopy

#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();

boost 时间的更多相关文章

  1. boost 时间与日期处理

    博客转载自: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确, ...

  2. date tod = boost::gregorian::day_clock::local_day(); //当前日期

    boost 时间和日期 - 苦逼码农 - 博客频道 - CSDN.NET date tod = boost::gregorian::day_clock::local_day(); //当前日期

  3. boost开发指南

    C++确实很复杂,神一样的0x不知道能否使C++变得纯粹和干爽? boost很复杂,感觉某些地方有过度设计和太过于就事论事的嫌疑,对实际开发工作的考虑太过于理想化.学习boost本身就是一个复杂度,有 ...

  4. Spell Boost

    Spell Boost 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Shadowverse is a funny card game. One day you are playing ...

  5. Boost学习笔记(二) 时间与日期

    timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...

  6. 《Boost程序库完全开发指南》读书笔记-日期时间

    ●timer库 #include <boost\timer.hpp> #include <boost\progress.hpp> 1.timer类 // timer类的示例. ...

  7. 利用boost获取时间并格式化

    利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题. 1. 输出YYYYMMDD #include <boost/date_time/gregorian/gregorian.hpp& ...

  8. (一)boost库之日期、时间

    (一)boost库之日期.时间 一.计时器  计时器,通常在一个项目中统计一个函数的执行时间是非常实用的.   #include <boost/timer.hpp> void PrintU ...

  9. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

随机推荐

  1. Struts1 工作流程

    一个老项目的维护 , 需要学习一下 Struts1. struts1运行步骤 1.项目初始化:项目启动时加载 web.xml,struts1 的总控制器 ActionServlet 是一个 Servl ...

  2. document.execCommand & contenteditable

    document.execCommand & contenteditable https://developer.mozilla.org/zh-CN/docs/Web/API/Document ...

  3. 关于Delphi内存表的使用说明

    关于Delphi内存表的使用说明: 1.建立临时表  数据输入是开发数据库程序的必然环节.在Client/Server结构中,客户端可能要输入一批数据后,再向服务器的后台数据库提交,这就需要在本地(客 ...

  4. Container With Most Water - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...

  5. 【BZOJ4869】【SHOI2017】相逢是问候

    Description BZOJ传送门 Solution 这题涉及到指数嵌套堆叠,可能可以用欧拉函数解决. 试想一个数\(a_i\)经过\(k\)次操作后会变成什么? \[ k个c\;\; \begi ...

  6. 【uoj129】 NOI2015—寿司晚宴

    http://uoj.ac/problem/129 (题目链接) 题意 给出2~n这n-1个数,求选2个集合,使得从两集合中任意各选取1个数出来它们都互质.求方案数. Solution PoPoQQQ ...

  7. 单点登录(七)-----实战-----cas server去掉https验证

    我们在搭建cas中已经说过如果不搭建https证书体系的需要去掉https的验证: 单点登录(二)----实战------简单搭建CAS---测试认证方式搭建CAS 因为cas4.2以上的代码做了一些 ...

  8. 使用VS2012调试Dump文件

    前一节我讲了怎么设置C++崩溃时生成Dump文件 , 点击 传送门 , 这一节我讲讲怎么使用 VS2012 调试生成的 Dump 文件 , 甚至可以精确到出错的那一行代码上面 ; 1. 生成 Dump ...

  9. 2018-2019 ACM-ICPC ECfinal I. Misunderstood … Missing

    题目链接 首先有两个个属性值:\(A,D\),其中\(A\)表示目前攻击力,\(D\)表示每回合攻击的增量. 现在一共有\(n\)个回合,每一回合\(i\),可以有以下三种操作: 1.进行攻击,造成\ ...

  10. md5sum/opensll md5

    http://m.blog.csdn.net/article/details?id=42041329 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Mess ...