【Boost】boost库获取格式化时间

获取时间方式
格式一: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库获取格式化时间的更多相关文章
- Python获取当前时间_获取格式化时间_格式化日期
Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...
- python 获取格式化时间
#!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.asctime( time.localtime(time.t ...
- 【javascript】获取 格式化时间
function getDate() { var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.ge ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
- Python获取当前时间及时间转换(datetime)
datetime是Python处理日期和时间的标准库 获取当前时间 import datetime day = datetime.datetime.now() day2 = datetime.date ...
- js 格式化时间、字符串指定长度、随机字符串
格式化字符串长度 方法 function formatWidth(str, width){ str += '' if(str.length<width) '+str, width) else r ...
- 一起学习Boost标准库--Boost.texical_cast&format库
今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...
- 如何在WINDOWS下编译BOOST C++库 .
如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25 写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0. 1)下载boost ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
随机推荐
- JedisPool的配置参数整理
JedisPool的配置参数大部分是由JedisPoolConfig的对应项来赋值的. maxActive:控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取: ...
- this license has been cancelled
是因为IDEA注册码的问题, 解决方案: 修改此路径的hosts文件:C:\Windows\System32\drivers\etc\hosts 在其最后一行加入:“0.0.0.0 account.j ...
- BEC listen and translation exercise 11
When you are in any contest you should work as if there were — to the very last minute — a chance to ...
- python二分查找模块bisect
bisect模块用于二分查找,非常方便. Bisect模块提供的函数有: 1.查找 bisect.bisect_left(a,x, lo=0, hi=len(a)) : 查找在有序列表a中插入x的in ...
- [Unity3D]EZGUI 操作简单介绍
官方的GUI根本无法跟EZGUI比,无论是资源还是易用性还是速度.EZGUI基于Mesh不占DrawCall.EZGUI是自动合并Mesh成为一个物体,并且贴图自动制作Atlas.所以效率高,CPU消 ...
- NOIP模拟题 栅栏
题目大意 给定一个$n\times m$的网格图,每次会选择一块矩形沿着网格线铺上栅栏,或者拆除之前铺的栅栏,或者询问两个格子能否不经过栅栏直接到达. 保证栅栏没有重叠或交叉,删去的栅栏删除前一定存在 ...
- [HihoCoder1413]Rikka with String
vjudge 题意 给你一个串,问你把每个位置的字符替换成#后串中有多少本质不同的子串. \(n\le 3*10^5\) sol 首先可以计算出原串里面有多少本质不同的子串.显然就是\(\sum_{i ...
- python sentence
1.while for 增加了循环正常结束后执行的else代码块. 2.Objects are mutable 3.import copy p1 = Point() p2=copy.copy(p1) ...
- 什么是VBA,他有什么作用
目录 什么是VBA,它有什么作用 VBA在哪里存放的?怎么运行的 什么是宏?宏和VBA有什么关系? 录制一个宏 编写第一个宏 一.什么是VBA,它有什么作用 VBA是一种编程语言,它依托于Office ...
- Oracle 数据库表(常见的表)
数据库表(常见的表) 堆组织表:普通表 索引组织表:iot 嵌套表 临时表 外部表 1 表 一个表最多1000列,oracle会把列大于254的行存储在多个单独的行段中, 表中的行是无限的, 术 ...