MFC中系统时间获取

来源:互联网 发布:mysql 存储过程 事物 编辑:程序博客网 时间:2024/06/03 13:10

编译环境:VS2010

获取系统时间的四种方法:


第1种:

CString str; //获取系统时间  
CTime tm;
tm=CTime::GetCurrentTime();   
str=tm.Format("现在时间是%Y年%m月%d日 %X");

MessageBox(str,NULL,MB_OK); 


第2种:
SYSTEMTIME st;  
CString strDate,strTime;  
GetLocalTime(&st);  
strDate.Format(L"%4d-%2d-%2d ",st.wYear,st.wMonth,st.wDay);  
strTime.Format(L"%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);  
AfxMessageBox(strDate+strTime);

第3种:
CString str; 
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)   
Sleep(100); 

long t2=GetTickCount();//程序段结束后取得系统运行时间(ms)   

str.Format(L"time:%dms",t2-t1);//前后之差即 程序运行时间   

AfxMessageBox(str);//获取系统运行时间  

第4种:

       LARGE_INTEGER litmp; 
       LONGLONG QPart1,QPart2;
       double dfMinus, dfFreq, dfTim; 
       QueryPerformanceFrequency(&litmp);
       dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
       QueryPerformanceCounter(&litmp);
       QPart1 = litmp.QuadPart;// 获得初始值
       Sleep(100);
       QueryPerformanceCounter(&litmp);
       QPart2 = litmp.QuadPart;//获得中止值
       dfMinus = (double)(QPart2-QPart1);
       dfTim = dfMinus / dfFreq;// 获得对应的时间值,单位为秒  

CString str; 
str.Format(L"time:%lf s",dfTim); 
AfxMessageBox(str);



精确到毫秒、微秒的延时函数:


       LARGE_INTEGER litmp; 
       LONGLONG QPart1,QPart2;
       double dfMinus, dfFreq, dfTim; 
       QueryPerformanceFrequency(&litmp);
       dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率
       QueryPerformanceCounter(&litmp);
       QPart1 = litmp.QuadPart;// 获得初始值
       do
       {
          QueryPerformanceCounter(&litmp);
          QPart2 = litmp.QuadPart;//获得中止值
          dfMinus = (double)(QPart2-QPart1);
          dfTim = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
       }while(dfTim<0.001);//ms

//while(dfTim<0.000001);//us







0 0
原创粉丝点击