⑴ c語言時間處理函數
C語言的標准庫函數包括一系列日期和時間處理函數,它們都在頭文件中說明。
在頭文件中定義了三種類型:time_t,struct tm和clock_t。
下面列出了這些函數。
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);
【具體應用舉例】
asctime(將時間和日期以字元串格式表示)
相關函數
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char * asctime(const struct tm * timeptr);
函數說明
asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。
此函數已經由時區轉換成當地時間,字元串格式為:"Wed Jun 30 21:49:08 1993\n"
返回值
若再調用相關的時間日期函數,此字元串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
附加說明
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
執行
Sat Oct 28 02:10:06 2000
ctime(將時間和日期以字元串格式表示)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char *ctime(const time_t *timep);
函數說明
ctime ()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。
此函數已經由時區轉換成當地時間,字元串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調用相關的時間日期函數,此字元串可能會被破壞。
返回值
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
執行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的時間)
相關函數
time,ctime,ftime,settimeofday
表頭文件
#i nclude
#i nclude
定義函數
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數說明
gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。
timeval結構定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 結構定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時間差了多少分鍾*/
int tz_dsttime; /*日光節約時間的狀態*/
};
上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下
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所指的內存空間超出存取許可權。
範例
#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);
}
執行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前時間和日期)
相關函數
time,asctime,ctime,localtime
表頭文件
#i nclude
定義函數
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 時間
範例
#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);
}
執行
2000/10/28 Sat 8:15:38
localtime(取得當地目前時間和日期)
相關函數
time, asctime, ctime, gmtime
表頭文件
#i nclude
定義函數
struct tm *localtime(const time_t * timep);
函數說明
localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義請參考gmtime()。此函
數返回的時間日期已經轉換成當地時區。
返回值
返回結構tm代表目前的當地時間。
範例
#i nclude
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
mktime(將時間結構數據轉換成經過的秒數)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
範例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#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);
}
執行
time():974943297
time()->localtime()->mktime():974943297
settimeofday(設置目前時間)
相關函數
time,ctime,ftime,gettimeofday
表頭文件
#i nclude
#i nclude
定義函數
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函數說明
settimeofday()會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。
注意,只有root許可權才能使用此函數修改時間。
返回值
成功則返回0,失敗返回-1,錯誤代碼存於errno。
錯誤代碼
EPERM 並非由root許可權調用settimeofday(),許可權不夠。
EINVAL 時區或某個數據是不正確的,無法正確設置時間。
time(取得目前的時間)
相關函數
ctime,ftime,gettimeofday
表頭文件
#i nclude
定義函數
time_t time(time_t *t);
函數說明
此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,
此函數也會將返回值存到t指針所指的內存。
返回值
成功則返回秒數,失敗則返回((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 proced.
%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.
%% %
⑵ c語言 時間函數
c語言時間函數:
1、獲得日歷時間函數:
可以通過time()函數來獲得日歷時間(Calendar Time),其原型為:time_t time(time_t * timer);
如果已經聲明了參數timer,可以從參數timer返回現在的日歷時間,同時也可以通過返回值返回現在的日歷時間,即從一個時間點(例如:1970年1月1日0時0分0秒)到現在此時的秒數。如果參數為空(NUL),函數將只通過返回值返回現在的日歷時間,比如下面這個例子用來顯示當前的日歷時間:
2、獲得日期和時間函數:
這里說的日期和時間就是平時所說的年、月、日、時、分、秒等信息。從第2節我們已經知道這些信息都保存在一個名為tm的結構體中,那麼如何將一個日歷時間保存為一個tm結構的對象呢?
其中可以使用的函數是gmtime()和localtime(),這兩個函數的原型為:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
其中gmtime()函數是將日歷時間轉化為世界標准時間(即格林尼治時間),並返回一個tm結構體來保存這個時間,而localtime()函數是將日歷時間轉化為本地時間。比如現在用gmtime()函數獲得的世界標准時間是2005年7月30日7點18分20秒,那麼用localtime()函數在中國地區獲得的本地時間會比世界標准時間晚8個小時,即2005年7月30日15點18分20秒。
⑶ c語言中time函數怎麼用
頭文件time.h
@函數名稱: localtime
函數原型: struct tm *localtime(const time_t *timer)
函數功能: 返回一個以tm結構表達的機器時間信息
函數返回: 以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;
};
參數說明: timer-使用time()函數獲得的機器時間
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}
@函數名稱: asctime
函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換為ASCII碼)
函數返回: 返回的時間字元串格式為:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到
所屬文件: <time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}
@函數名稱: ctime
函數原型: char *ctime(long time)
函數功能: 得到日歷時間
函數返回: 返回字元串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數time獲得
所屬文件: <time.h>
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}
@函數名稱: difftime
函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位為秒
函數返回: 時間差,單位為秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
所屬文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}
@函數名稱: gmtime
函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息
所屬文件: <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}
@函數名稱: time
函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日歷時間或者設置日歷時間
函數返回: 機器日歷時間
參數說明: timer=NULL時得到機器日歷時間,timer=時間數值時,用於設置日歷時間,time_t是一個long類型
所屬文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}
@函數名稱: tzset
函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
所屬文件: <time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}
⑷ 請問C語言中clock()函數該怎麼用
clock()是C/C++中的計時函數,而與其相關的數據類型是clock_t。
它的具體功能是返回處理器調用某個進程或函數所花費的時間。函數返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數,其中clock_t是用來保存時間的數據類型。
在time.h文件中,我們可以找到對它的定義:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
clock_t其實就是long,即長整形。該函數返回值是硬體滴答數,要換算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK CLOCKS_PER_SEC。比如,在VC++6.0下,這兩個量的值都是1000,這表示硬體滴答1000下是1秒,因此要計算一個進程的時間,用clock()除以1000即可。
clock的返回值一直是0的原因:
1、編譯器優化,for循環實際根本沒執行,直接跳過去了,所以時間為0。
2、clock計算的是程序佔用cpu的時間,如果你的程序執行的動作很少,那麼clock算出的時間也很少。
3、建議使用time gettimeofday函數來計時。
(4)c的時間函數使用方法擴展閱讀:
C語言中clock()函數的程序例1:(TC下運行通過)
#include<stdio.h>
#include<time.h>
intmain(void)
{
clock_tstart,end;
start=clock();
delay(2000);
end=clock();
printf("Thetimewas:%f ",(double)(end-start)/CLK_TCK);
return0;
}
說明:CLK_TCK定義在TC中的time.h中:#defineCLK_TCK18.2。
在VC6.0中也有關於CLK_TCK的宏定義,不過其值不再是18.2,而是1000。
實際上在VC6.0中CLK_TCK已完全等同CLOCKS_PER_SEC。
⑸ c語言時間函數的具體使用方法,時間的加減
#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));
return0;
}
說明:
time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,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 ); //轉為當地時間,tm 時間結構
asctime() // 轉為標准ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1
⑹ C語言的時間函數怎麼調用啊,要把他單獨寫成函數
// 定義一個延時xms毫秒的延時函數void delay(unsigned int xms) // xms代表需要延時的毫秒數{ unsigned int x,y; for(x=xms;x>0;x--) for(y=110;y>0;y--);}