内容:Q:如何获取时间?精度如何?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 FunctionsThe following functions are used with multimedia timers.timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTimetimeGetTime/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 是个结构体含:年,月,日,周几,小时,分,秒,毫秒。
以下是Time的MSDN文档:Compatibility in the Introduction.LibrariesLIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail versionReturn Valuetime returns the time in elapsed seconds. There is no error return.ParametertimerStorage location for timeRemarksThe time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.Example/* TIMES.C illustrates various time and date functions including:* time _ftime ctime asctime* localtime gmtime mktime _tzset* _strtime _strdate strftime** Also the global variable:* _tzname*/#include <time.h>#include <stdio.h>#include <sys/types.h>#include <sys/timeb.h>#include <string.h>void main(){char tmpbuf[128], ampm[] = "AM";time_t ltime;struct _timeb tstruct;struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };/* Set time zone from TZ environment variable. If TZ is not set,* the operating system is queried to obtain the default value* for the variable.*/_tzset();/* Display operating system-style date and time. */_strtime( tmpbuf );printf( "OS time:\t\t\t\t%s\n", tmpbuf );_strdate( tmpbuf );printf( "OS date:\t\t\t\t%s\n", tmpbuf );/* Get UNIX-style time and display as number and string. */time( <ime );printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );/* Display UTC. */gmt = gmtime( <ime );printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );/* Convert to time structure and adjust for PM if necessary. */today = localtime( <ime );if( today->tm_hour > 12 ){strcpy( ampm, "PM" );today->tm_hour -= 12;}if( today->tm_hour == 0 ) /* Adjust if midnight hour. */today->tm_hour = 12;/* Note how pointer addition is used to skip the first 11* characters and printf is used to trim off terminating* characters.*/printf( "12-hour time:\t\t\t\t%.8s %s\n",asctime( today ) + 11, ampm );/* Print additional time information. */_ftime( &tstruct );printf( "Plus milliseconds:\t\t\t%u\n", litm );printf( "Zone difference in seconds from UTC:\t%u\n",tstruct.timezone );printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );printf( "Daylight savings:\t\t\t%s\n",tstruct.dstflag ? "YES" : "NO" );/* Make time for noon on Christmas, 1993. */if( mktime( &xmas ) != (time_t)-1 )printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );/* Use time structure to build a customized time string. */today = localtime( <ime );/* Use strftime to build a customized time string. */strftime( tmpbuf, 128,"Today is %A, day %d of %B in the year %Y.\n", today );printf( tmpbuf );}OutputOS time: 21:51:03OS date: 05/03/94Time in seconds since UTC 1/1/70: 768027063UNIX time and date: Tue May 03 21:51:03 1994 Coordinated universal time: Wed May 04 04:51:03 1994 12-hour time: 09:51:03 PMPlus milliseconds: 279Zone difference in seconds from UTC: 480Time zone name:Daylight savings: YESChristmas Sat Dec 25 12:00:00 1993 Today is Tuesday, day 03 of May in the year 1994.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.wDa y);strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);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);如何在VC6.0中得到一个程序的运行时间,也就是这个程序耗费的时钟周期数// C和C++的时间编程#include<iostream> #include<ctime> using namespace std; int main() { time_t begin,end; begin=clock(); //这里加上你的代码end=clock(); cout<<"runtime: "<<double(end-begin)/CLOCKS_PER_SEC<<endl; }unix时间相关,也是标准库的这些在<time.h>1.timegm函数只是将struct tm结构转成time_t 结构,不使用时区信息;time_t timegm(struct tm *tm);2.mktime使用时区信息time_t mktime(struct tm *tm);timelocal 函数是GNU扩展的与posix函数mktime相当time_t timelocal (struct tm *tm);3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息;struct tm * gmtime(const time_t *clock);4.localtime使用时区信息struct tm * localtime(const time_t *clock);1.time获取时间,stime设置时间time_t t;t = time(&t);2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;int stime(time_t *tp)3.UTC=true 表示采用夏时制;4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;5.设置时区推荐使用setup来设置;6.设置时区也可以先更变/etc/sysconfig/clock中的设置再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效time_t只能表示68年的范围,即mktime只能返回1970-2038这一段范围的time_t看看你的系统是否有time_t64,它能表示更大的时间范围Window里面的一些不一样的CTime MFC类,好像就是把time.h封了个类,没扩展CTime t = GetCurrentTime(); SYSTEMTIME 结构包含毫秒信息typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME;SYSTEMTIME t1;GetSystemTime(&t1) CTime curTime(t1); WORD ms = t1.wMilliseconds; SYSTEMTIME sysTm;::GetLocalTime(&sysTm);在time.h中的_strtime() //只能在windows中用char t[11];_strtime(t);puts(t);------------------------------------------------------------------------------_timeb定义在SYS\TIMEB.H,有四个fieldsdstflagmillitmtimetimezonevoid _ftime( struct _timeb *timeptr );struct _timeb timebuffer; _ftime( &timebuffer );取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!-------------------------------------------------------------------------如何设定当前系统时间---windowsSYSTEMTIME m_myLocalTime,*lpSystemTime; m_myLocalTime.wYear=2003; m_myLocalTime.wMonth=1; m_myLocalTime.wDay=1; m_myLocalTime.wHour=0; m_myLocalTime.wMinute=0; m_myLocalTime.wSecond=0; m_myLocalTime.wMilliseconds=0; lpSystemTime=&m_myLocalTime; if( SetLocalTime(lpSystemTime) ) //此处换成SetSystemTime( )也不行MessageBox("OK !"); else MessageBox("Error !");SYSTEMTIMEm_myLocalTime,*lpSystemTime;m_myLocalTime.wYear=2003;m_myLocalTime.wMonth=1;m_ myLocalTime.wDay=1;lpSystemTime=&m_myLocalTime;if( SetDate(lpSystemTime) ) //此处换成SetSystemTime( )也不行MessageBox("OK !"); else MessageBox("Error !");-----------------------------------------------------------------------------用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。