商城首页欢迎来到中国正版软件门户

您的位置:首页 > 软件教程 >使用VC编程获取系统时间和程序运行时间的方法

使用VC编程获取系统时间和程序运行时间的方法

  发布于2025-01-28 阅读(0)

扫一扫,手机访问

如何用vc获取系统时间和程序运行时间

如何用vc获取系统时间和程序运行时间

#include

#include

int main()

{

SYSTEMTIME systime; //保存时间的结构体

GetLocalTime(&systime); //本地时间

printf("今天是:");

printf("%d年%d月%d日\n",systime.wYear,systime.wMonth,systime.wDay);

printf("现在时间是:");

printf("%d:%d:%d:%d\n",systime.wHour,systime.wMinute,systime.wSecond,systime.wMilliseconds);

printf("星期:%d\n\n",systime.wDayOfWeek);

GetSystemTime(&systime); //系统时间

printf("现在时间是:");

printf("%d:%d:%d:%d\n",systime.wHour,systime.wMinute,systime.wSecond,systime.wMilliseconds);

return 0;

}//一个Windows环境下取得时间的例子!

在VC中只用的API做一个时钟如何读取和刷新编辑框中的时间值

如果在MFC中

使用

SetTimer(1,1000,NULL);//1000表示1秒为周期

响应WM_TIMER消息

void MyDlg::OnTimer(UINT nIDEvent)

{

//获取当前时间

CTime time = CTime::GetCurrentTime();

//m_Time为控件变量。为控件设置时间。

m_Time.SetWindowText(time.Format("%H:%M:%S"));

CDialog::OnTimer(nIDEvent);

}

如果用win32 API写。

在消息WM_CREATE里

SetTimer (hwnd, ID_TIMER, 1000, NULL);

消息WM_TIMER里

InvalidateRect(hwnd, NULL, FALSE);

用VC获取系统时间几种方法

计算时间差使用double difftime( time_t timer1, time_t timer0 )

2 使用clock_t clock()

得到的是CPU时间

精确到1/CLOCKS_PER_SEC秒

3 使用DWORD GetTickCount() 得到的是系统运行的时间 精确到毫秒

4 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒

5 要获取高精度时间,可以使用BOOLQueryPerformanceFrequency(LARGE_INTEGER

*lpFrequency)获取系统的计数器的频率BOOLQueryPerformanceCounter(LARGE_INTEGER

*lpPerformanceCount)获取计数器的值

然后用两次计数器的差除以Frequency就得到时间。

6 还有David的文章中提到的方法:MultimediaTimer FunctionsThefollowing functions are used with multimedia timers.

timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime

timeGetTime/timeKillEvent/TimeProc/timeSetEvent精度很高Q:GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒?A:

GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022

"timeGetTime函数延时不准"。

GetSystemTime返回的是格林威志标准时间

GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间

VOID GetSystemTime(

LPSYSTEMTIME lpSystemTime // address of system time structure);

函数就可以获得了,其中LPSYSTEMTIME 是个结构体

含:年,月,日,周几,小时,分,秒,毫秒。

本文转载于:https://www.docexcel.net/show/23_142996.html 如有侵犯,请联系admin@zhengruan.com删除

热门关注