新聞中心
C語言中調(diào)用系統(tǒng)時間
#include?stdio.h?
我們提供的服務(wù)有:網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、饒陽ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的饒陽網(wǎng)站制作公司
#include?time.h?
int?main()
{?
time_t?rawtime;?
struct?tm?*?timeinfo;?
time?(?rawtime?);?
timeinfo?=?localtime?(?rawtime?);?
printf?(?"當前系統(tǒng)時間:?%s",?asctime?(timeinfo)?);?
return?0;
}
說明:
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)為當?shù)貢r間,tm 時間結(jié)構(gòu)?
asctime() // 轉(zhuǎn)為標準ASCII時間格式:?
//就是直接打印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1
C語言如何調(diào)用系統(tǒng)時間
方法一,#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);//轉(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;}
擴展資料
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ù)
C語言獲取系統(tǒng)時間
需要利用C語言的時間函數(shù)time和localtime,具體說明如下:
一、函數(shù)接口介紹:
1、time函數(shù)。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結(jié)構(gòu)體,一般為長整型。
這個函數(shù)會獲取當前時間,并返回。 如果參數(shù)__timer非空,會存儲相同值到__timer指向的內(nèi)存中。
time函數(shù)返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經(jīng)過的秒數(shù),不考慮閏秒。
由于是秒作為單位的,所以這并不是習慣上的時間,要轉(zhuǎn)為習慣上的年月日時間形式就需要另外一個函數(shù)了。
2、localtime函數(shù)。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結(jié)構(gòu)體,包含了年月日時分秒等信息。
這種結(jié)構(gòu)是適合用來輸出的。
如何使用C語言settime函數(shù)?(就是用來設(shè)置系統(tǒng)的時間)
1、函數(shù)名:
settime
功
能:
設(shè)置系統(tǒng)時間
原型:void
settime
2、例程:
#include stdio.h
#include dos.h
int main(void)
{
struct time t;
gettime(t);
printf("The current minute is: %d\n", t.ti_min);
printf("The current hour is: %d\n", t.ti_hour);
printf("The current hundredth of a second is: %d\n", t.ti_hund);
printf("The current second is: %d\n", t.ti_sec);
/* Add one to the minutes struct element and then call settime */
t.ti_min++;
settime(t); //設(shè)置系統(tǒng)時間
return 0;
}
本文名稱:c語言導(dǎo)入系統(tǒng)時間函數(shù)的簡單介紹
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dopojeh.html