C++ Tips: 获取更精确的系统时间(Windows 系统)
来源:互联网 发布:数据之魅 pdf 编辑:程序博客网 时间:2024/06/06 03:48
精确到秒的 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 26int _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 打交道,别无它法。
0 0
- C++ Tips: 获取更精确的系统时间(Windows 系统)
- Windows下精确到微秒的系统时间获取
- C/C++获取精确到微秒级的系统时间
- 获取精确到微秒的系统时间
- C/C++获取系统时间戳,精确到毫秒
- 更精确的获取时间的方法
- Windows系统获取系统的启动时间
- 精确到ms级别的获取系统时间办法
- windows系统中得到精确到微妙的时间
- VB.NET获取系统当前精确时间(毫秒级)
- Linux获取系统当前时间(精确到毫秒)
- VB.NET获取系统当前精确时间(毫秒级)
- Linux获取系统当前时间(精确到毫秒)
- Linux获取系统当前时间(精确到毫秒)
- 获取系统时间 精确到毫秒 iphone
- linux/unix 下精确获取系统时间
- 获取系统时间精确到毫秒级
- android获取系统时间精确到微秒
- Android系统简要分析
- apache kafka系列之kafka.common.ConsumerRebalanceFailedException异常解决办法
- POI 导入导出Excel文件到数据库(转载)
- Java 线程面试问题
- const 和 #define区别
- C++ Tips: 获取更精确的系统时间(Windows 系统)
- Android5.0以上使用XUtils访问网络出现错误: java.io.IOException: stack size 1036KB
- UI 常用方法总结之--- UITableView
- 使用Redis之前5个必须了解的事情(转载)
- Java设计模式----享元模式(FlyWeight)
- Linux命令(5):tee命令
- select, iocp, epoll,kqueue及各种I/O复用机制
- Python——入门学习之路
- Windows Server 2012 R2 安装报错解决方案