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

46 篇文章 0 订阅
11 篇文章 0 订阅

精确到秒的 std::time

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

  1. 它是精确到秒的。如果您需要更高精度的时间,比如说您需要精确到毫秒,那么它不合适。
  2. 它表示从 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 年好像看起来也不太遥远了。
  3. 在某些系统上(比如说我现在正在使用的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 中有两个精确到毫秒的获取当前时间的函数:

这两个函数的返回都是: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 打交道,别无它法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值