std::chrono::system_clock::time_point,自定义时间工具类

time_t 是32位的整数,用来存放格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,不能表示毫秒,约能表示135年

_t timep(0);("%d \n", timep); //0(&timep); /*获得time_t结构的时间,UTC时间*/("%d \n", timep); //1550036706 到现在为止的秒数
//#include<time.h> tm{t tm_sec; /* 秒–取值区间为[0,59] */t tm_min; /* 分 - 取值区间为[0,59] */t tm_hour; /* 时 - 取值区间为[0,23] */t tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */t tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */t tm_year; /* 年份,其值从1900开始 */t tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */};

1、需要的头文件

include

include

include <time.h>

2、std::chrono::system_clock::time_point的使用

::chrono::system_clock::time_point start_time;   //默认值为 1970-01-01 08:00:00::chrono::system_clock::time_point t(std::chrono::seconds(2)) ;//初始化2秒  1970-01-01 08:00:02::cout << t.time_since_epoch().count() <<std::endl;//纳秒  2000000000::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t.time_since_epoch()).count()     <<std::endl;//毫秒  2000::cout << std::chrono::system_clock::now().time_since_epoch().count()     <<std::endl; //纳秒  1536115134721690145_t sec= std::chrono::duration_cast<std::chrono::duration<int>>        (std::chrono::system_clock::now() - t).count();//秒 1536115132::cout << sec <<std::endl;= std::chrono::duration_cast<std::chrono::seconds>//秒 1536115132  (std::chrono::system_clock::now() - t).count();::cout << sec <<std::endl;= std::chrono::duration_cast<std::chrono::milliseconds>//毫秒 1536115132721  (std::chrono::system_clock::now() - t).count();::cout << sec <<std::endl;

3、自定义的时间工具类

#ifndef TOOL_TIME_H#define TOOL_TIME_H#include<iostream>#include<chrono>#include <time.h>#include <sys/time.h>tool_time{:atic uint32_t elapse_seconds(std::chrono::system_clock::time_point &start){  return std::chrono::duration_cast<std::chrono::seconds>          (std::chrono::system_clock::now() - start).count();}atic uint64_t elapse_ms(std::chrono::system_clock::time_point &start){  return std::chrono::duration_cast<std::chrono::milliseconds>          (std::chrono::system_clock::now() - start).count();}atic uint32_t now_to_seconds(){  return std::chrono::duration_cast<std::chrono::seconds>          (std::chrono::system_clock::now().time_since_epoch()).count();}atic uint64_t now_to_ms(){  return std::chrono::duration_cast<std::chrono::milliseconds>          (std::chrono::system_clock::now().time_since_epoch()).count();}atic uint64_t now_to_us(){  return std::chrono::duration_cast<std::chrono::microseconds>          (std::chrono::system_clock::now().time_since_epoch()).count();}atic uint64_t to_ms(const std::chrono::system_clock::time_point &time){  return std::chrono::duration_cast<std::chrono::milliseconds>          (time.time_since_epoch()).count();}//"%Y-%m-%d %H:%M:%S"atic std::string to_str(const std::chrono::system_clock::time_point &time){  char _time[25] = {0};  time_t tt = std::chrono::system_clock::to_time_t(time);  struct tm local_time;  localtime_r(&tt, &local_time);  strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);  return std::string(_time);}//"%d-%02d-%02d %02d:%02d:%02d.%03d"atic std::string to_str_ex(const std::chrono::system_clock::time_point &time){  uint64_t mill = std::chrono::duration_cast<std::chrono::milliseconds>(time.time_since_epoch()).count()          -std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count()*1000;  char _time[25] = {0};  time_t tt = std::chrono::system_clock::to_time_t(time);  struct tm local_time;  localtime_r(&tt, &local_time);  //strftime(_time, 22, "%Y-%m-%d %H:%M:%S", local_time);  sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d.%03d", local_time.tm_year+1900,          local_time.tm_mon+1, local_time.tm_mday, local_time.tm_hour,          local_time.tm_min, local_time.tm_sec, mill);  return std::string(_time);}atic uint64_t now_to_ms_ex(){  struct timeval tv;  ::gettimeofday(&tv, 0);  return tv.tv_sec*1000+tv.tv_usec/1000;}//"%Y-%m-%d %H:%M:%S"atic time_t to_time(std::string str){  time_t t_;  tm tm_;  strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间  t_ = mktime(&tm_); //将tm时间转换为秒时间  return t_;}"%d-%02d-%02d %02d:%02d:%02d.%03d"atic std::chrono::system_clock::time_point to_time_ex(std::string str){  int pos = str.length()-3;  time_t t_;  tm tm_;  strptime(str.substr(0,pos).c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间  t_ = mktime(&tm_); //将tm时间转换为秒时间  int milli = std::stoi(str.substr(pos));  return std::chrono::system_clock::time_point(std::chrono::milliseconds(t_*1000 + milli));}atic std::string to_str_ex(uint64_t ms){  uint64_t mill = ms%1000;  char _time[25] = {0};  time_t tt = ms/1000;  struct tm *local_time=localtime(&tt);  //strftime(_time, 22, "%Y-%m-%d %H:%M:%S", local_time);  sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d.%03d", local_time->tm_year+1900,          local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour,          local_time->tm_min, local_time->tm_sec, mill);  return std::string(_time);}atic uint32_t elapse_seconds(time_t &start){  time_t now;  time(&now);  return static_cast<uint32_t>(std::difftime(now, start));}//"%Y-%m-%d %H:%M:%S"atic std::string to_str(const std::time_t &time){  char _time[25] = {0};  struct tm local_time;  localtime_r(&time, &local_time);  strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);  return std::string(_time);}};#endif // TOOL_TIME_H

测试

in(){to now = std::chrono::system_clock::now();d::string tt=tool_time::to_str_ex(tool_time::now_to_ms());d::string tt2=tool_time::to_str_ex(tool_time::to_ms(now));d::string tt3=tool_time::to_str(std::chrono::system_clock::to_time_t(now));d::string tt4=tool_time::to_str(now);intf("%s\n", tt.c_str());intf("%s\n", tt2.c_str());intf("%s\n", tt3.c_str());intf("%s\n", tt4.c_str());intf("%ld\n", tool_time::to_time(tt));intf("%ld\n", tool_time::now_to_seconds());intf("%ld\n", tool_time::now_to_ms()/1000);intf("%ld\n", tool_time::now_to_ms_ex()/1000);intf("%ld\n", tool_time::now_to_ms());intf("%ld\n", tool_time::now_to_ms_ex());turn 0;}

结果:

-09-23 14:03:24.941-09-23 14:03:24.941-09-23 14:03:24-09-23 14:03:24260426042604260426049412604941

转自:https://blog.csdn.net/f110300641/article/details/82147885

posted @ 2023-02-17 16:47  MasonLee  阅读(346)  评论(0编辑  收藏  举报