获取时间方式

格式一:YYYYMMDD

#include<iostream>
#include<string>
#include<boost/date_time/gregorian/gregorian.hpp> using namespace std; int main(int argc, char* argv)
{
string strTime = boost::gregorian::to_iso_string(boost::gregorian::day_clock::local_day());
cout<<strTime<<endl;

  //getchar();
return ;
}

输出格式结果

输出格式二:YYYYMMDD-HH:MM:SS

#include<iostream>
#include<string>
#include<boost/date_time/posix_time/posix_time.hpp> using namespace std; int main(int argc, char* argv)
{
   string strPosixTime = boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time());

     //这时候strPosixTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了,这里是以秒为单元,也可以换成
     //string strPosixTime = boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::local_time());

    int pos = strPosixTime.find('T');
strPosixTime.replace(pos,,std::string("-"));
strPosixTime.replace(pos + ,,std::string(":"));
strPosixTime.replace(pos + ,,std::string(":"));
    cout<<strPosixTime<<endl;
return ;
}

输出格式结果

获取时间间隔

#include<iostream>
#include<string>
#include<boost/thread.hpp>
#include<boost/date_time/posix_time/posix_time.hpp> using namespace std; boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse; int main(int argc, char* argv)
{ // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time(); // sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec()); 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_milliseconds(); cout<<time_now<<" "<<time_now1<<endl;
cout<<time_elapse<<endl;
cout<<ticks<<endl;
cout<<sec<<endl; getchar();
return ;
}

结果输出

【Boost】boost库获取格式化时间的更多相关文章

  1. Python获取当前时间_获取格式化时间_格式化日期

    Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...

  2. python 获取格式化时间

    #!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.asctime( time.localtime(time.t ...

  3. 【javascript】获取 格式化时间

    function getDate() { var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.ge ...

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

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

  5. Python获取当前时间及时间转换(datetime)

    datetime是Python处理日期和时间的标准库 获取当前时间 import datetime day = datetime.datetime.now() day2 = datetime.date ...

  6. js 格式化时间、字符串指定长度、随机字符串

    格式化字符串长度 方法 function formatWidth(str, width){ str += '' if(str.length<width) '+str, width) else r ...

  7. 一起学习Boost标准库--Boost.texical_cast&format库

    今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...

  8. 如何在WINDOWS下编译BOOST C++库 .

    如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25   写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0.   1)下载boost ...

  9. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

随机推荐

  1. SpringBoot WebSocket实现

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. golang实现模拟键盘按键

    公司前段时间要我写个小项目需要可以局域网内一个ipad控制另一台pc上的键盘输入,github上找了找,居然有个robotgo库这么神级的存在,感觉go的库真是越来越多了,虽然大部分都是第三方的.ht ...

  3. poj2336

    题目大意:一个船要把n个车渡过河  船最多载m辆车  把车运过去需要t的时间 回来也要t的时间 给定n辆车依次到河边的时间 求最短运送时间  还有最短跑几趟 一维dp  可以直接d运送时间 dp[i] ...

  4. yum search/intall, Error: xz compression not available

    转自:http://blog.hexu.org/archives/2060.shtml 遇到这个问题情景: 下午第一台系统是Centos7, 安装配置完成后,接着一台是Centos 6 系统,由于疏忽 ...

  5. WebSphere & Log4j

    IBM的东西,真是太麻烦了,一个日志都给你替换掉,太霸道了,Google了下,居然是个普遍想象,不过也有相关的解决方 案,基本好像都是在 WAS5,WAS6.1上的,我没环境,不过我这边的WAS7 没 ...

  6. bzoj 3028 食物——生成函数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3028 把式子写出来,化一化,变成 x / ((1-x)^4) ,变成几个 sigma 相乘的 ...

  7. hihoCoder#1079(线段树+坐标离散化)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学 ...

  8. who命令参数及用法详解(linux查看在线用户命令)

    功能说明:显示目前登入系统的用户信息.  语 法:who [-Himqsw][--help][--version][am i][记录文件]  补充说明:执行这项指令可得知目前有那些用户登入系统,单独执 ...

  9. MongoTemplate聚合操作

    Aggregation简单来说,就是提供数据统计.分析.分类的方法,这与mapreduce有异曲同工之处,只不过mongodb做了更多的封装与优化,让数据操作更加便捷和易用.Aggregation操作 ...

  10. 运行jar应用程序引用其他jar包的三种常用方法(jar命令)

    参考:http://blog.csdn.net/wangmuming/article/details/44343017 方法一.使用Extension Classloader来加载 你可以把需要加载的 ...