使用boost data_time模块来获取毫秒级时间并转换为string字符串
本文首发于个人博客https://kezunlin.me/post/107cbcbb/,欢迎阅读!
using boost.date_time to get time in millisecond microsecond level
Guide
format flags
second millisecond microsecond nanosecond
Format Flags
%f
Fractional seconds are always used, even when their value is zero
"13:15:16.000000"%F *
Fractional seconds are used only when their value is not zero.
"13:15:16"
"05:04:03.001234"
to string
The Boost.Date_Time library provides the following ptime to std::string conversions within the boost::posix_time namespace:
std::string to_simple_string(ptime)returns a string in the form ofYYYY-mmm-DD HH:MM:SS.fffffffffformat where mmm is the three character month name.std::string to_iso_string(ptime)returns a string in the form ofYYYYMMDDTHHMMSS,fffffffffwhere T is the date-time separator.std::string to_iso_extended_string(ptime)returns a string in the form ofYYYY-MM-DDTHH:MM:SS,fffffffffwhere T is the date-time separator.
date_time.cpp
#include <iostream>
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
using namespace std;
using namespace boost::posix_time;
using namespace boost::gregorian;
/*
second millisecond microsecond nanosecond
Format Flags
%f
Fractional seconds are always used, even when their value is zero
"13:15:16.000000"
%F *
Fractional seconds are used only when their value is not zero.
"13:15:16"
"05:04:03.001234"
*/
std::string ptime_2_str_name(boost::posix_time::ptime now)
{
// https://stackoverflow.com/questions/5018188/how-to-format-a-datetime-to-string-using-boost
// for multiple use
std::stringstream ss;
//static std::locale loc(std::cout.getloc(), new time_facet("%Y%m%d_%H%M%S_%f"));
static std::locale loc(ss.getloc(), new time_facet("%Y%m%d_%H%M%S_%f"));
ss.imbue(loc);
ss << now;
return ss.str(); // 20180118_111501_208797
}
std::string str_name_2_iso_format(std::string str_name)
{
/*
20180118_111501_208797 ===> 20180118T111501.208797===>from_iso_string===>ptime
*/
//std::cout << "time length: " << str_ptime.length() << endl; //22
size_t first_pos = str_name.find_first_of('_');
size_t second_pos = str_name.find_last_of('_');
str_name[first_pos] = 'T';
str_name[second_pos] = '.';
return str_name;
}
ptime from_name_string(std::string str_ptime)
{
std::string str_iso_ptime = str_name_2_iso_format(str_ptime);
return from_iso_string(str_iso_ptime);
}
int main()
{
ptime p1(date(2001, 1, 1), hours(1));
ptime p2 = time_from_string("2002-2-2 02:00:00.999888"); // fraction part: 6 bits
ptime p3 = from_iso_string("20030303T031233.777666"); // fraction part: 6 bits
ptime p4 = second_clock::local_time(); // in second
ptime p5 = microsec_clock::universal_time(); // UTC World time in millisecond,microsecond
ptime p6 = microsec_clock::local_time(); // UTC local time in millisecond,microsecond
cout << p1 << endl
<< p2 << endl
<< p3 << endl
<< p4 << endl
<< p5 << endl
<< p6 << endl << endl;
/*
* date()
* time_of_day()
*/
date d = p1.date();
time_duration td = p1.time_of_day();
cout << d << ", " << td << endl << endl;
cout << to_simple_string(p2) << endl //YYYY-mmm-DD HH:MM:SS.ffffff
<< to_iso_string(p2) << endl //YYYYMMDDTHHMMSS,ffffff
<< to_iso_extended_string(p2) << endl; //YYYY-MM-DDTHH:MM:SS,ffffff
cout << "User defined format time:" << endl;
std::string str_name = ptime_2_str_name(p6);
ptime p7 = from_name_string(str_name);
cout <<" p6: "<< ptime_2_str_name(p6) << endl;
cout <<"p7 from String2Ptime(p6): "<< ptime_2_str_name(p7) << endl;
return 0;
}
CMakeLists.txt
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 2.8)
# Project's name
project(date_time)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
#find_package(Boost 1.5.8 REQUIRED COMPONENTS date_time system filesystem iostreams)
find_package(Boost 1.5.8 REQUIRED COMPONENTS date_time)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}")
add_executable(date_time ${PROJECT_SOURCE_DIR}/date_time.cpp)
target_link_libraries(date_time ${Boost_LIBRARIES})
run and output
compile program and run
mkdir build
cd build
cmake ..
make
cd bin
./date_time
output
2001-Jan-01 01:00:00
2002-Feb-02 02:00:00.999888
2003-Mar-03 03:12:33.777666
2018-Jan-18 15:20:47
2018-Jan-18 07:20:47.815415
2018-Jan-18 15:20:47.815419
2001-Jan-01, 01:00:00
2002-Feb-02 02:00:00.999888
20020202T020000.999888
2002-02-02T02:00:00.999888
User defined format time:
p6: 20180118_152047_815419
p7 from String2Ptime(p6): 20180118_152047_815419
Reference
History
- 20180118: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/107cbcbb/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
使用boost data_time模块来获取毫秒级时间并转换为string字符串的更多相关文章
- 在Windows及Linux下获取毫秒级运行时间的方法
在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...
- GetSystemTime API可以得到毫秒级时间
用Now返回的日期格式中年只有2位,即2000年显示为00, 这似乎不太令人满意. 此外Now和Time都只能获得精确到秒的时间,为了得到更精确的毫秒级时间,可以使用API函数GetSystemTim ...
- Linux下得到毫秒级时间--C语言实现(转-度娘818)
Linux下得到毫秒级时间--C语言实现 原文链接: http://www.cnblogs.com/nwf5d/archive/2011/06/03/2071247.html #ifdef HAVE_ ...
- C++获取毫秒级时间戳
#include<chrono> auto timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::sy ...
- java/python中获取当前系统时间,并与字符串相互转换格式,或者转化成秒数,天数等整数
java转换成秒数 Date类有一个getTime()可以换回秒数,例如: public class DateToSecond { public static void main(String[] a ...
- c++ 中 毫秒级时间获取
#include <time.h> clock_t start,ends; start=clock(); Sleep(); ends=clock(); cout<<ends-s ...
- Delphi获取毫秒级时间戳
function GetJavaTime( d: TDateTime ): Int64; var dJavaStart: TDateTime; begin //java里的时间是从1970年1月1日0 ...
- QT 获取当前毫秒级时间缀
long long currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
- C++计时器:毫秒级和微秒级
1.毫秒级 使用GetTickCount()获取系统启动所经过的毫秒数 #include<iostream> using namespace std; int main(){ DWORD ...
随机推荐
- jQuery插件图片懒加载lazyload
来自XXX的前言: 什么是ImageLazyLoad技术 在页面上图片比较多的时候,打开一张页面必然引起与服务器大数据量的 交互.尤其是对于高清晰的图片,占的几M的空间.ImageLazyLoad技术 ...
- 百万年薪python之路 -- MySQL数据库之 Navicat工具和pymysql模块
一. IDE工具介绍(Navicat) 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具,我们使用Navicat工具,这个工具本质上就是一个socket客户端,可视化的连接 ...
- typescript 入门教程二
ts中面向对象成员修饰符:public , private , protexted(ts官方网站:ts) 在ts中,默认的成员修饰符就是public public:是表示是公开的,在任何地方,都可以调 ...
- Java基础(37)ArrayList的remove方法
1.问题描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 输入: s = "abcd& ...
- 设计模式C++描述----09.桥接(Bridge)模式
一. 举例 N年前: 计算机最先出来时,软件和硬件是一绑在一起的,比如IBM出了一台电脑,上面有一个定制的系统,假如叫 IBM_Win,这个IBM_Win系统当然不能在HP电脑上运行,同样HP出的HP ...
- iOS开发 swift 3dTouch实现 附代码
iOS开发 swift 3dTouch实现 附代码 一.What? 从iphone6s开始,苹果手机加入了3d touch技术,最简单的理解就是可以读取用户的点击屏幕力度大小,根据力度大小给予不同的反 ...
- .Net Core WebApi(三)在Linux服务器上部署
鸽了好久,终于有个时间继续写了,继上一篇之后,又写(水)了一篇,有什么不足之处请大家指出,多谢各位了. 下面有两个需要用到的软件,putty和pscp,我已经上传到博客园了,下载请点击这里. 一.准备 ...
- 初学android小笔记(一)
一:应用外观基础设置 (1)去掉标题栏:打开Android Manifest文件,将theme如下设置 (2)改应用图标:将icon指定图片改为想要的app图标即可 (3)改应用名字: 在Manife ...
- Centos6 Tengine开启http2传输协议
1.前言 最近在优化网站的访问速度,为网站开启http2协议,这个协议有什么优点呢?如下: http2是下一代的传输协议,以后都会普遍用它,是一个趋势. http2有多路复用特性,意思是访问一个域名下 ...
- 「2019.8.9 考试」神仙的dp总让人无所适从
T1是个容斥,我掐手指一算他为了卡容斥的正确性,绝不会把n和m出的很相近($O(n^2)$算法在nm相等的时候达到最高时间复杂度),不然就太好做了,于是开了特判+各种卡常和滚动数组优化,卡到了70分, ...