C語言strdate函數(shù) c++stl常用函數(shù)

如何用C語言獲取當(dāng)前系統(tǒng)時間?

需要利用C語言的時間函數(shù)time和localtime,具體說明如下:

泉州網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

一、函數(shù)接口介紹:

1、time函數(shù)。

形式為time_t time (time_t *__timer);

其中time_t為time.h定義的結(jié)構(gòu)體,一般為長整型。

這個函數(shù)會獲取當(dāng)前時間,并返回。 如果參數(shù)__timer非空,會存儲相同值到__timer指向的內(nèi)存中。

time函數(shù)返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經(jīng)過的秒數(shù),不考慮閏秒。

由于是秒作為單位的,所以這并不是習(xí)慣上的時間,要轉(zhuǎn)為習(xí)慣上的年月日時間形式就需要另外一個函數(shù)了。

2、localtime函數(shù)。

形式為struct tm *localtime (const time_t *__timer);

其中tm為一個結(jié)構(gòu)體,包含了年月日時分秒等信息。

這種結(jié)構(gòu)是適合用來輸出的。

二、參考代碼:

#include?stdio.h

#include?time.h

int?main?()

{

time_t?t;

struct?tm?*?lt;

time?(t);//獲取Unix時間戳。

lt?=?localtime?(t);//轉(zhuǎn)為時間結(jié)構(gòu)。

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);//輸出結(jié)果

return?0;

}

注意事項:

struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt-tm_year+1900。

C語言怎樣返回電腦時間

#include time.h

#includestdio.h

int main()

{

time_t t;

time(t);

puts(ctime(t));

}

C語言的標準庫函數(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

表頭文件

#include

定義函數(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

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

定義函數(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ù)貢r間,字符串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調(diào)用相關(guān)的時間日期函數(shù),此字符串可能會被破壞。

返回值

返回一字符串表示目前當(dāng)?shù)氐臅r間日期。

范例

#include

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

#include

定義函數(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)限。

范例

#include

#include

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

定義函數(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 代表目前分數(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ū)轉(zhuǎn)換,而是UTC時間。

返回值

返回結(jié)構(gòu)tm代表目前UTC 時間

范例

#include

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

定義函數(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

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

定義函數(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

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

#include

定義函數(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)。詳細的說明請參考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

定義函數(shù)

time_t time(time_t *t);

函數(shù)說明

此函數(shù)會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現(xiàn)在所經(jīng)過的秒數(shù)。如果t 并非空指針的話,此函數(shù)也會將返回值存到t指針所指的內(nèi)存。

返回值

成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯誤原因存于errno中。

范例

#include

mian()

{

int seconds= time((time_t*)NULL);

printf("%d\n",seconds);

}

C語言中計算2個時間的差值的函數(shù)

#include?time.h

#include?stdio.h

time_t?_mktime(?char?*slTime?)?/**?yyyy-mm-dd?**/

{

struct?tm?tm_t;

int?year;

int?mon;

int?day;

sscanf(?slTime,?"%4d-%2d-%2d",?year,?mon,?day?);

tm_t.tm_year?=?year?-?1900;

tm_t.tm_mon?=?mon?-?1;

tm_t.tm_mday?=?day;

tm_t.tm_hour?=?12;

tm_t.tm_min?=?00;

tm_t.tm_sec?=?01;

tm_t.tm_wday?=?0;

tm_t.tm_yday?=?0;

tm_t.tm_isdst?=?0;

return?mktime(?tm_t?);

}

int?daydiff(?char?*date1,?char?*date2?)?/**?yyyy-mm-dd?**/

{

time_t?t1?=?_mktime(?date1?);

time_t?t2?=?_mktime(?date2?);

time_t?diff?=?abs(?t2?-?t1?);

return?(int)(?diff?/?(24*60*60)?);

}

int?main()

{

char?date1[12],?date2[12];

printf(?"input?date1:?"?);

scanf(?"%s",?date1?);

fflush(?stdin?);

printf(?"input?date2:?"?);

scanf(?"%s",?date2?);

fflush(?stdin?);

printf(?"%s?-?%s?is?%d?days\n",?date1,?date2,?daydiff(date1,?date2)?);

}

C語言問題 高手進來啊

是取出系統(tǒng)時間嗎?

那就用這個吧,gettime是我自己寫的取系統(tǒng)日期和時間函數(shù),注意調(diào)用的方法.

#include "stdio.h"

int gettime(hh,mm,ss) //取系統(tǒng)時間函數(shù)

int *hh,*mm,*ss;

{FILE *fp;

int s_hh;

int s_mm;

int s_ss;

system("time /tgetthetime.tmp");

fp=fopen("getthetime.tmp","r");

fscanf(fp,"%d-%d-%d",s_hh,s_mm,s_ss);

fclose(fp);

system("del getthetime.tmp");

*hh=s_hh;

*mm=s_mm;

*ss=s_ss;

}

int getdate(yy,mm,dd) //取日期函數(shù)

int *yy,*mm,*dd;

{FILE *fp;

int s_yy;

int s_mm;

int s_dd;

system("date /tgetthetime.tmp");

fp=fopen("getthetime.tmp","r");

fscanf(fp,"%d-%d-%d",s_yy,s_mm,s_dd);

fclose(fp);

system("del getthetime.tmp");

*yy=s_yy;

*mm=s_mm;

*dd=s_dd;

}

int main()

{

int s_yy;

int s_mm;

int s_dd;

getdate(s_yy,s_mm,s_dd); //調(diào)用范例

printf("%d-%d-%d",s_yy,s_mm,s_dd);

getch();

}

C語言中 有沒有函數(shù)可以將字符串直接轉(zhuǎn)為時間格式的?

由于實際生活中,字符串形式的時間有可能有多種形式,比如月日年,或年月日,中間的分隔符也可能有所不同。所以C語言并沒有提供此類的轉(zhuǎn)換函數(shù)。

如果有需求,那么在確定字符串的組織格式前提下,可以自行書寫一個轉(zhuǎn)換函數(shù)。

有兩種思路:

1 傳入字符串,逐位解析每個字符,智能檢查出數(shù)字之間的分隔符。然后根據(jù)分隔符,取出各個位上的數(shù)值,如年月日時分秒等。最終賦值到時間結(jié)構(gòu)的對應(yīng)成員變量上。

2 使用sscanf,根據(jù)約定好的格式,構(gòu)建對應(yīng)的格式字符串,將數(shù)值提取到對應(yīng)的變量中。

對比二者,第一種方式代碼量更大,但可以兼容更復(fù)雜的輸入方式,使得輸入更靈活,程序健壯性更好。第二種方式適用于嚴格約定輸入格式的情況,以最少的代碼量實現(xiàn)效果。

本文名稱:C語言strdate函數(shù) c++stl常用函數(shù)
路徑分享:http://muchs.cn/article28/dosdgjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、商城網(wǎng)站網(wǎng)站制作、App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計