C++获取windows系统时间的七种方式

来源:互联网 发布:mmd恋爱循环数据 编辑:程序博客网 时间:2024/04/30 23:43

1.CTime类:获取系统当前时间,精确到秒

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CString str;  
  2. CTime tm;  
  3. tm=CTime::GetCurrentTime();//获取系统日期  
  4. str=tm.Format("现在时间是%Y年%m月%d日 %X");  
  5. MessageBox(str,NULL,MB_OK);</span>  

a,从CTimet中提取年月日时分秒 

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CTime t = CTime::GetCurrentTime();  
  2. int d=t.GetDay(); //获得几号  
  3. int y=t.GetYear(); //获取年份  
  4. int m=t.GetMonth(); //获取当前月份  
  5. int h=t.GetHour(); //获取当前为几时   
  6. int mm=t.GetMinute(); //获取分钟  
  7. int s=t.GetSecond(); //获取秒  
  8. int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六</span>  

b,计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CTime t1( 1999, 3, 19, 22, 15, 0 );  
  2. CTime t = CTime::GetCurrentTime();  
  3. CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔  
  4. int iDay=span.GetDays(); //获取这段时间间隔共有多少天  
  5. int iHour=span.GetTotalHours(); //获取总共有多少小时  
  6. int iMin=span.GetTotalMinutes();//获取总共有多少分钟  
  7. int iSec=span.GetTotalSeconds();//获取总共有多少秒  

c,获得当前日期和时间,并可以转化为CString

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CTime tm=CTime::GetCurrentTime();   
  2. CString str=tm.Format("%Y-%m-%d");//显示年月日</span>  

2. time_t,time():C标准库(精确到秒)

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. time_t time(time_t *timer);//得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数   
  2. double difftime(time_t timer1,time_t timer0);//计算时间差</span>  

返回的字符串可以依下列的格式而定:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. %a 星期几的缩写。Eg:Tue   
  2. %A 星期几的全名。 Eg: Tuesday   
  3. %b 月份名称的缩写。   
  4. %B 月份名称的全名。   
  5. %c 本地端日期时间较佳表示字符串。   
  6. %d 用数字表示本月的第几天 (范围为 00 至 31)。日期   
  7. %H 用 24 小时制数字表示小时数 (范围为 00 至 23)。   
  8. %I 用 12 小时制数字表示小时数 (范围为 01 至 12)。   
  9. %j 以数字表示当年度的第几天 (范围为 001 至 366)。   
  10. %m 月份的数字 (范围由 1 至 12)。   
  11. %M 分钟。   
  12. %p 以 ''AM'' 或 ''PM'' 表示本地端时间。   
  13. %S 秒数。   
  14. %U 数字表示为本年度的第几周,第一个星期由第一个周日开始。   
  15. %W 数字表示为本年度的第几周,第一个星期由第一个周一开始。   
  16. %w 用数字表示本周的第几天 ( 0 为周日)。   
  17. %x 不含时间的日期表示法。   
  18. %X 不含日期的时间表示法。 Eg: 15:26:30   
  19. %y 二位数字表示年份 (范围由 00 至 99)。   
  20. %Y 完整的年份数字表示,即四位数。 Eg:2008   
  21. %Z(%z) 时区或名称缩写。Eg:中国标准时间   
  22. %% % 字符  

更细致可参考:http://blog.csdn.net/love_gaohz/article/details/6637625

3.clock_t,clock():C标准库(精确到毫秒)

其实返回的是程序运行到现在的时间 

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. int main(void)   
  5. {   
  6.     long i = 10000000L;   
  7.     clock_t start, finish;   
  8.     double duration;   
  9.     /* 测量一个事件持续的时间*/   
  10.     printf( "Time to do %ld empty loops is ", i) ;   
  11.     start = clock();   
  12.     while( i-- );   
  13.     finish = clock();   
  14.     duration = (double)(finish - start) / CLOCKS_PER_SEC; /*CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元*/  
  15.     printf( "%f seconds\n", duration );   
  16.     system("pause");   
  17. }<span style="font-family: 宋体;"> </span>  

4.GetTickCount()

从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。(精确到毫秒)

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //获取程序运行时间  
  2. long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)  
  3. Sleep(500);  
  4. long t2=GetTickCount();();//程序段结束后取得系统运行时间(ms)  
  5. str.Format("time:%dms",t2-t1);//前后之差即 程序运行时间  
  6. AfxMessageBox(str);  
  7. //获取系统运行时间  
  8. long t=GetTickCount();  
  9. CString str,str1;  
  10. str1.Format("系统已运行 %d时",t/3600000);  
  11. str=str1;  
  12. t%=3600000;  
  13. str1.Format("%d分",t/60000);  
  14. str+=str1;  
  15. t%=60000;  
  16. str1.Format("%d秒",t/1000);  
  17. str+=str1;  
  18. AfxMessageBox(str);  

5、QueryPerformanceFrequencyQueryPerformanceCounter:毫秒级

该方法是一种重量级的计算,可以计算非常准确的时间。一般情况下,到毫秒级别的,只需要使用clock即可。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LARGE_INTEGER frequency;   
  2. LARGE_INTEGER timeStart, timeEnd;   
  3. QueryPerformanceFrequency(&frequency); // 返回每秒,cpu 所使用的时钟数   
  4. QueryPerformanceCounter(&timeStart);   
  5. Sleep(100);   
  6. QueryPerformanceCounter(&timeEnd);   
  7. std::cout << difftime(timeEnd.QuadPart, timeStart.QuadPart)* 1000/frequency.QuadPart << std::endl;  

6、timeGetTime()

也是精确到毫秒级别的时间函数,但是该函数只是一个轻量级的时间函数

例:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //需要 #pragma comment(lib, "winmm.lib")  
  2. DWORD dw = timeGetTime();   
  3. std::cout << dw << std::endl;   
  4. Sleep(120);   
  5. DWORD dwEnd = timeGetTime();   
  6. std::cout << difftime(dwEnd, dw) << std::endl;  // 我的电脑显示的是121  

7、GetLocalTime、GetSystemTime:Windows API 函数

GetLocalTime,获取当地的当前系统日期和时间 (精确到毫秒)

GetSystemTime,则获取的是标准的时间。

两个函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. typedef struct _SYSTEMTIME  
  2. {  
  3.   WORD wYear;//年  
  4.   WORD wMonth;//月  
  5.   WORD wDayOfWeek;//星期:0为星期日,1为星期一,2为星期二……  
  6.   WORD wDay;//日  
  7.   WORD wHour;//时  
  8.   WORD wMinute;//分  
  9.   WORD wSecond;//秒  
  10.   WORD wMilliseconds;//毫秒  
  11. }SYSTEMTIME,*PSYSTEMTIME;  

例:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. SYSTEMTIME st;  
  2. CString strDate,strTime;  
  3. GetLocalTime(&st);  
  4. strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);  
  5. strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond) ;  
  6. AfxMessageBox(strDate);  
  7. AfxMessageBox(strTime);  
0 0
原创粉丝点击