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