解析Linux下的時(shí)間函數(shù):設(shè)置以及獲取時(shí)間的方法
更新時(shí)間:2013年05月27日 09:24:12 作者:
本篇文章是對(duì)Linux下的時(shí)間函數(shù):設(shè)置以及獲取時(shí)間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
一、時(shí)間函數(shù)
time_t time(time_t *t);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep); //獲取的為英國(guó)時(shí)間
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep); //獲取的為本地時(shí)間,注意與英國(guó)時(shí)間的區(qū)別。
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
二、設(shè)置和獲取時(shí)間
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t1;
time_t t2;
struct tm *my_tm;
char buf[128] = {0};
//自Epoch (00:00:00 UTC, January 1,1970)的秒數(shù)
t1 = time(&t1);
printf("%d\n", t1); //1355905754
t2 = time(&t2);
sleep(1);
printf("%lf\n", difftime(t2, t1)); //t1,t2相差:1.000000,有時(shí)候可以用這個(gè)函數(shù)來(lái)做偽定時(shí)器
printf("%s\n",ctime(&t1)); //Wed Dec 19 16:29:14 2012
//init tm
my_tm->tm_year = 2012-1900;
my_tm->tm_mon = 12-1;
my_tm->tm_mday = 12;
my_tm->tm_hour = 12;
my_tm->tm_min = 12;
my_tm->tm_sec = 12;
//設(shè)置時(shí)間
t1 = mktime(my_tm);
//獲取時(shí)間
my_tm = localtime(&t1);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
my_tm->tm_year + 1900, my_tm->tm_mon + 1, my_tm->tm_mday, my_tm->tm_hour, my_tm->tm_min, my_tm->tm_sec);
printf("%s\n", buf);//2012-12-12 12:12:12
return 0;
}
復(fù)制代碼 代碼如下:
time_t time(time_t *t);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep); //獲取的為英國(guó)時(shí)間
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep); //獲取的為本地時(shí)間,注意與英國(guó)時(shí)間的區(qū)別。
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
二、設(shè)置和獲取時(shí)間
復(fù)制代碼 代碼如下:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t1;
time_t t2;
struct tm *my_tm;
char buf[128] = {0};
//自Epoch (00:00:00 UTC, January 1,1970)的秒數(shù)
t1 = time(&t1);
printf("%d\n", t1); //1355905754
t2 = time(&t2);
sleep(1);
printf("%lf\n", difftime(t2, t1)); //t1,t2相差:1.000000,有時(shí)候可以用這個(gè)函數(shù)來(lái)做偽定時(shí)器
printf("%s\n",ctime(&t1)); //Wed Dec 19 16:29:14 2012
//init tm
my_tm->tm_year = 2012-1900;
my_tm->tm_mon = 12-1;
my_tm->tm_mday = 12;
my_tm->tm_hour = 12;
my_tm->tm_min = 12;
my_tm->tm_sec = 12;
//設(shè)置時(shí)間
t1 = mktime(my_tm);
//獲取時(shí)間
my_tm = localtime(&t1);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
my_tm->tm_year + 1900, my_tm->tm_mon + 1, my_tm->tm_mday, my_tm->tm_hour, my_tm->tm_min, my_tm->tm_sec);
printf("%s\n", buf);//2012-12-12 12:12:12
return 0;
}
您可能感興趣的文章:
- Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)
- Linux下date命令,格式化輸出,時(shí)間設(shè)置方法
- Linux下用C獲取當(dāng)前時(shí)間
- Linux date 時(shí)間設(shè)置同步命令分享
- 設(shè)置Linux系統(tǒng)的空閑等待時(shí)間TMOUT的方法
- 詳解linux ntp服務(wù)器時(shí)間同步設(shè)置
- Linux/Unix關(guān)于時(shí)間和時(shí)間戳的命令行
- 程序中獲取linux系統(tǒng)啟動(dòng)時(shí)間方法
- linux獲取進(jìn)程執(zhí)行時(shí)間方法示例
- Linux時(shí)間子系統(tǒng)之時(shí)間的表示示例詳解
相關(guān)文章
C++數(shù)組放在main函數(shù)內(nèi)外的區(qū)別
大家好,本篇文章主要講的是C++數(shù)組放在main函數(shù)內(nèi)外的區(qū)別,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01C語(yǔ)言中結(jié)構(gòu)體的內(nèi)存對(duì)齊規(guī)則講解
C 數(shù)組允許定義可存儲(chǔ)相同類(lèi)型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是 C 編程中另一種用戶自定義的可用的數(shù)據(jù)類(lèi)型,它允許你存儲(chǔ)不同類(lèi)型的數(shù)據(jù)項(xiàng),本篇讓我們來(lái)了解C 的結(jié)構(gòu)體內(nèi)存對(duì)齊2022-05-05C++11中value category(值類(lèi)別)及move semantics(移動(dòng)語(yǔ)義)的介紹
這篇文章主要給大家介紹了C++11中value category(值類(lèi)別)及move semantics(移動(dòng)語(yǔ)義)的介紹,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05C++獲取字符串長(zhǎng)度的幾個(gè)函數(shù)方式
這篇文章主要介紹了C++獲取字符串長(zhǎng)度的幾個(gè)函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12C語(yǔ)言break和continue的語(yǔ)句用法
這篇文章主要介紹了C語(yǔ)言break和continue的語(yǔ)句用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04c語(yǔ)言實(shí)現(xiàn)詞頻統(tǒng)計(jì)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇c語(yǔ)言實(shí)現(xiàn)詞頻統(tǒng)計(jì)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09