最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Linux下用C獲取當(dāng)前時間

 更新時間:2017年01月24日 09:17:42   作者:Boblim  
本篇文章主要介紹了Linux獲取當(dāng)前時間的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Linux下用C獲取當(dāng)前時間,具體如下:

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

void getNowTime()
{
 timespec time;
 clock_gettime(CLOCK_REALTIME, &time); //獲取相對于1970到現(xiàn)在的秒數(shù)
 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()

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

語法:

#include<time.h>

int clock_gettime(clockid_t clk_id,struct timespec *tp);

參數(shù):

clk_id : 檢索和設(shè)置的clk_id指定的時鐘時間。

CLOCK_REALTIME:系統(tǒng)實(shí)時時間,隨系統(tǒng)實(shí)時時間改變而改變,即從UTC1970-1-1 0:0:0開始計(jì)時,中間時刻如果系統(tǒng)時間被用戶改成其他,則對應(yīng)的時間相應(yīng)改變

  •   CLOCK_MONOTONIC:從系統(tǒng)啟動這一刻起開始計(jì)時,不受系統(tǒng)時間被用戶改變的影響
  •   CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時間
  •   CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時間
struct timespec

{

time_t tv_sec; /* 秒*/

long tv_nsec; /* 納秒*/

};

localtime()

localtime是 把從1970-1-1零點(diǎn)零分到當(dāng)前時間系統(tǒng)所偏移的秒數(shù)時間轉(zhuǎn)換為本地時間.

語法

說明:此函數(shù)獲得的tm結(jié)構(gòu)體的時間是日歷時間。

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

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

例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;
}

執(zhí)行結(jié)果:

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

例2:

上面的例子用了asctime函數(shù),下面這個例子不使用這個函數(shù)一樣能獲取系統(tǒng)當(dāng)前時間。需要注意的是年份加上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()的區(qū)別

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; 
 //注意下面兩行的區(qū)別 
 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; 
}

最后出來的結(jié)果是:

21:18:39

21:18:39

和最初想法不一致。

查閱localtime的文檔,發(fā)現(xiàn)這段話:

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()函數(shù)一次,要不就會被重寫!

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里提供了一個可重入版的函數(shù)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; 
}

最后出來的結(jié)果是:

10:29:06
10:59:06

tm

 struct tm {
     int tm_sec;  /* 秒 – 取值區(qū)間為[0,59] */ 
     int tm_min;  /* 分 - 取值區(qū)間為[0,59] */ 
     int tm_hour;  /* 時 - 取值區(qū)間為[0,23] */ 
     int tm_mday;  /* 一個月中的日期 - 取值區(qū)間為[1,31] */ 
     int tm_mon;  /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */ 
     int tm_year;  /* 年份,其值等于實(shí)際年份減去1900 */ 
     int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一 */ 
     int tm_yday; /* 從每年1月1日開始的天數(shù)– 取值區(qū)間[0,365],其中0代表1月1日 */ 
     int tm_isdst; /* 夏令時標(biāo)識符,夏令時tm_isdst為正;不實(shí)行夏令時tm_isdst為0 */ 
   };

time 函數(shù)

返回:1970-1-1, 00:00:00以來經(jīng)過的秒數(shù)

原型: time_t time(time_t *calptr) 

結(jié)果可以通過返回值,也可以通過參數(shù)得到,見實(shí)例

頭文件 <time.h>

返回值: 

成功:秒數(shù),從1970-1-1,00:00:00 可以當(dāng)成整型輸出或用于其它函數(shù)

失?。?1

例:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Linux模擬網(wǎng)絡(luò)丟包與延遲的方法

    Linux模擬網(wǎng)絡(luò)丟包與延遲的方法

    這篇文章主要給大家介紹了關(guān)于Linux模擬網(wǎng)絡(luò)丟包與延遲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Linux具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • linux防火墻如何查看狀態(tài)firewall

    linux防火墻如何查看狀態(tài)firewall

    這篇文章主要介紹了linux防火墻如何查看狀態(tài)firewall問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 火山互聯(lián)linux VPS配置方法

    火山互聯(lián)linux VPS配置方法

    火山互聯(lián)推出的免費(fèi)VPS...拿來學(xué)習(xí)下還是不錯的.申請過幾次Liunx系統(tǒng)的.由于不是很熟悉.幾次都沒能配置好.下面介紹一下.前幾天又申請了一個.
    2009-12-12
  • linux下安裝配置svn獨(dú)立服務(wù)器的步驟分享

    linux下安裝配置svn獨(dú)立服務(wù)器的步驟分享

    subversion(以下簡稱svn)是近年來崛起的版本管理工具,是cvs的接班人。 svn服務(wù)器有2種運(yùn)行方式:獨(dú)立服務(wù)器和借助apache。2種方式各有利弊
    2011-05-05
  • Ubuntu系統(tǒng)查看進(jìn)程被kill的命令和方法

    Ubuntu系統(tǒng)查看進(jìn)程被kill的命令和方法

    這篇文章主要介紹了Ubuntu查看進(jìn)程被kill的幾種方式,在Ubuntu系統(tǒng)中,要查看進(jìn)程被殺掉的記錄,可以使用文中命令和方法,文章通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Linux和window環(huán)境下開放防火墻端口的操作

    Linux和window環(huán)境下開放防火墻端口的操作

    開發(fā)時會遇到在服務(wù)器上部署mysql或者es數(shù)據(jù)庫的情況,此時如果訪問數(shù)據(jù)庫就需要開放防火墻端口,本文會講解windows和linux環(huán)境下端口的開放,文章通過圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • Centos7.5配置java環(huán)境安裝tomcat的講解

    Centos7.5配置java環(huán)境安裝tomcat的講解

    今天小編就為大家分享一篇關(guān)于Centos7.5配置java環(huán)境安裝tomcat的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Linux磁盤空間釋放問題整理

    Linux磁盤空間釋放問題整理

    在本篇文章里小編給大家分享了一篇關(guān)于Linux磁盤空間釋放問題整理內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2020-12-12
  • Linux部署jar包過程

    Linux部署jar包過程

    文章介紹了在Linux系統(tǒng)上部署Java(jar)包時需要注意的幾個關(guān)鍵點(diǎn),包括統(tǒng)一JDK版本、添加打包插件、修改數(shù)據(jù)庫密碼以及正確執(zhí)行jar包的方法
    2025-02-02
  • apache rewrite規(guī)則實(shí)現(xiàn)白名單

    apache rewrite規(guī)則實(shí)現(xiàn)白名單

    今天弄了半天apache 的rewrite 規(guī)則,還是沒有配置出符合的規(guī)則。后一同事,研究了半個小時弄出來,很是慚愧。
    2014-06-06

最新評論

中江县| 金华市| 工布江达县| 喀什市| 静乐县| 芦山县| 景宁| 惠来县| 浦县| 交口县| 建阳市| 深水埗区| 安西县| 新巴尔虎左旗| 绍兴县| 富锦市| 兰溪市| 定州市| 利津县| 奉化市| 呼图壁县| 榆中县| 元谋县| 云南省| 贡山| 满城县| 铜陵市| 哈巴河县| 建湖县| 隆昌县| 安庆市| 沾化县| 精河县| 桐柏县| 永济市| 海盐县| 通河县| 依安县| 兰坪| 汉寿县| 海原县|