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

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

以下是Time的MSDN文档:

Compatibility in the Introduction.

Libraries

 

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.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 <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( &ltime );

      printf( "Time in seconds since UTC 1/1/70:/t%ld/n", ltime );

      printf( "UNIX time and date:/t/t/t%s", ctime( &ltime ) );

      /* Display UTC. */

      gmt = gmtime( &ltime );

      printf( "Coordinated universal time:/t/t%s", asctime( gmt ) );

      /* Convert to time structure and adjust for PM if necessary. */

      today = localtime( &ltime );

      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", tstruct.millitm );

      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( &ltime );

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

}

 

Output

OS time:                                  21:51:03

OS date:                                  05/03/94

Time in seconds since UTC 1/1/70:         768027063

UNIX 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 PM

Plus milliseconds:                        279

Zone difference in seconds from UTC:      480

Time zone name:                          

Daylight savings:                         YES

Christmas                                 Sat Dec 25 12:00:00 1993

Today is Tuesday, day 03 of May in the year 1994.

 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值