#include stdio.h
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站建設、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的寧鄉(xiāng)網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
#include time.h
int main()
{
time_t timep; //時間變量,從1970年1月1日0時起的秒數(shù)
struct tm * p; //時間結構,含年月日時分秒星期幾,一年中第幾天,夏時制等成員。年從1900起算,月從0起算,...
time(timep); // 獲取當前時間,從1970年1月1日0時起的秒數(shù)
p = gmtime(timep); // 獲取UTC時間 結構成員數(shù)值們
printf("%d %d %d\n",1900+p-tm_year, 1+p-tm_mon, p-tm_mday); //輸出UTC時間的年月日
p = localtime(timep); // 獲取本地 時間 結構成員數(shù)值們
printf("%d %d %d\n",1900+p-tm_year, 1+p-tm_mon, p-tm_mday); //輸出本地時間年月日
return 0;
}
CLOCK()函數(shù):
clock()是C/C++中的計時函數(shù),而與其相關的數(shù)據(jù)類型是clock_t。在MSDN中,查得對clock函數(shù)定義如下:
clock_t
clock(void)
;
這個函數(shù)返回從“開啟這個程序進程”到“程序中調用clock()函數(shù)”時之間的CPU時鐘計時單元(clock
tick)數(shù),在MSDN中稱之為掛鐘時間(wal-clock);若掛鐘時間不可取,則返回-1。其中clock_t是用來保存時間的數(shù)據(jù)類型,在time.h文件中,我們可以找到對它的定義:
#ifndef
_CLOCK_T_DEFINED
typedef
long
clock_t;
#define
_CLOCK_T_DEFINED
#endif
很明顯,clock_t是一個長整形數(shù)。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鐘會有多少個時鐘計時單元,其定義如下:
#define
CLOCKS_PER_SEC
((clock_t)1000)
可以看到每過千分之一秒(1毫秒),調用clock()函數(shù)返回的值就加1。下面舉個例子,你可以使用公式clock()/CLOCKS_PER_SEC來計算一個進程自身的運行時間:
void
elapsed_time()
{
printf("Elapsed
time:%u
secs.\n",clock()/CLOCKS_PER_SEC);
}
當然,你也可以用clock函數(shù)來計算你的機器運行一個循環(huán)或者處理其它事件到底花了多少時間:
#include
stdio.h
#include
stdlib.h
#include
time.h
int
main(void)
{
long
i
=
10000000L;
clock_t
start,
finish;
double
duration;
/*
測量一個事件持續(xù)的時間*/
printf(
"Time
to
do
%ld
empty
loops
is
",
i)
;
start
=
clock();
while(
i--
);
finish
=
clock();
duration
=
(double)(finish
-
start)
/
CLOCKS_PER_SEC;
printf(
"%f
seconds\n",
duration
);
system("pause");
}
在筆者的機器上,運行結果如下:
Time
to
do
10000000
empty
loops
is
0.03000
seconds
上面我們看到時鐘計時單元的長度為1毫秒,那么計時的精度也為1毫秒,那么我們可不可以通過改變CLOCKS_PER_SEC的定義,通過把它定義的大一些,從而使計時精度更高呢?通過嘗試,你會發(fā)現(xiàn)這樣是不行的。在標準C/C++中,最小的計時單位是一毫秒。
time_t
time(
time_t
*timer
);
返回值是1970年到現(xiàn)在的秒數(shù)
用long型接就可以了
參數(shù)也是同樣意義
如
long
time_s
=
0;
time_s
=
time(
NULL
);
//
time_s就是1970年到現(xiàn)在的秒數(shù)
或者
long
*
time_s
=
NULL;
time(time_s);
//
*time_s就是1970年到現(xiàn)在的秒數(shù)
要計算前后一段時間的話之前取一次time,之后取一次相減就知道用了多少秒了
C語言中讀取系統(tǒng)時間的函數(shù)為time(),其函數(shù)原型為:
#include time.h
time_t time( time_t * ) ;
time_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現(xiàn)在的的秒數(shù)。可以調用ctime()函數(shù)進行時間轉換輸出:
char * ctime(const time_t *timer);
將日歷時間轉換成本地時間,按年月日格式,進行輸出,如:
Wed Sep 23 08:43:03 2015
C語言還提供了將秒數(shù)轉換成相應的時間結構的函數(shù):
struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標準時間(即格林尼治時間)
struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間
將通過time()函數(shù)返回的值,轉換成時間結構struct tm :
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; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};
編程者可以根據(jù)程序功能的情況,靈活的進行日期的讀取與輸出了。
例如:
#includetime.h
main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf("%d\n",p-tm_sec); /*獲取當前秒*/
printf("%d\n",p-tm_min); /*獲取當前分*/
printf("%d\n",8+p-tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p-tm_mday);/*獲取當前月份日數(shù),范圍是1-31*/
printf("%d\n",1+p-tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p-tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d\n",p-tm_yday); /*從今年1月1日算起至今的天數(shù),范圍為0-365*/
}
C語言的標準庫函數(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);
【具體應用舉例】
asctime(將時間和日期以字符串格式表示)
相關函數(shù)
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數(shù)
char * asctime(const struct tm * timeptr);
函數(shù)說明
asctime()將參數(shù)timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果以字符串形態(tài)返回。
此函數(shù)已經(jīng)由時區(qū)轉換成當?shù)貢r間,字符串格式為:"Wed Jun 30 21:49:08 1993\n"
返回值
若再調用相關的時間日期函數(shù),此字符串可能會被破壞。此函數(shù)與ctime不同處在于傳入的參數(shù)是不同的結構。
附加說明
返回一字符串表示目前當?shù)氐臅r間日期。
范例
#i nclude
main()
{
time_t timep;
time (timep);
printf("%s",asctime(gmtime(timep)));
}
執(zhí)行
Sat Oct 28 02:10:06 2000
ctime(將時間和日期以字符串格式表示)
相關函數(shù)
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數(shù)
char *ctime(const time_t *timep);
函數(shù)說明
ctime ()將參數(shù)timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果以字符串形態(tài)返回。
此函數(shù)已經(jīng)由時區(qū)轉換成當?shù)貢r間,字符串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調用相關的時間日期函數(shù),此字符串可能會被破壞。
返回值
返回一字符串表示目前當?shù)氐臅r間日期。
范例
#i nclude
main()
{
time_t timep;
time (timep);
printf("%s",ctime(timep));
}
執(zhí)行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的時間)
相關函數(shù)
time,ctime,ftime,settimeofday
表頭文件
#i nclude
#i nclude
定義函數(shù)
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數(shù)說明
gettimeofday()會把目前的時間有tv所指的結構返回,當?shù)貢r區(qū)的信息則放到tz所指的結構中。
timeval結構定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 結構定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時間差了多少分鐘*/
int tz_dsttime; /*日光節(jié)約時間的狀態(tài)*/
};
上述兩個結構都定義在/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所指的內存空間超出存取權限。
范例
#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(取得目前時間和日期)
相關函數(shù)
time,asctime,ctime,localtime
表頭文件
#i nclude
定義函數(shù)
struct tm*gmtime(const time_t*timep);
函數(shù)說明
gmtime()將參數(shù)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 代表目前秒數(shù),正常范圍為0-59,但允許至61秒
int tm_min 代表目前分數(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é)約時間的旗標
此函數(shù)返回的時間日期未經(jīng)時區(qū)轉換,而是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);
}
執(zhí)行
2000/10/28 Sat 8:15:38
localtime(取得當?shù)啬壳皶r間和日期)
相關函數(shù)
time, asctime, ctime, gmtime
表頭文件
#i nclude
定義函數(shù)
struct tm *localtime(const time_t * timep);
函數(shù)說明
localtime()將參數(shù)timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果由結構tm返回。
結構tm的定義請參考gmtime()。此函
數(shù)返回的時間日期已經(jīng)轉換成當?shù)貢r區(qū)。
返回值
返回結構tm代表目前的當?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); /*取得當?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(將時間結構數(shù)據(jù)轉換成經(jīng)過的秒數(shù))
相關函數(shù)
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數(shù)
time_t mktime(strcut tm * timeptr);
函數(shù)說明
mktime()用來將參數(shù)timeptr所指的tm結構數(shù)據(jù)轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經(jīng)過的秒數(shù)。
返回值
返回經(jīng)過的秒數(shù)。
范例
/* 用time()取得時間(秒數(shù)),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數(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ù)
time,ctime,ftime,gettimeofday
表頭文件
#i nclude
#i nclude
定義函數(shù)
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函數(shù)說明
settimeofday()會把目前時間設成由tv所指的結構信息,當?shù)貢r區(qū)信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。
注意,只有root權限才能使用此函數(shù)修改時間。
返回值
成功則返回0,失敗返回-1,錯誤代碼存于errno。
錯誤代碼
EPERM 并非由root權限調用settimeofday(),權限不夠。
EINVAL 時區(qū)或某個數(shù)據(jù)是不正確的,無法正確設置時間。
time(取得目前的時間)
相關函數(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指針所指的內存。
返回值
成功則返回秒數(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.
%% %
方法一,#includetime.h
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf("%d\n",p-tm_sec); /*獲取當前秒*/
printf("%d\n",p-tm_min); /*獲取當前分*/
printf("%d\n",8+p-tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p-tm_mday);/*獲取當前月份日數(shù),范圍是1-31*/
printf("%d\n",1+p-tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p-tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d\n",p-tm_yday); /*從今年1月1日算起至今的天數(shù),范圍為0-365*/
}
方法二.#include?stdio.h
#include?time.h
int?main?()
{
time_t?t
struct?tm?*?lt;????time?(t);//獲取Unix時間戳。
lt?=?localtime?(t);//轉為時間結構。
printf?(?"%d/%d/%d?%d:%d:%d\n",lt-tm_year+1900,?lt-tm_mon,?lt-tm_mday,
lt-tm_hour,?lt-tm_min,?lt-tm_sec);//輸出結果
return?0;}
擴展資料
1、CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統(tǒng)時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數(shù)
_timeb定義在SYS\TIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數(shù)
1、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 ); //轉為當?shù)貢r間,tm 時間結構?
asctime() // 轉為標準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ù) c語言時間函數(shù)計時
網(wǎng)站路徑:http://chinadenli.net/article4/hgdooe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、小程序開發(fā)、網(wǎng)站策劃、、品牌網(wǎng)站設計、Google
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)