您的位置:首页 > 编程语言 > C语言/C++

C++ Tips: 获取更精确的系统时间(Windows 系统)

2015-10-28 12:21 513 查看

精确到秒的 std::time

为了获得系统当前时间,目前 C++ 标准库里面给出的方法是 std::time,它返回的结构体是 std::time_t。这个方法很方便很通用,但它有一些局限:

它是精确到秒的。如果您需要更高精度的时间,比如说您需要精确到毫秒,那么它不合适。

它表示从 Epoch (1970年1月1日00:00:00)到现在所经过的秒数。最初 std::time_t 的定义是 32 位的,因此它的时间最多能表示到 2038年1月19日 03:14:07 (GMT时间),再过 1 秒,std::time 的返回就会变成 1901年12月13日 20:45:52 (GMT时间)。这就是著名的 Year 2038 problem。话说 2038 年好像看起来也不太遥远了。

在某些系统上(比如说我现在正在使用的Windows 7 64位),std::time_t 的定义变成了 64 位。这样就暂时给这个函数从死刑改判成死缓了。std::time_t 的定义从 32 位改为 64 位只是延长了它所表示的时间范围,但并没有改变它的精度,依然是精确到秒。

下面给出示范程序:

// GetSystemTime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <ctime>
#include <iostream>

#define BUF_SIZE 26

int _tmain(void)
{
TCHAR buf[BUF_SIZE] = { 0 };
errno_t err;
std::time_t cur_time;

/* Obtain current time as seconds elapsed since the Epoch. */
cur_time = std::time(nullptr);

if (cur_time == ((time_t)-1))
{
fprintf_s(stderr, "Failure to compute the current time.\n");
goto EXIT;
}

/* Convert to local time format. */
err = _tctime_s(buf, BUF_SIZE, &cur_time);
if (err != 0)
{
_tprintf_s(_T("Invalid Arguments for _wctime_s. Error Code: %d\n"), err);
goto EXIT;
}

_tprintf_s(_T("The local time is %s\n"), buf);

EXIT:
return 0;
}


执行结果:



精确到毫秒的 GetSystemTime / GetLocalTime

Windows SDK 中有两个精确到毫秒的获取当前时间的函数:

GetSystemTime:获取 UTC 时间。

GetLocalTime:获取当地时间。

这两个函数的返回都是:SYSTEMTIME

这个结构体占用了 16 个字节,它的定义如下:

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;


其中 wYear 表示的范围是:从 1601 到 30827。范围很广了,应该能用到天荒地老了。:-)

示范程序:

// GetSystemTime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>

int _tmain(void)
{
SYSTEMTIME utc_time = { 0 };
SYSTEMTIME local_time = { 0 };

GetSystemTime(&utc_time);
GetLocalTime(&local_time);

_tprintf(_T("The UTC time is \t: %02d:%02d:%02d.%03d\n"), utc_time.wHour, utc_time.wMinute, utc_time.wSecond, utc_time.wMilliseconds);
_tprintf(_T("The local time is\t: %02d:%02d:%02d.%03d\n"), local_time.wHour, local_time.wMinute, local_time.wSecond, local_time.wMilliseconds);

return 0;
}


运行结果:



精确到 100 纳秒的 GetSystemTimeAsFileTime

究竟能不能达到 100 纳秒的精确度呢?在 Windows SDK 中有一个结构体:FILETIME,它可以记录精度达到 100ns 的时间。用哪个函数得到这个值呢?可以用 GetSystemTimeAsFileTime

但是,不要高兴得太早,虽然 FILETIME 能够达到如此高的精度,但是这个函数我连试都懒得试。为什么呢?因为 Windows 系统并不是一个实时操作系统(Windows Embedded Compact 2013 是一个实时系统),其时钟精度一般认为是 15 ~ 16 毫秒。

Windows Sysinternals 给我们提供了一个查看你使用的 Windows 系统的时钟分辨率的小工具:ClockRes v2.0。把它下载下来在控制台中执行,结果如下:



看到了吧。死心了吧。

所以说,用 GetSystemTime / GetLocalTime 就已经很好了。

如果要获得真正毫秒级甚至更高精度的当前系统时间,必须跟 CPU 打交道,别无它法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C-C++ Win-SDK