国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Linux服務器 > 正文

Linux下用C獲取當前時間

2024-09-05 23:03:33
字體:
來源:轉載
供稿:網友

Linux下用C獲取當前時間,具體如下:

代碼(可以把clock_gettime換成time(NULL))

void getNowTime(){ timespec time; clock_gettime(CLOCK_REALTIME, &time); //獲取相對于1970到現在的秒數 tm nowTime; localtime_r(&time.tv_sec, &nowtime); char current[1024]; sprintf(current, "%04d%02d%02d%02d:%02d:%02d", nowTime.tm_year + 1900, nowTime.tm_mon, nowTime.tm_mday,   nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec);}

分析:

clock_gettime()

 函數"clock_gettime"是基于Linux C語言的時間函數,他可以用于計算精度和納秒。

語法:

#include<time.h>int clock_gettime(clockid_t clk_id,struct timespec *tp);

參數:

clk_id : 檢索和設置的clk_id指定的時鐘時間。

CLOCK_REALTIME:系統實時時間,隨系統實時時間改變而改變,即從UTC1970-1-1 0:0:0開始計時,中間時刻如果系統時間被用戶改成其他,則對應的時間相應改變

  •   CLOCK_MONOTONIC:從系統啟動這一刻起開始計時,不受系統時間被用戶改變的影響
  •   CLOCK_PROCESS_CPUTIME_ID:本進程到當前代碼系統CPU花費的時間
  •   CLOCK_THREAD_CPUTIME_ID:本線程到當前代碼系統CPU花費的時間
struct timespec{time_t tv_sec; /* 秒*/long tv_nsec; /* 納秒*/};

localtime()

localtime是 把從1970-1-1零點零分到當前時間系統所偏移的秒數時間轉換為本地時間.

語法

說明:此函數獲得的tm結構體的時間是日歷時間。

用 法: struct tm *localtime(const time_t *clock);

返回值:返回指向tm 結構體的指針.tm結構體是time.h中定義的用于分別存儲時間的各個量(年月日等)的結構體.

例1:

#include <stdio.h>#include <stddef.h>#include <time.h>int main(void){ time_t timer;//time_t就是long int 類型 struct tm *tblock; timer = time(NULL); tblock = localtime(&timer); printf("Local time is: %s/n", asctime(tblock)); return 0;}

執行結果:

Local time is: Mon Feb 16 11:29:26 2009

例2:

上面的例子用了asctime函數,下面這個例子不使用這個函數一樣能獲取系統當前時間。需要注意的是年份加上1900,月份加上1。

#include<time.h>#include<stdio.h>int main(){ struct tm *t; time_t tt; time(&tt); t = localtime(&tt); 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); return 0;}

localtime()與localtime_r()的區別

localtime():

#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h>  using namespace std;  int main(int argc, char *argv[]) {  time_t tNow =time(NULL);  time_t tEnd = tNow + 1800;  //注意下面兩行的區別  struct tm* ptm = localtime(&tNow);  struct tm* ptmEnd = localtime(&tEnd);   char szTmp[50] = {0};  strftime(szTmp,50,"%H:%M:%S",ptm);  char szEnd[50] = {0};  strftime(szEnd,50,"%H:%M:%S",ptmEnd);     printf("%s /n",szTmp);  printf("%s /n",szEnd);     system("PAUSE");  return EXIT_SUCCESS; }

最后出來的結果是:

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())

#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h>  using namespace std;  int main(int argc, char *argv[]) {  time_t tNow =time(NULL);  time_t tEnd = tNow + 1800;   //在這里修改程序  //struct tm* ptm = localtime(&tNow);  //struct tm* ptmEnd = localtime(&tEnd);  struct tm ptm = { 0 };  struct tm ptmEnd = { 0 };  localtime_r(&tNow, &ptm);  localtime_r(&tEnd, &ptmEnd);    char szTmp[50] = {0};  strftime(szTmp,50,"%H:%M:%S",&ptm);  char szEnd[50] = {0};  strftime(szEnd,50,"%H:%M:%S",&ptmEnd);  printf("%s /n",szTmp);  printf("%s /n",szEnd);     system("PAUSE");  return EXIT_SUCCESS; }

最后出來的結果是:

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 函數

返回:1970-1-1, 00:00:00以來經過的秒數

原型: time_t time(time_t *calptr) 

結果可以通過返回值,也可以通過參數得到,見實例

頭文件 <time.h>

返回值: 

成功:秒數,從1970-1-1,00:00:00 可以當成整型輸出或用于其它函數

失敗:-1

例:

 time_t now; time(&now);// 等同于now = time(NULL) printf("now time is %d/n", now);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安市| 新晃| 邳州市| 尉犁县| 华阴市| 平湖市| 鹤岗市| 荆门市| 阜康市| 右玉县| 寻甸| 东光县| 山阴县| 桦甸市| 墨竹工卡县| 信丰县| 南投市| 丹寨县| 涟源市| 正阳县| 颍上县| 任丘市| 金寨县| 株洲县| 海伦市| 龙州县| 基隆市| 招远市| 道孚县| 中宁县| 景泰县| 华池县| 泸州市| 磐石市| 海晏县| 砀山县| 武汉市| 桑植县| 彝良县| 察隅县| 镇巴县|