您的位置:首页 > 其它

学习笔记之三 获取系统时间的方法汇总

2014-05-06 23:06 363 查看


Ⅰ.VC中得到当前系统的时间和日期:

得到时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒)。主要方法如下:
1)使用CRT函数

C++代码  


char szCurrentDateTime[32];     

time_t nowtime;     

struct tm* ptm;     

time(&nowtime);     

ptm = localtime(&nowtime);     

sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",     

    ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,     

    ptm->tm_hour, ptm->tm_min, ptm->tm_sec);    

2)使用SYSTEMTIME

C++代码  


char szCurrentDateTime[32];     

SYSTEMTIME systm;     

GetLocalTime(&systm);     

sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",     

        systm.wYear, systm.wMonth, systm.wDay,     

        systm.wHour, systm.wMinute, systm.wSecond);    

3)使用CTime

C++代码  


char szCurrentDateTime[32];     

CTime nowtime;     

nowtime = CTime::GetCurrentTime();     

    

sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",     

    nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay(),     

    nowtime.GetHour(), nowtime.GetMinute(), nowtime.GetSecond());   


Ⅱ.用VC++获取系统时间几种方法:

A:

1 使用time_t time( time_t * timer )    精确到秒

  计算时间差使用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 要获取高精度时间,可以使用

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

    BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值

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

6 还有David的文章中提到的方法:

    Multimedia Timer Functions

    The following 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 "VC++中使用高精度定时器"、QA001813 "如何在Windows实现准确的定时"和QA004842 "timeGetTime函数延时不准"。

Q:vc++怎样获取系统时间,返回值是什么类型的变量呢?

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

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

 

VOID GetSystemTime( 

LPSYSTEMTIME lpSystemTime // address of system time structure 

); 

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

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

Ⅲ.VC++获得当前系统时间的几种方案

//方案- 优点:仅使用C标准库;缺点:只能精确到秒级
#include < time.h>   
#include < stdio.h>   
int main( void )   
{   
time_t t = time( 0 );   
char tmp[64];   
strftime( tmp, sizeof(tmp), " %Y/%m/%d %X %A 本年第%j天 %z" , localtime(&t) );   
puts( tmp );   
return 0;   
}  

//方案二 优点:能精确到毫秒级;缺点:使用了windows API

#include < windows.h>   
#include < stdio.h>   
int main( void )   
{   
SYSTEMTIME sys;   
GetLocalTime( &sys );   
printf( " %4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n"   
,sys.wYear,sys.wMonth,sys.wDay   
,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds   
,sys.wDayOfWeek);   
return 0;   
}  

//方案三,优点:利用系统函数

#include< stdlib.h>   
#include< iostream>   
using namespace std;   
void main(){   
system(" time" );   
}  

可以改变电脑的时间设定

方案4:

#include< iostream>   
#include< ctime>   
using namespace std;   
int main()   
{   
time_t now_time;   
now_time = time(NULL);   
cout< < now_time;   
return 0;   
}  

另一:_strdate(tempstr);

另二: CTime

CString CTestView::GetTime()   
{   
CTime CurrentTime=CTime::GetCurrentTime();   
CString strTime;   
strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()   
,CurrentTime.GetSecond());   
return strTime;   
}  

Ⅳ.VC++
时间类型转换: CTime, COleDateTime, time_t, CString

CTime==>CString 

CTime time;

time.GetCurrentTime();

CString str;

str.Format("%s",time.Format("%y:%m:%d %H-%M-%S")

1

CString str;

CTime t = CTime::GetCurrentTime();

str.Format("%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay());

2

CString strTime;

CTime tTime = CTime::GetCurrentTime();

strTime = tTime.Format("%Y-%m-%d %H:%M:%S");

CString ==>CTime 

strCString="2003-10-27 6:24:37"; //CString--->COleDateTime 

COleVariant vtime(strCString);

vtime.ChangeType(VT_DATE);

COleDateTime time4=vtime;

COleDateTime time1(1977,4,16,2,2,2); //COleDataTime--->CTime

SYSTEMTIME systime;

VariantTimeToSystemTime(time1, &systime);

CTime tm(systime);

time_t time2=tm.GetTime(); //CTime--->time_t 

COleDateTime time3(time2); //time_t--->COleDateTime 

时间差

COleDateTime strFirst,strEnd;

strFirst = COleDateTime(strFirst.GetYear(),strFirst.GetMonth(),strFirst.GetDay(),0,0,0);

strEnd = COleDateTime(tmCurrent.GetYear(),tmCurrent.GetMonth(),tmCurrent.GetDay(),23,59,59);

tspan= strEnd - strFirst;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows timer vc