利用boost获取时间并格式化的方法

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

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站制作、猇亭网络推广、微信小程序开发、猇亭网络营销、猇亭企业策划、猇亭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供猇亭建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

1.   输出YYYYMMDD

#include  
#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

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

#include  
#include  
#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获取时间并格式化的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。


文章题目:利用boost获取时间并格式化的方法
URL标题:http://bzwzjz.com/article/jsppih.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都响应式网站建设 网站建设推广 广安网站设计 成都网站建设 重庆网站制作 响应式网站设计方案 网站制作 网站设计 手机网站制作 响应式网站设计 成都网站建设 外贸营销网站建设 营销网站建设 成都网站制作公司 重庆网站建设 成都响应式网站建设公司 成都网站建设 品牌网站建设 成都网站制作 成都网站设计公司 高端网站建设 网站制作