VC++获取当前系统时间

r囧r小猫 2022-09-20 13:00 420阅读 0赞
  1. VC++获取当前系统时间日期 GetSystemTime()函数的用法

  2. 一、使用MFC可以用以下代码得到:

  3. CTime time = CTime::GetCurrentTime(); ///构造CTime对象
  4. int m_nYear = time.GetYear(); ///年
  5. int m_nMonth = time.GetMonth(); ///月
  6. int m_nDay = time.GetDay(); ///日
  7. int m_nHour = time.GetHour(); ///小时
  8. int m_nMinute = time.GetMinute(); ///分钟
  9. int m_nSecond = time.GetSecond(); ///秒
  10. // CTime time = CTime::GetCurrentTime();
  11. //CString strTime = time.Format(“%Y-%m-%d %H:%M:%S”); //Format(“%I:%M:%S %p, %A, %B %d, %Y”)
  12. 我们还可以用CTime::Format函数将CTime对象转换为字符串对象
  13. 例如:
  14. CString m_strTime = time.Format(“%Y-%m-%d %H:%M:%S”);
  15. 运行结果:m_strTime为 2001-8-1 12:11:05
  16. 函数GetSystemTime和GetLocalTime声明如下:
  17. WINBASEAPI
  18. VOID
  19. WINAPI
  20. GetSystemTime(
  21. __out LPSYSTEMTIME lpSystemTime
  22. );
  23. WINBASEAPI
  24. VOID
  25. WINAPI
  26. GetLocalTime(
  27. __out LPSYSTEMTIME lpSystemTime
  28. );
  29. lpSystemTime是获取系统时间的结构。
  30. 调用函数的例子如下:
  31. //获取系统时间。
  32. //蔡军生 2007/11/11 QQ:9073204 深圳
  33. void TestSystem(void)
  34. {
  35. //获取系统的UTC时间。
  36. SYSTEMTIME stUTC;
  37. ::GetSystemTime(&stUTC);
  38. //显示时间的间隔。
  39. const**int** nBufSize = 256;
  40. TCHAR chBuf[nBufSize];
  41. wsprintf(chBuf,_T(“UTC: %u/%u/%u %u:%u:%u:%u %d\r\n”),
  42. stUTC.wYear, stUTC.wMonth, stUTC.wDay,
  43. stUTC.wHour, stUTC.wMinute, stUTC.wSecond,
  44. stUTC.wMilliseconds,stUTC.wDayOfWeek);
  45. OutputDebugString(chBuf);
  46. //获取当地的时间。
  47. SYSTEMTIME stLocal;
  48. ::GetLocalTime(&stLocal);
  49. //显示时间的间隔。
  50. wsprintf(chBuf,_T(“Local: %u/%u/%u %u:%u:%u:%u %d\r\n”),
  51. stLocal.wYear, stLocal.wMonth, stLocal.wDay,
  52. stLocal.wHour, stLocal.wMinute, stLocal.wSecond,
  53. stLocal.wMilliseconds,stLocal.wDayOfWeek);
  54. OutputDebugString(chBuf);
  55. }
  56. 上面两个函数在我测试时输出的结果,如下:
  57. UTC: 2007/11/11 1:53:1:46 0
  58. Local: 2007/11/11 9:53:1:46 0
  59. strTime.Format(_T(“%d-%d-%d %d:%d:%d.%d “),stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond,stLocal.wMilliseconds); //时间显示
  60. /
  61. VC获取系统时间、程序运行时间

    1.使用CTime类
    CString str;
    //获取系统时间
    CTime tm;
    tm=CTime::GetCurrentTime();
    str=tm.Format(“现在时间是%Y年%m月%d日 %X”);
    MessageBox(str,NULL,MB_OK);

    2.得到系统时间日期(使用GetLocalTime)

    SYSTEMTIME st;
    CString strDate,strTime;
    GetLocalTime(&st);
    strDate.Format(“%4d-%2d-%2d”,st.wYear,st.wMonth,st.wDay);
    strTime.Format(“%2d:%2d:%2d”,st.wHour,st.wMinute,st.wSecond) ;
    AfxMessageBox(strDate);
    AfxMessageBox(strTime);

    3.使用GetTickCount

    //获取程序运行时间
    long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
    Sleep(500);
    long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)
    str.Format(“time:%dms”,t2-t1);//前后之差即 程序运行时间
    AfxMessageBox(str);
    //获取系统运行时间
    long t=GetTickCount();
    CString str,str1;
    str1.Format(“系统已运行 %d时”,t/3600000);
    str=str1;
    t%=3600000;
    str1.Format(“%d分”,t/60000);
    str+=str1;
    t%=60000;
    str1.Format(“%d秒”,t/1000);
    str+=str1;
    AfxMessageBox(str);

  1. 4.使用time\_t time( time\_t \* timer ) 精确到秒
  2.   计算时间差使用double difftime( time\_t timer1, time\_t timer0
  3. 5.使用DWORD GetTickCount() 得到的是系统运行的时间 精确到毫秒
  4. 6.要获取高精度时间,可以使用
  5. BOOL QueryPerformanceFrequency(LARGE\_INTEGER \*lpFrequency)获取系统的计数器的频率
  6. BOOL QueryPerformanceCounter(LARGE\_INTEGER \*lpPerformanceCount)获取计数器的值
  7. 然后用两次计数器的差除以Frequency就得到时间。
  8. 7.还有David的文章中提到的方法:
  9. Multimedia Timer Functions
  10. The following functions are used with multimedia timers.
  11. timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
  12. timeGetTime/timeKillEvent/TimeProc/timeSetEvent 精度很高
  13. Q:
  14. GetTickCount()函数,说是毫秒记数,是真的吗,还是精确到55毫秒?
  15. A:
  16. GetTickCount()和GetCurrentTime()都只精确到55ms(1tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数。具体例子可以参考QA001022 "VC++中使用高精度定时器"QA001813 "如何在Windows实现准确的定时"QA004842 "timeGetTime函数延时不准"
  17. Q:
  18. vc++怎样获取系统时间,返回值是什么类型的变量呢?
  19. A:
  20. GetSystemTime返回的是格林威志标准时间
  21. VOID GetSystemTime(
  22. LPSYSTEMTIME lpSystemTime // address of system time structure
  23. );
  24. 函数就可以获得了,其中LPSYSTEMTIME 是个结构体
  25. 含:年,月,日,周几,小时,分,秒,毫秒。

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读