【C++】时间系统详解

一、时间基础知识

(1)UTC (Coordinated Universal Time):协调世界时,又称世界标准时间。曾由格林威治平均时间(Greenwich Mean Time,GMT)提供,如今由原子钟提供。比方,中国内地的时间与UTC的时差为+8。也就是UTC+8。美国是UTC-5。

(2)Calendar Time:日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说。这个标准时间点是不变的,该编译系统中的时间相应的日历时间都通过该标准时间点来衡量,所以能够说日历时间是“相对时间”,可是不管你在哪一个时区。在同一时刻对同一个标准时间点来说,日历时间都是一样的。

(3)Epoch指的是一个特定的时间点:1970-01-01 00:00:00 UTC,即Unix 时间戳。

(4)clock tick:时钟计时单元(而不把它叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的。一个clock tick不是CPU的一个时钟周期,而是C/C++的一个基本计时单位。

二、C++中获取时间的方法

(一)time.h中的方法

clock函数:返回程序当前运行时长

#define CLOCKS_PER_SEC ((clock_t)1000)
clock_t clock( void );

time函数:返回日历时间

#ifndef _CRT_NO_TIME_T
    #ifdef _USE_32BIT_TIME_T
        typedef __time32_t time_t;
    #else
        typedef __time64_t time_t;
    #endif
#endif

time_t time(time_t * timer);
gmtime()函数:转换成格林尼治时间,存在结构体(struct tm)里
localtime()函数:转换成本地时间,存在结构体(struct tm)里
struct tm * gmtime(const time_t *timer);  //转换成格林尼治时间,存在结构体里
struct tm * localtime(const time_t * timer);   //转换成本地时间,存在结构体里

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900 从1900年到现在的年数。如想表达当前年份,需加上1900,即tm_year + 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};
asctime()函数与ctime()函数
char * asctime(const struct tm * timeptr);   asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串
char * ctime(const time_t *timer);   ctime()是通过日历时间来生成时间字符串
strftime()函数
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
difftime()函数
double difftime(time_t time1, time_t time0); 计算两个时间点之间的时长
mktime()
time_t mktime(struct tm * timeptr); 将他tm 结构体转换为 t_time日历时间

demo:

/*方法一*/
    time_t * timer = nullptr;
    time_t t = time(timer);
    timer = &t;
    qDebug()<< "t = " << t;//输出日历时间
    qDebug()<< "timer = %d" << *timer;//输出日历时间,同t数值一致
    struct tm* stime = localtime(&t);//获取当前时间:本地时间
    qDebug()<< stime->tm_year + 1900 << " year " << stime->tm_mon + 1 << " mon " << stime->tm_mday << " day " << stime->tm_hour << " hour " << stime->tm_min << " min "<< stime->tm_sec << " sec ";

当前时间:2022年 3月 23日 21点 45分 17秒

测试输出结果如下:

t = 1648043117

timer = 1648043117

2022 year 3 mon 23 day 21 hour 45 min 17 sec

//方法二
    char tmp[32]={NULL};
    time_t t = time(timer);
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&t));
    qDebug() << "fun2 = " << tmp;

fun2 = 2022-03-23 21:45:17

//方法三:

    time_t tm;
    time(&tm);
    char tmp1[128]={NULL};
    strcpy(tmp1,ctime(&tm));
    qDebug() << "fun3 = " << tmp1;

fun3 = Wed Mar 23 21:45:17 2022

(二)windows中的方法

    #include <windows.h>   

    SYSTEMTIME sys;
    GetLocalTime(&sys);

    char tm1p[64]={NULL};
    sprintf(tm1p,"%4d-%02d-%02d %02d:%02d:%02d ms:%03d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds);
    qDebug() << "fun 4 = " << tm1p;

fun 4 = 2022-03-23 21:45:17 ms:173

(三)Unix中的方法

#include <sys/time.h> 
#include <unistd.h>

struct timeval now_time;
gettimeofday(&now_time, NULL);
time_t tt = now_time.tv_sec;
tm *temp = localtime(&tt);
char time_str[32]={NULL};
sprintf(time_str,"%04d-%02d-%02d%02d:%02d:%02d",temp->tm_year+ 1900,temp->tm_mon+1,temp->tm_mday,temp->tm_hour,temp->tm_min, temp->tm_sec);
cout<<tmp<<endl;

输出:

2022-03-23 21:45:17

参考文章:

C/C++获取本地时间常见方法 - gavanwanggw - 博客园跨平台方法 方法一:手动暴力法 #include <iostream> using namespace std; #include <time.h> time_t t = tihttps://www.cnblogs.com/gavanwanggw/p/7019898.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值