新聞中心
求c語言大神指點 gettime函數(shù)的使用 ,用來編輯個時鐘
你的代碼沒有錯,在DOS的C下沒有問題。但DOS的有些時間函數(shù)在C++編譯器下已經(jīng)無效了。可嘗試用下面的——
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供臨海網(wǎng)站建設(shè)、臨海做網(wǎng)站、臨海網(wǎng)站設(shè)計、臨海網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、臨海企業(yè)網(wǎng)站模板建站服務,十年臨海做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務。
#include "stdio.h"
#include "time.h"
#include DOS.H
void main(void){
struct tm *pt;
time_t t;
t=time(NULL);
pt=localtime(t);
printf("The current time is: %2d:%02d:%02d\n",
pt-tm_hour, pt-tm_min, pt-tm_sec);
}
但tm結(jié)構(gòu)沒有ms級變量。
C語言中的gettime()是在哪個頭文件里?
#includeconin.h
#include dos.h
或使用
#include time.h
time_t t;
time(t);
puts(ctime(t));
C語言里time結(jié)構(gòu)體和gettime()函數(shù)包含在哪個頭文件里?
二者均定義在time.h中。
1 在C語言中,為了操作簡單,減少引入頭文件的數(shù)量,相關(guān)功能的類型及函數(shù)均會定義在同一頭文件中,比如輸入輸出相關(guān)的均定義在stdio.h中,而時間相關(guān)的均定義在time.h中。
2 time結(jié)構(gòu)體,即struct time, 是用來存儲時間的結(jié)構(gòu)體。
3 gettime函數(shù),為獲取時間函數(shù),其參數(shù)為struct time *類型。
另外,在不確定是存儲在哪個頭文件,即編程時不確定要引用哪個頭文件時,可以在系統(tǒng)頭文件文件夾中,進行全文搜索,從而得知要需要的頭文件,及對應的使用方式。
請問在C語言里怎么獲取當前時間和日期(精確到毫秒)?
先申明下,這個是我轉(zhuǎn)百度知道的,經(jīng)常BAIDU一下,就OK了
#include stdio.h
#include time.h
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( rawtime );
timeinfo = localtime ( rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include time.h -- 必須的時間函數(shù)頭文件
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時間格式:
星期 月 日 時:分:秒 年
=========================================
你要的格式可這樣輸出:
printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo-tm_year, 1+timeinfo-tm_mon,
timeinfo-tm_mday,timeinfo-tm_hour,timeinfo-tm_min,timeinfo-tm_sec);
就是直接打印tm,tm_year 從1900年計算,所以要加1900,
月tm_mon,從0計算,所以要加1
其它你一目了然啦。
如何用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)是適合用來輸出的。
二、參考代碼:
#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語言如何取得系統(tǒng)時間??
#include "time.h"
time_t time(time_t *timer);
調(diào)用后將當前系統(tǒng)時間與1900年1月1日相差的秒數(shù)存入到timer中,timer可看成是一個長整型數(shù)
struct tm* localtime(const time_t *timer)
將time()函數(shù)調(diào)用的結(jié)果做為參數(shù)傳入到localtime()函數(shù)中就能得到當前時間和日期,注意得到的年是和1970的差值,月份是和1月的差值
struct tm是一個結(jié)構(gòu)體,定義如下
struct tm
{
int tm_sec; //當前秒
int tm_min; //當前分鐘
int tm_hour; //當前小時
int tm_mday; //當前在本月中的天,如11月1日,則為1
int tm_mon; //當前月,范圍是0~11
int tm_year; //當前年和1900的差值,如2006年則為36
int tm_wday; //當前在本星期中的天,范圍0~6
int tm_yday; //當前在本年中的天,范圍0~365
int tm_isdst; //這個我也不清楚
}
求當前時間的示例
int getSystemTime()
{
time_t timer;
struct tm* t_tm;
time(timer);
t_tm = localtime(timer);
printf("today is %4d%02d%02d%02d%02d%02d\n", t_tm.tm_year+1900,
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);
return 0;
}
其他時間的函數(shù)和結(jié)構(gòu)還有:
timeval結(jié)構(gòu)
#include include/linux/time.h
struct timeval
{
time_t tv_sec;
susecond_t tv_usec; //當前妙內(nèi)的微妙數(shù)
};
tms結(jié)構(gòu)
保存著一個進程及其子進程使用的cpu時間
struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
timer_struct結(jié)構(gòu)
#include include/linux/timer.h
struct timer_struct
{
unsigned long expires; //定時器被激活的時刻
void (*fn)(void); //定時器激活后的處理函數(shù)
}
utime函數(shù)
更改文件的存取和修改時間
int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1
times 為空指針,存取和修改時間設(shè)置為當前時間
struct utimbuf
{
time_t actime;
time_t modtime;
}
網(wǎng)站欄目:gettime函數(shù)c語言,c語言datetime
轉(zhuǎn)載來于:http://www.ef60e0e.cn/article/hdpgjo.html