如何用vc 获取系统时间和程序运行时间

  • 格式:doc
  • 大小:44.50 KB
  • 文档页数:7

下载文档原格式

  / 7
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

标题:如何用vc++获取系统时间和程序运行时间

出处:春天的事业

时间:Mon, 22 Jun 2009 17:34:26 +0000

作者:xiechunye

地址:/read.php/612.htm

内容:

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 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 是个结构体

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

以下是Time的MSDN文档:

Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

time returns the time in elapsed seconds. There is no error return.

Parameter

timer

Storage location for time

Remarks

The 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

#include

#include

#include

#include

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 ) );