boost::ptime的常用方法

本文详细介绍了Boost库中ptime类型获取和转换时间的方法,包括使用boost::posix_time::ptime获取本地时间、转换为string、从string转换回来,以及将ptime转换为uint64表示的秒、毫秒和微秒。还展示了如何从uint64转换回时间字符串,适用于需要精确时间戳的场景,例如视频帧的时间戳处理。
摘要由CSDN通过智能技术生成

boost::ptime的常用方法

主要介绍常用获取时间的方法,以及相互之间的转换
需要使用boost库,用到的头文件 “boost/timer/timer.hpp"和"boost/date_time.hpp”

获取本地时间

boost::posix_time::ptime nowTime = boost::posix_time::second_clock::local_time();
std::cout << "nowTime is: " << nowTime << std::endl;

boost::posix_time::ptime nowTimeMic = boost::posix_time::microsec_clock::local_time();
std::cout << "nowTimeMic is: " << nowTimeMic << std::endl;

输出:

nowTime is: 2022-Sep-05 14:40:28
nowTimeMic is: 2022-Sep-05 14:40:28.606521

两种获取时间的方法,前者精确到秒,后者精确到微妙
直接获取的时间,月份使用英文简写表示,直接存储不友好,可以将其转换为string

将ptime转换为string

std::string strYMD = boost::gregorian::to_iso_extended_string(nowTime.date());
std::string strHMS = boost::posix_time::to_simple_string(nowTime.time_of_day());
std::string strTime = strYMD + " " + strHMS;
std::cout << "string time is: " << strTime << std::endl << std::endl;   

输出:

string time is: 2022-09-05 14:40:28

string转换为ptime

std::string strTime_t("2022-08-15 08:08:08");
boost::posix_time::ptime pptime = boost::posix_time::time_from_string(strTime_t);
std::cout << "pptime is: " << pptime << std::endl << std::endl;

输出:

pptime is: 2022-Aug-15 08:08:08

ptime转换为uint64

ptime转换为uint64可以精确到秒、毫秒和微秒

boost::posix_time::time_duration diffTime = nowTime - boost::posix_time::time_from_string("1970-01-01 08:00:00");   //固定写死
uint64_t microTime = boost::lexical_cast<uint64_t>(diffTime.total_microseconds());
uint64_t milliTime = boost::lexical_cast<uint64_t>(diffTime.total_milliseconds());
uint64_t secondTime = boost::lexical_cast<uint64_t>(diffTime.total_seconds());
std::cout << "microTime(uint64) is: " << microTime << std::endl;
std::cout << "milliTime(uint64) is: " << milliTime << std::endl;
std::cout << "secondTime(uint64) is: " << secondTime << std::endl << std::endl;

输出:

microTime(uint64) is: 1662360028000000
milliTime(uint64) is: 1662360028000
secondTime(uint64) is: 1662360028

毫秒和微秒适合使用在需要精确时间的场景;比如视频帧的时间戳(每一秒包含30帧、60帧等),使用秒级就会出现重复的情况

uint64转换为string

std::string stringFromInt(uint64_t secondTimeStamp) 
{
  std::string baseTimeStr = "1970-01-01 08:00:00";
  boost::posix_time::ptime stime = boost::posix_time::time_from_string(baseTimeStr);
  //int64_t timeStamp = microTime / 1000000;    //微妙级
  //int64_t timeStamp = milliTime / 1000;      //毫秒级
  int64_t timeStamp = secondTimeStamp;
  stime += boost::posix_time::seconds(timeStamp);
  std::string strDate = boost::gregorian::to_iso_extended_string(stime.date());
  std::string strDay = boost::posix_time::to_simple_string(stime.time_of_day());
  std::string strTimeFromUint = strDate + " " + strDay;
  return strTimeFromUint;
}

输出:

time is: 2022-09-05 14:40:28

这里直接封装了一个函数,入参需要为秒级;毫秒级和微妙级需要对数据进行处理,如代码中的注释部分,在函数调用前和函数中处理都可以

完整代码

#include <iostream>
#include <string>
#include "boost/timer/timer.hpp"
#include "boost/date_time.hpp"

std::string stringFromInt(uint64_t secondTimeStamp) 
{
  std::string baseTimeStr = "1970-01-01 08:00:00";
  boost::posix_time::ptime stime = boost::posix_time::time_from_string(baseTimeStr);
  //int64_t timeStamp = microTime / 1000000;    //微妙级
  //int64_t timeStamp = milliTime / 1000;      //毫秒级
  int64_t timeStamp = secondTimeStamp;
  stime += boost::posix_time::seconds(timeStamp);
  std::string strDate = boost::gregorian::to_iso_extended_string(stime.date());
  std::string strDay = boost::posix_time::to_simple_string(stime.time_of_day());
  std::string strTimeFromUint = strDate + " " + strDay;
  return strTimeFromUint;
}

int main
{
  //获取本地时间
  boost::posix_time::ptime nowTime = boost::posix_time::second_clock::local_time();
  std::cout << "nowTime is: " << nowTime << std::endl;

  boost::posix_time::ptime nowTimeMic = boost::posix_time::microsec_clock::local_time();
  std::cout << "nowTimeMic is: " << nowTimeMic << std::endl;

  //将ptime转换为string
  std::string strYMD = boost::gregorian::to_iso_extended_string(nowTime.date());
  std::string strHMS = boost::posix_time::to_simple_string(nowTime.time_of_day());
  std::string strTime = strYMD + " " + strHMS;
  std::cout << "string time is: " << strTime << std::endl << std::endl;                                
  
  //string转换为ptime
  std::string strTime_t("2022-08-15 08:08:08");
  boost::posix_time::ptime pptime = boost::posix_time::time_from_string(strTime_t);
  std::cout << "pptime is: " << pptime << std::endl << std::endl;
  
  //ptime转换为uint64
  boost::posix_time::time_duration diffTime = nowTime - boost::posix_time::time_from_string("1970-01-01 08:00:00");   //固定写死
  uint64_t microTime = boost::lexical_cast<uint64_t>(diffTime.total_microseconds());
  uint64_t milliTime = boost::lexical_cast<uint64_t>(diffTime.total_milliseconds());
  uint64_t secondTime = boost::lexical_cast<uint64_t>(diffTime.total_seconds());
  std::cout << "microTime(uint64) is: " << microTime << std::endl;
  std::cout << "milliTime(uint64) is: " << milliTime << std::endl;
  std::cout << "secondTime(uint64) is: " << secondTime << std::endl << std::endl;

  //uint64转换为string
  std::cout << "time is: " << stringFromInt(secondTime) << std::endl;
  
  return 0;
}

编译调试

编译调试是通过cmake来完成
CMakeLists.txt文件

cmake_minimum_required(VERSION 3.3)
PROJECT(time)
set(CMAKE_CXX_STANDARD 11)
aux_source_directory(./ PROGRAM_SOURCE)

include_directories("/include")  #头文件路径
link_directories("/lib/")   #库路径

add_executable(time ${PROGRAM_SOURCE})

target_link_libraries(time libboost_timer.a libboost_date_time.a)

cmake …/
make
./time
(系统:CentOS Linux release 7.3.1611 内核:3.10.0-514)
运行结果:

nowTime is: 2022-Sep-05 14:40:28
nowTimeMic is: 2022-Sep-05 14:40:28.606521
string time is: 2022-09-05 14:40:28

pptime is: 2022-Aug-15 08:08:08

microTime(uint64) is: 1662360028000000
milliTime(uint64) is: 1662360028000
secondTime(uint64) is: 1662360028

time is: 2022-09-05 14:40:28
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值