boost::ptime的常用方法

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

你可能感兴趣的:(C++,c++)