代碼(可以把clock_gettime換成time(NULL))
1 void getNowTime() 2 { 3 timespec time; 4 clock_gettime(CLOCK_REALTIME, &time); //獲取相對於1970到現在的秒數 5 tm nowTime; 6 localtime_r(&time.tv_sec, &nowtime); 7 char current[1024]; 8 sprintf(current, "%04d%02d%02d%02d:%02d:%02d", nowTime.tm_year + 1900, nowTime.tm_mon+1, nowTime.tm_mday,
nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec); 9 }
分析:
clock_gettime()
函數"clock_gettime"是基於Linux C語言的時間函數,他可以用於計算精度和納秒。
語法:
#include<time.h>
int
clock_gettime(clockid_t clk_id,
struct
timespec *tp);
CLOCK_MONOTONIC:從系統啟動這一刻起開始計時,不受系統時間被用戶改變的影響
CLOCK_PROCESS_CPUTIME_ID:本進程到當前代碼系統CPU花費的時間
CLOCK_THREAD_CPUTIME_ID:本線程到當前代碼系統CPU花費的時間
1 #include <stdio.h> 2 #include <stddef.h> 3 #include <time.h> 4 int main(void) 5 { 6 time_t timer;//time_t就是long int 類型 7 struct tm *tblock; 8 timer = time(NULL); 9 tblock = localtime(&timer); 10 printf("Local time is: %s\n", asctime(tblock)); 11 return 0; 12 }
例2:
上面的例子用了asctime函數,下面這個例子不使用這個函數一樣能獲取系統當前時間。需要注意的是年份加上1900,月份加上1。
1 #include<time.h> 2 #include<stdio.h> 3 int main() 4 { 5 struct tm *t; 6 time_t tt; 7 time(&tt); 8 t = localtime(&tt); 9 printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); 10 return 0; 11 }
localtime()與localtime_r()的區別
localtime():
1 #include <cstdlib> 2 #include <iostream> 3 #include <time.h> 4 #include <stdio.h> 5 6 using namespace std; 7 8 int main(int argc, char *argv[]) 9 { 10 time_t tNow =time(NULL); 11 time_t tEnd = tNow + 1800; 12 //注意下面兩行的區別 13 struct tm* ptm = localtime(&tNow); 14 struct tm* ptmEnd = localtime(&tEnd); 15 16 char szTmp[50] = {0}; 17 strftime(szTmp,50,"%H:%M:%S",ptm); 18 char szEnd[50] = {0}; 19 strftime(szEnd,50,"%H:%M:%S",ptmEnd); 20 21 22 printf("%s /n",szTmp); 23 printf("%s /n",szEnd); 24 25 26 system("PAUSE"); 27 return EXIT_SUCCESS; 28 }
最后出來的結果是:
21:18:39
21:18:39
和最初想法不一致。
查閱localtime的文檔,發現這段話:
This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.
也就是說每次只能同時使用localtime()函數一次,要不就會被重寫!
The localtime() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.
因此localtime()不是可重入的。同時libc里提供了一個可重入版的函數localtime_r();
Unlike localtime(), the reentrant version is not required to set tzname。
修改程序:(localtime_r())
1 #include <cstdlib> 2 #include <iostream> 3 #include <time.h> 4 #include <stdio.h> 5 6 using namespace std; 7 8 int main(int argc, char *argv[]) 9 { 10 time_t tNow =time(NULL); 11 time_t tEnd = tNow + 1800; 12 13 //在這里修改程序 14 //struct tm* ptm = localtime(&tNow); 15 //struct tm* ptmEnd = localtime(&tEnd); 16 struct tm ptm = { 0 }; 17 struct tm ptmEnd = { 0 }; 18 localtime_r(&tNow, &ptm); 19 localtime_r(&tEnd, &ptmEnd); 20 21 char szTmp[50] = {0}; 22 strftime(szTmp,50,"%H:%M:%S",&ptm); 23 char szEnd[50] = {0}; 24 strftime(szEnd,50,"%H:%M:%S",&ptmEnd); 25 printf("%s /n",szTmp); 26 printf("%s /n",szEnd); 27 28 29 system("PAUSE"); 30 return EXIT_SUCCESS; 31 }
最后出來的結果是:
10:29:06
10:59:06
tm
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日 */
int tm_isdst; /* 夏令時標識符,夏令時tm_isdst為正;不實行夏令時tm_isdst為0 */
};
time
time 函數
返回:1970-1-1, 00:00:00以來經過的秒數
原型: time_t time(time_t *calptr)
結果可以通過返回值,也可以通過參數得到,見實例
頭文件 <time.h>
返回值:
成功:秒數,從1970-1-1,00:00:00 可以當成整型輸出或用於其它函數
失敗:-1
例:
1 time_t now; 2 time(&now);// 等同於now = time(NULL) 3 printf("now time is %d\n", now);
轉載請注明出處:http://www.cnblogs.com/fnlingnzb-learner/p/5985822.html