kezunlin · 2019年11月26日

使用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 of YYYY-mmm-DD HH:MM:SS.fffffffff format where mmm is the three character month name.
  • std::string to_iso_string(ptime) returns a string in the form of YYYYMMDDTHHMMSS,fffffffff where T is the date-time separator.
  • std::string to_iso_extended_string(ptime) returns a string in the form of YYYY-MM-DDTHH:MM:SS,fffffffff where 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

推荐阅读
关注数
2
文章数
52
[链接] C++,Python. Computer Vision and Deep Learning.
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息