【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 ...
随机推荐
- 14-THREE.JS 聚光灯
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- [转载]java导出word的5种方式
在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...
- CMMI 3级精简并行过程综述
“精简并行过程”(Simplified Parallel Process,SPP)是基于CMMI以及软件工程和项目管理知识而创作的一种“软件过程改进方法和规范”,它由众多的过程规范和文档模板组成.SP ...
- mobiscroll时间控件
https://docs.mobiscroll.com/3-0-0_beta/javascript/numpad#events 这个是官方的日期插件,日历,什么效果都有,很强大的. mobiscrol ...
- MySQL 约束和数据库设计
1.MySQL 约束: 1.约束的概念: 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性. MySQL中,常用的几种约束: 约束类型: 非空 主键 唯一 外键 默认值 ...
- SpringCloud教程 | 第五篇: 路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...
- 剑指offer-7.旋转数组的最小数字
看起来不需要用二分法查找 --------------------------------------------------------- 时间限制:3秒 空间限制:32768K 热度指数:5098 ...
- Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of th ...
- BZOJ5302: [Haoi2018]奇怪的背包
BZOJ5302: [Haoi2018]奇怪的背包 https://lydsy.com/JudgeOnline/problem.php?id=5302 分析: 方程\(\sum\limits_{i=1 ...
- 【LeetCode】005. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...