C語言如何獲取時間?有多準確?
1使用time_t時間(time_t * timer)到秒。
2使用clock_t clock(),CPU時間精確到1/CLOCKS_PER_SEC秒。
3雙difftime (time _ t timer1,time _ t timer0)用於計算時間差。
4使用DWORD GetTickCount()精確到毫秒。
5如果使用MFC的CTime類,可以使用CTime::GetCurrentTime()精確到秒。
6要獲得高精度時間,可以使用
BOOL QueryPerformanceFrequency(LARGE _ INTEGER * LP frequency)
獲取系統計數器的頻率。
BOOL QueryPerformanceCounter(LARGE _ INTEGER * lpPerformanceCount)
獲取計數器的值
然後用兩個計數器的差值除以頻率得到時間。
7個多媒體定時器功能
以下功能用於多媒體定時器。
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
//*********************************************************************
//用標準c實現獲取當前系統時間的功能。
壹、時間()函數
時間(& ampRawtime)函數獲取當前時間到1970 65438+10月1的秒數,並存儲在rawtime中。
#包含“time.h”
void main()
{
time _ t rawtime
struct tm * timeinfo
時間(& ampraw time);
timeinfo = local time(& amp;raw time);
printf("/007當前日期/時間是:%s ",ASC time(timeinfo));
退出(0);
}
=================
# include-必需的時間函數頭文件
Time_t -時間類型(time.h定義為typedef long time _ t;追根溯源,time_t很長)
結構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
時間(& ampraw time);-采集時間,以秒為單位,從1970 1.1開始計數,存儲在rawtime中。
本地時間(& ampraw time);-轉換為本地時間,tm時間結構
Asctime () -轉換為標準ASCII時間格式:
周、月、日、小時:分鐘:第二年
-
2.clock()函數,利用clock()函數,得到系統啟動後的毫秒時間,然後除以CLOCKS_PER_SEC,可以改成“秒”,標準的C函數。
clock _ t clock(void);
#包括
clock _ t t = clock();
long SEC = t/CLOCKS _ PER _ SEC;
它記錄時鐘周期,實現好像不是很準確,需要實驗驗證;
-
3.gettime(&;t);據說tc2.0的時間結構包含毫秒級信息。
#包括
#包括
int main(void)
{
結構時間t;
gettime(& amp;t);
printf("當前時間是:%2d:%02d:%02d。%02d/n”,
t.ti_hour,t.ti_min,t.ti_sec,t . ti _ hund);
返回0;
}
時間是壹個結構,其中成員函數ti_hund是毫秒。。。
-
GetTickCount(),是windows中常用的計算程序運行時間的函數;
DWORD dw start = GetTickCount();
//在此運行您的程序代碼
DWORD dwEnd = GetTickCount();
然後(dwEnd-dwStart)是程序的運行時間,以毫秒為單位。
這個函數只精確到55ms,1 tick就是55 ms。
-
5.timeGetTime()t,imeGetTime()基本等於GetTickCount(),但精度更高。
DWORD dw start = time gettime();
//在此運行您的程序代碼
DWORD dwEnd = time gettime();
然後(dwEnd-dwStart)是程序的運行時間,以毫秒為單位。
雖然返回值單位應該是ms,但傳說中的精度只有10 ms。
=========================================
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Unix
##unix是時間相關的,也是標準庫。
//*********************************************************************
1.timegm函數只將struct tm結構轉換為time_t結構,不使用時區信息;
time _ t timegm(struct TM * TM);
2.mktime使用時區信息
time _ t mktime(struct TM * TM);
timelocal函數是GNU的擴展,相當於posix函數mktime。
time _ t time local(struct TM * TM);
3.gmtime函數只將time_t結構轉換為struct tm結構,不使用時區信息;
struct TM * GM time(const time _ t * clock);
4.localtime使用時區信息。
struct TM * local time(const time _ t * clock);
1.time獲取時間,stime設置時間。
time _ t t
t =時間。t);
2.2.stime的參數應該是GMT時間,根據當地時區設置為當地時間;
int stime(time_t *tp)
3.UTC=true表示采用夏令時;
4.文件的修改時間等信息都以GMT時間存儲,不同系統在獲取修改時間後通過本地時間轉換為local time
5.建議使用setup來設置時區;
6.您也可以在設置ln-fs/usr/share/zoneinfo/xxxx/XXX/etc/local time之前更改/etc/sysconfig/clock中的設置。
Time_t只能表示68年的範圍,即mktime只能返回1970-2038範圍內的time_t。
看看妳的系統有沒有time_t64,可以代表更大的時間範圍。
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * windows
# #窗口中的# #不同內容。
//*********************************************************************
壹、CTime()類的VC編程壹般使用CTime類獲取當前日期時間。
CTime t = GetCurrentTime();
SYSTEMTIME結構包含毫秒信息。
typedef struct _SYSTEMTIME {
字wYear
WORD wMonth
WORD wDayOfWeek
WORD wDay
單詞wHour
WORD wMinute
WORD wSecond
WORD wMilliseconds
} SYSTEMTIME,* PSYSTEMTIME
system time t 1;
getsystem time(& amp;t1)
CTime curTime(t 1);
WORD ms = t 1 . wmillseconds;
系統時間系統;
* GetLocalTime(& amp;sysTm);
_strtime() // in time.h只能在windows中使用。
char t[11];
_ strtime(t);
賣出(t);
//*****************************
獲取當前日期和時間
CTime TM = CTime::GetCurrentTime();
CString str=tm。格式(" % Y-% m-% d ");
在VC中,我們可以借助CTime時間類得到系統的當前日期。具體使用方法如下:
CTime t = CTime::GetCurrentTime();//獲取系統日期並存儲在t中。
int d = t . GetDay();//獲取當前日期
int y = t . get year();//獲取當前年份
int m = t . get month();//獲取當前月份
int h = t . get hour();//獲取當前時間。
int mm = t . get minute();//獲取當前分鐘
int s = t . get second();//獲取當前秒
int w = t . getday ofweek();//獲取星期幾。註意1是周日,7是周六。
二。CTimeSpan級
如果要計算兩個時間段之間的差異,可以使用CTimeSpan類,如下所示:
CTime t1( 1999,3,19,22,15,0);
CTime t = CTime::GetCurrentTime();
CTimeSpan span = t-t 1;//計算當前系統時間與時間t1的間隔。
int iDay=span。GetDays();//得到這個時間間隔有多少天* * *
int iHour=span。GetTotalHours();//得到總* * *,需要多少個小時?
int iMin=span。gettotal minutes();//得出總* * *,需要多少分鐘?
int iSec=span。GetTotalSeconds();//得到總* * *,需要多少秒?
-
3._timeb()函數
_timeb在SYS/TIMEB中定義。h,有四個字段。
dstflag
millitm
時間
時區
void _ ftime(struct _ timeb * time ptr);
struct _ timeb timebuffer
_ ftime(& amp;time buffer);
就拿現在的時間來說:文件上說可以去ms,也有人測試過。看來只能去16ms了!
第四步:設置計時器
定義計時器ID
#define TIMERID_JISUANFANGSHI 2
將時鐘設置在需要啟動其功能的適當位置;
SetTimer(TIMERID_JISUANFANGSHI,200,NULL);
不需要定時器的時候銷毀時鐘。
kill timer(time rid _ JISUANFANGSHI);
VC程序對應的消息映射
void cji suan::on timer(UINT nIDEvent)
{開關(nIDEvent)}
-
# #如何設置當前系統時間窗口。
SYSTEMTIME m_myLocalTime,* lpSystemTime
m _ my local time . wyear = 2003;
m _ my local time . WM;
m _ my local time . wday = 1;
m _ my local time . whour = 0;
m _ my local time . WM inute = 0;
m _ my local time . wsec;
m _ my local time . wmillisec;
lpSystemTime = & ampm _ myLocalTime
If(設置本地時間(LP系統時間))//這裏改成SetSystemTime()不好。
MessageBox("好的!");
其他
MessageBox("錯誤!");
SYSTEMTIME m_myLocalTime,* lpSystemTime
m _ my local time . wyear = 2003;
m _ my local time . WM;
m _ my local time . wday = 1;
lpSystemTime = & ampm _ myLocalTime
If( SetDate(lpSystemTime)) //這裏改成SetSystemTime()不好。
MessageBox("好的!");
其他
MessageBox("錯誤!");