C++獲取系統時間方法綜述


Unix時間

unix時間戳是從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數

獲取方法

gettimeofday函數

函數原型

#include<sys/time.h>
int gettimeofday(struct  timeval*tv,struct  timezone *tz )

結構體說明

struct  timeval{
       long  tv_sec;/*秒*/
       long  tv_usec;/*微妙*/
};
struct  timezone{
        int tz_minuteswest;/*和greenwich時間差*/
        int tz_dsttime; 
}

使用方法

int64_t GetCurrentTime()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

time函數

#include <time.h>
time_t time(time_t *t);

time_t其實就是long int類型,只不過被typedef重命名了。
調用time時,需要定一個long int變量(緩存)來存放總秒數。

功能

返回從1970.1.1經過的秒數

使用方法

//通過返回值獲取, 不使用參數時,參數指定為NULL
time_t tim = time(NULL); 
//通過參數獲取
long int tim = 0;
time(&tim);
printf("tim = %ld\n",tim);

ctime函數

#include <time.h>
char *ctime(const time_t *timep);

功能

將time返回的總秒數,轉為固定的格式時間,不過這個時間是國際時間,並不是本地時間(我們的本地時間是北京時間)

使用方法

long int tim = 0;
time(&tim);
char * ctim = NULL;
ctim = ctime(&tim);
printf("%s\n",ctim); //Mon Feb 17 11:00:01 2020

gtime函數

#include <time.h>
struct tm *gmtime(const time_t *timep);

功能

將time返回的總秒數,轉為國際時間的年 月 日 時 分 秒。
然后開辟一個struct tm結構體變量,將年月日時分秒放到struct tm結構體變量中

//time.h
struct tm {
          int tm_sec;       /* 秒 – 取值區間為[0,59] */
          int tm_min;       /* 分 - 取值區間為[0,59] */
          int tm_hour;      /* 時 - 取值區間為[0,23] */
          int tm_mday;      /* 一個月中的日期 - 取值區間為[1,31] */
          int tm_mon;       /* 月份(從一月開始,0代表一月) - 取值區間為[0,11]*/
          int tm_year;      /* 年份,其值等於實際年份減去1900 */
          int tm_wday;      /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
          int tm_yday;      /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
          int tm_isdst;     /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
 };

使用方法

long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = gmtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year-1900, gmt->tm_mon+1 ,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);
return 0;

locatime函數

#include <time.h>
struct tm *localtime(const time_t *timep);

功能

與gmtime完全一樣,只不過是轉為本地時間的年月日時分秒,我們的本地時間是北京時間

使用方法

long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year,gmt->tm_mon,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);
return 0;

mktime函數

#include <time.h>	
time_t mktime(struct tm *tm);

功能

將struct tm變量中的年月日時分秒,反過來轉為總秒數。

使用方法

long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year,gmt->tm_mon,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);

long int  mkt = mktime(gmt);
printf("總秒數為:%ld\n",mkt);
return 0;

asctime函數

#include <time.h>	
char *asctime(const struct tm *tm);

功能

負責將struct tm中的年月日時分秒,組合為固定格式的時間。

使用方法

long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
char *asc = asctime(gmt);
printf("%s\n",asc);//Mon Feb 17 11:00:01 2020
return 0;

strftime函數

#include <time.h>				
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

###功能
與asctime功能一樣,只不過strftime能夠組合為我們自己指定的時間格式。
為了組合為我們自定義的時間格式,我們需要為函數其指定格式

使用方法

參數
s:緩存地址,這個緩存用於存放轉換后的字符串。
max:緩存的大小
tm:放有年月日時分秒的結構體變量的地址。
format:自定義時間格式與printf("%d %s", a, buf);指定打印格式的操作方式是一樣的

long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
char buf[100] = {0};
strftime(buf, sizeof(buf),"%Y.%m.%d %H:%M:%S\n", gmt);
printf("buf = %s\n",buf); //2020.11.23 18:00:00
return 0;

參考鏈接

獲取系統時間, time 函數...
C/C++獲取時間方法:gettimeofday()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM