c语言时间算法如何以毫秒显示,C语言时间的方法.doc
C語言時間的方法.doc
C語言獲取系統時間的幾種方式
—源于網絡
C語言中如何獲取時間?精度如何?
1 使用time_t time( time_t * timer ) 精確到秒
2 使用clock_t clock() 得到的是CPU時間精確到1/CLOCKS_PER_SEC秒
3 計算時間差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精確到毫秒
5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒
6 要獲取高精度時間,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
獲取系統的計數器的頻率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
獲取計數器的值
然后用兩次計數器的差除以Frequency就得到時間。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
//*********************************************************************
//用標準C實現獲取當前系統時間的函數
?
一.time()函數
???? time(&rawtime)函數獲取當前時間距1970年1月1日的秒數,以秒計數單位,存于rawtime 中。
#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
time_t -- 時間類型(time.h 定義是typedef long time_t; 追根溯源,time_t是long)
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月1日起算,存于rawtime
localtime ( &rawtime ); -- 轉為當地時間,tm 時間結構
asctime ( )-- 轉為標準ASCII時間格式:
星期 月 日 時:分:秒 年
?
-----------------------------------------------------------------------------
二.clock()函數, clock()函數返回程序開始執行起處理器所消耗的時間(毫秒級),然后除以CLOCKS_PER_SEC,就可以換成“秒”,標準c函數。
clock_t clock ( void );
#include
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是記錄時鐘周期的,實現看來不會很精確,需要試驗驗證。
---------------------------------------------------------------------------
三.gettime(&t); 據說tc2.0的time結構含有毫秒信息
#include
#include
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: -:d:d.d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
time 是一個結構體,, 其中成員函數 ti_hund 是毫秒…
?說明:這個在vc++6.0不行的。
-------------------
總結
以上是生活随笔為你收集整理的c语言时间算法如何以毫秒显示,C语言时间的方法.doc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言给bmp图片加滤镜,关于BMP位图
- 下一篇: android root工具twrp,安