【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 ...
随机推荐
- LeetCode OJ:Subsets(子集)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- UVA - 10817 Headmaster's Headache (状压类背包dp+三进制编码)
题目链接 题目大意:有S门课程,N名在职教师和M名求职者,每名在职教师或求职者都有自己能教的课程集合以及工资,要求花费尽量少的钱选择一些人,使得每门课程都有至少两人教.在职教师必须选. 可以把“每个课 ...
- LeetCode Target Sum
原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...
- 修改git commit 最后一次提交的注释信息 以及如何退出git bash vim编辑器
https://www.cnblogs.com/sandy-happyhour/p/5950084.html 今天用git commit -m “注释”提交的时候,注释写错了,于是各种查资料开始了和g ...
- Python函数-complex()
complex([real[, imag]]) 作用: 创建一个值为real + imag * j的复数或者转化一个字符串或数为复数.如果第一个参数为字符串,则不需要指定第二个参数. 参数real: ...
- AfxExtractSubString 函数的相关问题
AfxExtractSubString函数的用法 注:本文系rainy8758原创,转载请注明出处:http://blog.hjenglish.com/rainy8758/articles/10109 ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
- laravel的批量插入
在日常开发中,用到批量插入的操作还是挺多的.记得很早很早以前,我还是在循环中写sql插入,结果被项目经理按在地上摩擦.好吧,性能这东西,用不到的时候还好,万一性能成为瓶颈,那代码优化,数据库优化就首当 ...
- 蓝桥杯 历届试题 PREV-1 核桃的数量
历届试题 核桃的数量 时间限制:1.0s 内存限制:256.0MB 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑 ...
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...