1、C語言中讀取系統(tǒng)時間的函數(shù)為time(),其函數(shù)原型為:\x0d\x0a#include \x0d\x0atime_t time( time_t * ) ;\x0d\x0atime_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現(xiàn)在的的秒數(shù)。\x0d\x0a2、C語言還提供了將秒數(shù)轉(zhuǎn)換成相應(yīng)的時間格式的函數(shù):\x0d\x0a char * ctime(const time_t *timer); //將日歷時間轉(zhuǎn)換成本地時間,返回轉(zhuǎn)換后的字符串指針 可定義字符串或是字符指針來接收返回值\x0d\x0a struct tm * gmtime(const time_t *timer); //將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a struct tm * localtime(const time_t * timer); //將日歷時間轉(zhuǎn)化為本地時間,返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a3、例程:\x0d\x0a#include \x0d\x0avoid main()\x0d\x0a{\x0d\x0a time_t t;\x0d\x0a struct tm *pt ;\x0d\x0a char *pc ;\x0d\x0a time(t);\x0d\x0a pc=ctime(t) ; printf("ctime:%s", pc );\x0d\x0a pt=localtime(t) ; printf("year=%d", pt-tm_year+1900 );\x0d\x0a}\x0d\x0a\x0d\x0a時間結(jié)構(gòu)體struct tm 說明:\x0d\x0a\x0d\x0astruct tm { \x0d\x0a int tm_sec; /* 秒 _ 取值區(qū)間為[0,59] */ \x0d\x0a int tm_min; /* 分 - 取值區(qū)間為[0,59] */ \x0d\x0a int tm_hour; /* 時 - 取值區(qū)間為[0,23] */ \x0d\x0a int tm_mday; /* 一個月中的日期 - 取值區(qū)間為[1,31] */ \x0d\x0a int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */ \x0d\x0a int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */ \x0d\x0a int tm_wday; /* 星期 _ 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */ \x0d\x0a int tm_yday; /* 從每年的1月1日開始的天數(shù) _ 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */ \x0d\x0a int tm_isdst; /* 夏令時標(biāo)識符,實(shí)行夏令時的時候,tm_isdst為正。不實(shí)行夏令時的進(jìn)候,tm_isdst為0;不了解情況時,tm_isdst()為負(fù)。*/ \x0d\x0a};
目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計、婺源網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
C語言的標(biāo)準(zhǔn)庫函數(shù)包括一系列日期和時間處理函數(shù),它們都在頭文件中說明。
在頭文件中定義了三種類型:time_t,struct tm和clock_t。
下面列出了這些函數(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);
【具體應(yīng)用舉例】
asctime(將時間和日期以字符串格式表示)
相關(guān)函數(shù)
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數(shù)
char * asctime(const struct tm * timeptr);
函數(shù)說明
asctime()將參數(shù)timeptr所指的tm結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(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間日期。
范例
#i nclude
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
表頭文件
#i nclude
定義函數(shù)
char *ctime(const time_t *timep);
函數(shù)說明
ctime ()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(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ù),此字符串可能會被破壞。
返回值
返回一字符串表示目前當(dāng)?shù)氐臅r間日期。
范例
#i nclude
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
表頭文件
#i nclude
#i nclude
定義函數(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
#i nclude
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
表頭文件
#i nclude
定義函數(shù)
struct tm*gmtime(const time_t*timep);
函數(shù)說明
gmtime()將參數(shù)timep 所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(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 時間
范例
#i nclude
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
表頭文件
#i nclude
定義函數(shù)
struct tm *localtime(const time_t * timep);
函數(shù)說明
localtime()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(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間。
范例
#i nclude
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
表頭文件
#i nclude
定義函數(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ù)*/
#i nclude
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
表頭文件
#i nclude
#i nclude
定義函數(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
表頭文件
#i nclude
定義函數(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中。
范例
#i nclude
mian()
{
int seconds= time((time_t*)NULL);
printf("%d\n",seconds);
}
Date and Time Functions: time.h
The header time.h declares types and functions for manipulating date and time. Some functions process local time,
which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types
representing times, and struct tm holds the components
of a calendar time:
int tm_sec; seconds after the minute (0,61)
int tm_min; minutes after the hour (0,59)
int tm_hour; hours since midnight (0,23)
int tm_mday; day of the month (1,31)
int tm_mon; months since January (0,11)
int tm_year; years since 1900
int tm_wday; days since Sunday (0,6)
int tm_yday; days since January 1 (0,365)
int tm_isdst; Daylight Saving Time flag
tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.
clock_t clock(void)
clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.
clock()/CLK_PER_SEC is a time in
seconds.
time_t time(time_t *tp)
time returns the current calendar time or -1 if the time is not available. If tp is not NULL,
the return value is also assigned to *tp.
double difftime(time_t time2, time_t time1)
difftime returns time2-time1 expressed in seconds.
time_t mktime(struct tm *tp)
mktime converts the local time in the structure *tp into calendar time in the same representation used by time.
The components will have values
in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.
The next four functions return pointers to static objects that may be overwritten by other calls.
char *asctime(const struct tm *tp)
asctime*tp into a string of the form
Sun Jan 3 15:14:13 1988\n\0
char *ctime(const time_t *tp)
ctime converts the calendar time *tp to local time; it is equivalent to
asctime(localtime(tp))
struct tm *gmtime(const time_t *tp)
gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.
The name gmtime has historical significance.
struct tm *localtime(const time_t *tp)
localtime converts the calendar time *tp into local time.
size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.
Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below,
using values appropriate for the local environment.
No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0',
or zero if more than smax characters were produced.
%a abbreviated weekday name.
%A full weekday name.
%b abbreviated month name.
%B full month name.
%c local date and time representation.
%d day of the month (01-31).
%H hour (24-hour clock) (00-23).
%I hour (12-hour clock) (01-12).
%j day of the year (001-366).
%m month (01-12).
%M minute (00-59).
%p local equivalent of AM or PM.
%S second (00-61).
%U week number of the year (Sunday as 1st day of week) (00-53).
%w weekday (0-6, Sunday is 0).
%W week number of the year (Monday as 1st day of week) (00-53).
%x local date representation.
%X local time representation.
%y year without century (00-99).
%Y year with century.
%Z time zone name, if any.
%% %
time_t nowtime; -- 聲明變量 nowtime(現(xiàn)在時間) 為 time_t 型
struct tm *timeinfo; -- 聲明變量timeinfo(時間信息)為 tm 型 結(jié)構(gòu) 指針。
time_t , tm 都是 time.h 頭文件里定義 的 類型。
time( nowtime ); -- 調(diào)系統(tǒng)函數(shù) time(), 獲得 現(xiàn)在時間 (1970年起多少個“滴答”,世界標(biāo)準(zhǔn)時間)
timeinfo = localtime( nowtime ); -- 調(diào)系統(tǒng)函數(shù), 獲得 當(dāng)?shù)?現(xiàn)在時間 (例如 東8 區(qū),北京時間)。時間數(shù)據(jù)是 tm 型 結(jié)構(gòu)。
int hour; -- 聲明變量 hour (小時),整型。
hour = timeinfo-tm_hour+1 ; -- 結(jié)構(gòu) timeinfo的成員tm_hour 是時間值,+1 得 hour(小時)。
tm_hour -- 數(shù)值范圍 0-23。
1、time_t // 時間類型(time.h 定義)?
struct tm { // 時間結(jié)構(gòu),time.h 定義如下:?
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;?
}?
time ( rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存于rawtime?
localtime ( rawtime ); //轉(zhuǎn)為當(dāng)?shù)貢r間,tm 時間結(jié)構(gòu)?
asctime() // 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時間格式:?
//就是直接打印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1
2、time函數(shù)使用示例
#include?stdio.h??
#include?time.h????
int?main()
{??
time_t?rawtime;??
struct?tm?*?timeinfo;??
time?(?rawtime?);??
timeinfo?=?localtime?(?rawtime?);??
printf?(?"The?current?date/time?is:?%s",?asctime?(timeinfo)?);??
return?0;
}
c語言時間函數(shù):
1、獲得日歷時間函數(shù):
可以通過time()函數(shù)來獲得日歷時間(Calendar Time),其原型為:time_t time(time_t * timer);
如果已經(jīng)聲明了參數(shù)timer,可以從參數(shù)timer返回現(xiàn)在的日歷時間,同時也可以通過返回值返回現(xiàn)在的日歷時間,即從一個時間點(diǎn)(例如:1970年1月1日0時0分0秒)到現(xiàn)在此時的秒數(shù)。如果參數(shù)為空(NUL),函數(shù)將只通過返回值返回現(xiàn)在的日歷時間,比如下面這個例子用來顯示當(dāng)前的日歷時間:
2、獲得日期和時間函數(shù):
這里說的日期和時間就是平時所說的年、月、日、時、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個名為tm的結(jié)構(gòu)體中,那么如何將一個日歷時間保存為一個tm結(jié)構(gòu)的對象呢?
其中可以使用的函數(shù)是gmtime()和localtime(),這兩個函數(shù)的原型為:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
其中g(shù)mtime()函數(shù)是將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),并返回一個tm結(jié)構(gòu)體來保存這個時間,而localtime()函數(shù)是將日歷時間轉(zhuǎn)化為本地時間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時間是2005年7月30日7點(diǎn)18分20秒,那么用localtime()函數(shù)在中國地區(qū)獲得的本地時間會比世界標(biāo)準(zhǔn)時間晚8個小時,即2005年7月30日15點(diǎn)18分20秒。
C語言的建時間函數(shù)是 mktime(),原型在 time.h 里
調(diào)用有點(diǎn)繁。
下面,用我的程序輸入 年月日時分秒,調(diào)用mktime(), 就得 C語言 可直接使用的 時間, 存放在 t 里。
例如 輸入年月日時分秒: 2008 8 16 9 55 25
time_t t; 里 就有了 各種時間信息,例如星期幾...
#include stdio.h
#include time.h
void main(){
struct tm *target_time;
time_t rawtime, t;
int year,month,mday,hh,mm,ss;
time ( rawtime );
target_time = localtime ( rawtime );
printf("Please enter year month day hour minute second\n");
printf("For example: \n");
printf("2008 8 16 9 55 25\n");
scanf("%d %d %d %d %d %d", year, month, mday, hh,mm,ss);
target_time-tm_year = year - 1900;
target_time-tm_mon= month - 1;
target_time-tm_mday = mday ;
target_time-tm_hour = hh ;
target_time-tm_min = mm ;
target_time-tm_sec = ss ;
//
t = mktime (target_time);
// t is ready to use
printf("%s ",ctime(t));
}