电竞比分网-中国电竞赛事及体育赛事平台

分享

c語言中時間的獲取

 qinxuanzeng 2010-06-19
c語言中時間的獲取
 
  C語言的標(biāo)準(zhǔn)庫函數(shù)包括一系列日期和時間處理函數(shù),它們都在頭文件中說明。下面列出了這些函數(shù)。在頭文件中定義了三種類型:time_t,struct tm和clock_t。

在頭文件中說明的C語言時間函數(shù)

time_t time(time_t *timer);

double difftime(time_t time1,time_t time2);

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timer);

size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);

time_t mktime(struct tm *timeptr);

clock_t clock(void);
 
   下面是我從網(wǎng)上收集到的時間函數(shù)集

asctime(將時間和日期以字符串格式表示)
相關(guān)函數(shù)
time,ctime,gmtime,localtime
表頭文件
#i nclude  <time.h>
定義函數(shù)
char * asctime(const struct tm * timeptr);
函數(shù)說明
asctime()將參數(shù)timeptr所指的tm結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。此函數(shù)已經(jīng)由時區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r間,字符串格式為:"Wed Jun 30 21:49:08 1993\n"
返回值
若再調(diào)用相關(guān)的時間日期函數(shù),此字符串可能會被破壞。此函數(shù)與ctime不同處在于傳入的參數(shù)是不同的結(jié)構(gòu)。
附加說明
返回一字符串表示目前當(dāng)?shù)氐臅r間日期。
范例
#include  <time.h>
main()

time_t timep; 
time (&timep); 
printf("%s",asctime(gmtime(&timep)));
}
執(zhí)行
Sat Oct 28 02:10:06 2000


ctime(將時間和日期以字符串格式表示)
相關(guān)函數(shù)
time,asctime,gmtime,localtime
表頭文件
#include  <time.h>
定義函數(shù)
char *ctime(const time_t *timep);
函數(shù)說明
ctime ()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。此函數(shù)已經(jīng)由時區(qū)轉(zhuǎn)換成當(dāng)?shù)?時間,字符串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調(diào)用相關(guān)的時間日期函數(shù),此字符串可能會被破壞。
返回值
返回一字符串表示目前當(dāng)?shù)氐臅r間日期。
范例
#include  <time.h>
main()

time_t timep; 
time (&timep); 
printf("%s",ctime(&timep));
}
執(zhí)行
Sat Oct 28 10 : 12 : 05 2000


gettimeofday(取得目前的時間)
相關(guān)函數(shù)
time,ctime,ftime,settimeofday
表頭文件
#include  <time.h>
定義函數(shù)
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數(shù)說明
gettimeofday()會把目前的時間有tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r區(qū)的信息則放到tz所指的結(jié)構(gòu)中。
timeval結(jié)構(gòu)定義為:
struct timeval{ 
long tv_sec; /*秒*/ 
long tv_usec; /*微秒*/
};
timezone 結(jié)構(gòu)定義為: 
struct timezone{ 
int tz_minuteswest; /*和Greenwich 時間差了多少分鐘*/ 
int tz_dsttime; /*日光節(jié)約時間的狀態(tài)*/
};
上述兩個結(jié)構(gòu)都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態(tài)如下
DST_NONE /*不使用*/
DST_USA /*美國*/
DST_AUST /*澳洲*/
DST_WET /*西歐*/
DST_MET /*中歐*/
DST_EET /*東歐*/
DST_CAN /*加拿大*/
DST_GB /*大不列顛*/
DST_RUM /*羅馬尼亞*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值
成功則返回0,失敗返回-1,錯誤代碼存于errno。附加說明EFAULT指針tv和tz所指的內(nèi)存空間超出存取權(quán)限。
范例
#i nclude  <time.h>
main(){ 
struct timeval tv; 
struct timezone tz; 
gettimeofday (&tv , &tz);     
printf("tv_sec; %d\n", tv,.tv_sec) ; 
printf("tv_usec; %d\n",tv.tv_usec); 
printf("tz_minuteswest; %d\n", tz.tz_minuteswest); 
printf("tz_dsttime, %d\n",tz.tz_dsttime);
}
執(zhí)行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0


gmtime(取得目前時間和日期)
相關(guān)函數(shù)
time,asctime,ctime,localtime
表頭文件
#include  <time.h>
定義函數(shù)
struct tm*gmtime(const time_t*timep);
函數(shù)說明
gmtime()將參數(shù)timep 所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。
結(jié)構(gòu)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 代表目前秒數(shù),正常范圍為0-59,但允許至61秒
int tm_min 代表目前分?jǐn)?shù),范圍0-59
int tm_hour 從午夜算起的時數(shù),范圍為0-23
int tm_mday 目前月份的日數(shù),范圍01-31
int tm_mon 代表目前月份,從一月算起,范圍從0-11
int tm_year 從1900 年算起至今的年數(shù)
int tm_wday 一星期的日數(shù),從星期一算起,范圍為0-6
int tm_yday 從今年1月1日算起至今的天數(shù),范圍為0-365
int tm_isdst 日光節(jié)約時間的旗標(biāo)
此函數(shù)返回的時間日期未經(jīng)時區(qū)轉(zhuǎn)換,而是UTC時間。
返回值
返回結(jié)構(gòu)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);
}
執(zhí)行
2000/10/28 Sat 8:15:38


localtime(取得當(dāng)?shù)啬壳皶r間和日期)
相關(guān)函數(shù)
time, asctime, ctime, gmtime
表頭文件
#include  <time.h>
定義函數(shù)
struct tm *localtime(const time_t * timep);
函數(shù)說明
localtime()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。結(jié)構(gòu)tm的定義請參考gmtime()。此函數(shù)返回的時間日期已經(jīng)轉(zhuǎn)換成當(dāng)?shù)貢r區(qū)。
返回值
返回結(jié)構(gòu)tm代表目前的當(dāng)?shù)貢r間。
范例
#include  <time.h>
main(){ 
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
time_t timep; 
struct tm *p; 
time(&timep); 
p=localtime(&timep); /*取得當(dāng)?shù)貢r間*/ 
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);
}
執(zhí)行
2000/10/28 Sat 11:12:22


mktime(將時間結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成經(jīng)過的秒數(shù))
相關(guān)函數(shù)
time,asctime,gmtime,localtime
表頭文件
#include  <time.h>
定義函數(shù)
time_t mktime(strcut tm * timeptr);
函數(shù)說明
mktime()用來將參數(shù)timeptr所指的tm結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經(jīng)過的秒數(shù)。
返回值
返回經(jīng)過的秒數(shù)。
范例
/* 用time()取得時間(秒數(shù)),利用localtime()
轉(zhuǎn)換成struct tm 再利用mktine()將struct tm轉(zhuǎn)換成原來的秒數(shù)*/
#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);
}
執(zhí)行
time():974943297
time()->localtime()->mktime():974943297


settimeofday(設(shè)置目前時間)
相關(guān)函數(shù)
time,ctime,ftime,gettimeofday
表頭文件
#include  <time.h>
定義函數(shù)
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函數(shù)說明
settimeofday()會把目前時間設(shè)成由tv所指的結(jié)構(gòu)信息,當(dāng)?shù)貢r區(qū)信息則設(shè)成tz所指的結(jié)構(gòu)。詳細(xì)的說明請參考gettimeofday()。注意,只有root權(quán)限才能使用此函數(shù)修改時間。
返回值
成功則返回0,失敗返回-1,錯誤代碼存于errno。
錯誤代碼
EPERM 并非由root權(quán)限調(diào)用settimeofday(),權(quán)限不夠。
EINVAL 時區(qū)或某個數(shù)據(jù)是不正確的,無法正確設(shè)置時間。


time(取得目前的時間)
相關(guān)函數(shù)
ctime,ftime,gettimeofday
表頭文件
#include  <time.h>
定義函數(shù)
time_t time(time_t *t);
函數(shù)說明
此函數(shù)會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現(xiàn)在所經(jīng)過的秒數(shù)。如果t 并非空指針的話,此函數(shù)也會將返回值存到t指針?biāo)傅膬?nèi)存。
返回值
成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯誤原因存于errno中。
范例
#include  <time.h>
mian()

int seconds= time((time_t*)NULL); 
printf("%d\n",seconds);
}
執(zhí)行
9.73E+08
------------------------------------------------------------------------------------------------
關(guān)鍵字:c語言 時間函數(shù) time.h c語言時間函數(shù),時間頭函數(shù)
 
 
 
  1,時間的獲?。?/span>
通過time()函數(shù)來獲得日歷時間(Calendar Time),其原型為:time_t time(time_t * timer);
 
#include "stdafx.h"
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
 
int main(void)
{
struct tm t;            //定義tm時間結(jié)構(gòu),用來存儲時間格式的數(shù)據(jù)信息
time_t t_of_day;     //定義time_t時間結(jié)構(gòu)
t.tm_year=2006-1900;//以1900年為標(biāo)準(zhǔn)計算時間
t.tm_mon=6;                 //為結(jié)構(gòu)體成員賦值
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
//使用mktime()函數(shù)將用tm結(jié)構(gòu)表示的時間轉(zhuǎn)化為日歷時間:time_t型變量。其函數(shù)原型如下:time_t mktime(struct tm * timeptr);ctime()函數(shù)(參數(shù)為time_t結(jié)構(gòu))將時間以固定的格式顯示出來,返回值是char*型的字符串。
return 0;
}
 
2,時間的儲存,通過預(yù)定義的兩種結(jié)構(gòu)來存儲:
1,日歷時間(Calendar Time)是通過time_t數(shù)據(jù)類型來表示的,用time_t表示的時間(日歷時間)是從一個時間點(例如:1970年1月1日0時0分0秒)到此時的秒數(shù)。在time.h中,我們也可以看到time_t是一個長整型數(shù):
 
#ifndef _TIME_T_DEFINED
typedef long time_t;         /* 時間值 */
#define _TIME_T_DEFINED      /* 避免重復(fù)定義 time_t */
#endif
2,在標(biāo)準(zhǔn)C/C++中,我們可通過tm結(jié)構(gòu)來獲得日期和時間,tm結(jié)構(gòu)在time.h中的定義如下:
 
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;    /* 年份,其值等于實際年份減去1900 */
        int tm_wday;    /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
        int tm_yday;    /* 從每年的1月1日開始的天數(shù) – 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
        int tm_isdst;   /* 夏令時標(biāo)識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進(jìn)候,tm_isdst為0;不了解情況時,tm_isdst()為負(fù)。*/
        };
 
3,時間的顯示:
time.h頭文件中提供了asctime()函數(shù)(參數(shù)為tm結(jié)構(gòu)指針)和ctime()函數(shù)(參數(shù)為time_t結(jié)構(gòu))將時間以固定的格式 顯示出來,兩者的返回值都是char*型的字符串。返回的時間格式為:星期幾 月份 日期 時:分:秒 年\n\0;time.h還提供了兩種不同的函數(shù)將日歷時間(一個用time_t表示的整數(shù))轉(zhuǎn)換為我們平時看到的把年月日時分秒分開顯示的時間格式 tm:
struct tm * gmtime(const time_t *timer);
gmtime()函數(shù)是將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),并返回一個tm結(jié)構(gòu)體來保存這個時間
struct tm * localtime(const time_t * timer);localtime()函數(shù)是將日歷時間轉(zhuǎn)化為本地時間
#include <stdafx.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
       struct tm *local,*ptr; //定義tm結(jié)構(gòu)指針存儲時間信息
       time_t t;                       //時間結(jié)構(gòu)或者對象
       t=time(NULL);                     //獲取當(dāng)前系統(tǒng)的日歷時間
       //通過time()函數(shù)來獲得日歷時間(Calendar Time),
       //其原型為:time_t time(time_t * timer);
       local=localtime(&t);//localtime()函數(shù)是將日歷時間轉(zhuǎn)化為本地時間
       printf("Local hour is: %d\n",local->tm_hour);//輸出tm結(jié)構(gòu)體的時間成員
       printf("UTC hour is: %d\n",local->tm_hour);
       //local=gmtime(&t);
       //gmtime()函數(shù)是將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),
       //并返回一個tm結(jié)構(gòu)體來保存這個時間  
       ptr=gmtime(&t);//將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間
       printf("The UTC time is %s\n",asctime(ptr)); //格式化輸出世界標(biāo)準(zhǔn)時間
       printf("The local time is %s\n",ctime(&t));//輸出本地時間
       /*asctime()函數(shù)(參數(shù)為tm結(jié)構(gòu)指針)和ctime()函數(shù)(參數(shù)為time_t結(jié)構(gòu))將時間以固定的格式顯示出來,兩者的返回值都是char*型的字符串。返回的時間格式為:星期幾 月份 日期 時:分:秒 年\n\0 */
       return 0;
}
 
4,時間差的計算:
所用函數(shù):C/C++中的計時函數(shù)是clock(),而與其相關(guān)的數(shù)據(jù)類型是clock_t。在MSDN中對clock函數(shù)定義如下:
clock_t clock( void );函數(shù)返回從“開啟這個程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時之間的CPU時鐘計時單元(clock tick)數(shù),clock_t是一個長整形數(shù),保存時間的數(shù)據(jù)類型。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一 秒鐘會有多少個時鐘計時單元,其定義如下:
#define CLOCKS_PER_SEC ((clock_t)1000)
每過千分之一秒(1毫秒),調(diào)用clock()函數(shù)返回的值就加1,時鐘計時單元的長度為1毫秒,那么計時的精度也為1毫秒,那么我們可不可以 通過改變CLOCKS_PER_SEC的定義,通過把它定義的大一些,從而使計時精度更高呢?這樣是不行的。在標(biāo)準(zhǔn)C/C++中,最小的計時單位是一毫 秒。double difftime(time_t time1, time_t time0);這個函數(shù)來計算時間差。
 
#include "stdafx.h"
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
    time_t c_start,t_start, c_end,t_end;  
    c_start = clock();
       t_start = time(NULL) ;
    system("pause") ;
       c_end = clock();
    t_end = time(NULL) ;
    printf("The pause used %f ms by time().\n",difftime(c_end,c_start)) ; 
       printf("The pause used %f s by clock().\n",difftime(t_end,t_start)) ;
    system("pause");
    return 0;
}
5,時間的其他用途
用作隨機(jī)數(shù)的種子,由于時間獲得的實際上是一個double類型的長整數(shù),通過time(NULL)函數(shù)獲得,作為srand(time(NULL))的種子產(chǎn)生隨機(jī)數(shù)比較好。
 
#include "stdafx.h"
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
       srand(time(NULL));
       //設(shè)置種子,如果將這個函數(shù)注釋掉,每次運行程序得到的隨機(jī)數(shù)十相同的
       for(int i=0;i<100;i++)
       {
              printf("%d\t",rand());
       }
    system("pause");
    return 0;
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多