歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux獲取時間函數及計算時間差

Linux獲取時間函數及計算時間差

日期:2017/3/1 9:36:16   编辑:Linux編程

第一章 獲取時間函數

1. char * asctime(const struct tm * timeptr);

函數說明
asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式為:“Wed Jun 30 21:49:08 1993\n”

返回值
若再調用相關的時間日期函數,此字符串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。

附加說明
返回一字符串表示目前當地的時間日期。

范例
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}

執行
Sat Oct 28 02:10:06 2000

Linux函數庫操作相關 http://www.linuxidc.com/Linux/2010-09/28768.htm

2. 定義函數
char *ctime(const time_t *timep);

函數說明
ctime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式為“Wed Jun 30 21 :49 :08 1993\n”。若再調用相關的時間日期函數,此字符串可能會被破壞。

返回值
返回一字符串表示目前當地的時間日期。

范例
#include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}

執行
Sat Oct 28 10 : 12 : 05 2000

3. struct tm*gmtime(const time_t*timep);

函數說明
gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義為
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時間。

返回值
返回結構tm代表目前UTC 時間

范例
#include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}

執行
2000/10/28 Sat 8:15:38

4. struct tm *localtime(const time_t * timep);

函數說明
localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。結構tm的定義請參考gmtime()。此函數返回的時間日期已經轉換成當地時區。

返回值
返回結構tm代表目前的當地時間。

范例
#include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得當地時間*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}

執行
2000/10/28 Sat 11:12:22


5. time_t mktime(strcut tm * timeptr);

函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。

返回值
返回經過的秒數。

范例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}

執行
time():974943297
time()->localtime()->mktime():974943297


6. time_t time(time_t *t);

函數說明
此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,此函數也會將返回值存到t指針所指的內存。

返回值
成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存於errno中。

范例
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}


7. gettimeofday() :可獲得微妙級(0.000001秒)的系統時間,調用兩次gettimeofday(),前後做減法,從而達到定時或者計算時間的目的。


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); //獲取的為英國時間

struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep); //獲取的為本地時間,注意與英國時間的區別。

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

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-12/110499p2.htm

Copyright © Linux教程網 All Rights Reserved