文章目錄
基本關係
time_t&tm&time_str之間的關係
相關函數
time()
time()是最重要的一個函數,這個函數返回的是 1970年1月1日UTC時間0點0分0秒到現在所經過的秒數
函數原型
time_t time(time_t *t);
time_t跟size_t以及wchar_t都一樣,表示的是一個無符號整形值,可以存儲time返回的最大值。
asctime()
asctime()作用是把tm結構體轉換爲字符串格式輸出。
函數原型
char * asctime(const struct tm * timeptr);
gmtime()
gmtime()函數的作用是把從time中獲取的秒數轉換爲UTC時區的時間,存儲在tm結構中.
函數原型
struct tm *gmtime(const time_t *timep);
localtime()
localtime()函數的作用是把從time中獲取的時間秒數轉換爲特定時區的時間,存儲在tm結構中
函數原型
struct tm *localtime(const time_t * timep);
ctime()
ctime以“Wed Jun 30 21 :49 :08 1993\n”。格式輸出當前本地時間字符串
char *ctime(const time_t *timep);
ctime(&time_p)相當於asctime(localtime(time(&time_p)))
mktime()
mktime函數的作用和和gmtime的作用剛好相反,把tm結構體轉換爲time返回值
time_t mktime(strcut tm * timeptr);
settimeofday() 和gettimeofday()
settimeofday() 設置當前時間,只有管理員可以用。
gettimeofday() 這個函數一般作用是來獲取程序執行時間的。很奇怪,我在公司的服務器上沒有找到這個函數,因此後面的例子中沒有這兩個函數。
函數原型
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
int gettimeofday ( struct timeval * tv , struct timezone * tz );
相關數據結構和結構體
time_t
time_t是Unix時間戳(Unix timestamp),或稱Unix時間(Unix time)、POSIX時間(POSIX time),是一種時間表示方式,定義爲從格林威治時間1970年01月01日00時00分00秒起至現在的總秒數。
time_str
time_str不是c++的結構體名字,是我自己這麼定義time相關函數中的時間字符串輸出格式
"Wed Jun 30 21 :49 :08 1993\n"
tm
C++中的時間結構體
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
- int tm_sec 代表目前秒數,正常範圍爲0-59,但允許至61 秒
- int tm_min 代表目前分數,範圍0-59
- int tm_hour 從午夜算起的時數,範圍爲0-23
- int tm_mday 目前月份的日數,範圍01-31
- int tm_mon 代表目前月份,從一月算起,範圍從0-11
- int tm_year 從1900 年算起至今的年數
- int tm_wday 一星期的日數,從星期一算起,範圍爲0-6
- int tm_yday 從今年1 月1 日算起至今的天數,範圍爲0-365
- int tm_isdst 日光節約時間的旗標
此函數返回的時間日期未經時區轉換,而是UTC時間。
timeval
時間間隔結構體,第一個表示秒,第二個表示微秒
struct timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* Microseconds. */
};
timezone
時區結構體,tz_minuteswest表示和格林尼治時間差了多少分鐘,tz_dsttime表示夏令時
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
實例
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(int argc,char** argv){
time_t v_t;
time(&v_t);
printf("\n");
printf("------------time()-----------\n");
printf("\n");
printf("the time func return [%d] \n",(int)v_t);
tm *v_tm;
v_tm=gmtime(&v_t);
printf("\n");
printf("------------gmtime func result-----------\n");
printf("\n");
printf("gmtime func v_tm->tm_year is [%d] \n",v_tm->tm_year);
printf("gmtime func v_tm->tm_mon is [%d] \n",v_tm->tm_mon);
printf("gmtime func v_tm->tm_mday is [%d] \n",v_tm->tm_mday);
printf("gmtime func v_tm->tm_hour is [%d] \n",v_tm->tm_hour);
printf("gmtime func v_tm->tm_min is [%d] \n",v_tm->tm_min);
printf("gmtime func v_tm->tm_sec is [%d] \n",v_tm->tm_sec);
printf("gmtime func v_tm->tm_wday is [%d] \n",v_tm->tm_wday);
printf("gmtime func v_tm->tm_yday is [%d] \n",v_tm->tm_yday);
printf("gmtime func v_tm->tm_isdst is [%d] \n",v_tm->tm_isdst);
printf("asctime func use gmtime tm [%s",asctime(v_tm));
v_tm=localtime(&v_t);
printf("\n");
printf("------------localtime func result-----------\n");
printf("\n");
printf("localtime func v_tm->tm_year is [%d] \n",v_tm->tm_year);
printf("localtime func v_tm->tm_mon is [%d] \n",v_tm->tm_mon);
printf("localtime func v_tm->tm_mday is [%d] \n",v_tm->tm_mday);
printf("localtime func v_tm->tm_hour is [%d] \n",v_tm->tm_hour);
printf("localtime func v_tm->tm_min is [%d] \n",v_tm->tm_min);
printf("localtime func v_tm->tm_sec is [%d] \n",v_tm->tm_sec);
printf("localtime func v_tm->tm_wday is [%d] \n",v_tm->tm_wday);
printf("localtime func v_tm->tm_yday is [%d] \n",v_tm->tm_yday);
printf("localtime func v_tm->tm_isdst is [%d] \n",v_tm->tm_isdst);
printf("asctime func use localtime tm [%s",asctime(v_tm));
printf("\n");
printf("------------ctime func result-------------\n");
printf("\n");
printf("ctime result is [%s",ctime(&v_t));
printf("\n");
printf("------------mktime func result-----------\n");
printf("\n");
printf("mktime result is [%d] \n",(int)mktime(v_tm));
return 1;
}
返回結果爲
------------time()-----------
the time func return [1542358166]
------------gmtime func result-----------
gmtime func v_tm->tm_year is [118]
gmtime func v_tm->tm_mon is [10]
gmtime func v_tm->tm_mday is [16]
gmtime func v_tm->tm_hour is [8]
gmtime func v_tm->tm_min is [49]
gmtime func v_tm->tm_sec is [26]
gmtime func v_tm->tm_wday is [5]
gmtime func v_tm->tm_yday is [319]
gmtime func v_tm->tm_isdst is [0]
asctime func use gmtime tm [Fri Nov 16 08:49:26 2018
------------localtime func result-----------
localtime func v_tm->tm_year is [118]
localtime func v_tm->tm_mon is [10]
localtime func v_tm->tm_mday is [16]
localtime func v_tm->tm_hour is [16]
localtime func v_tm->tm_min is [49]
localtime func v_tm->tm_sec is [26]
localtime func v_tm->tm_wday is [5]
localtime func v_tm->tm_yday is [319]
localtime func v_tm->tm_isdst is [0]
asctime func use localtime tm [Fri Nov 16 16:49:26 2018
------------ctime func result-------------
ctime result is [Fri Nov 16 16:49:26 2018
------------mktime func result-----------
mktime result is [1542358166]