获得精确时间到微秒

Posted 1CM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获得精确时间到微秒相关的知识,希望对你有一定的参考价值。

#include "windows.h"
#include "stdio.h"
#include "inttypes.h"
int main()
{
	FILETIME flt;
	SYSTEMTIME st, lot;
	__int64 wintime;
	__int64 seconds;
	__int64 nano_seconds;
	int milliseconds;
	int microsecond;
	
	// Windows 2000 Professional [desktop apps | UWP apps]
	GetSystemTimeAsFileTime((FILETIME*)&wintime);

	// Windows 8 [desktop apps | UWP apps]
	//GetSystemTimePreciseAsFileTime((FILETIME*)&wintime);
	
	wintime      -=116444736000000000; 
	seconds = wintime / 10000000;
	nano_seconds = wintime % 10000000 * 100;
	milliseconds = nano_seconds / 1000000;
	microsecond = (nano_seconds - milliseconds * 1000000)/100;
	
	//printf("The file time is: seconds %lld nano-seconds %lld   milliseconds %d %ld\n", 
	//    seconds, nano_seconds, milliseconds, microsecond);

	FileTimeToSystemTime((FILETIME*)&wintime, &st);
	FileTimeToLocalFileTime((FILETIME*)&wintime, &flt);
	FileTimeToSystemTime(&flt, &lot);

	printf("The system time is: %02d:%02d:%02d.%03d.%04d \n", 
	    st.wHour, st.wMinute , st.wSecond, milliseconds, microsecond);

	printf("The local  time is: %02d:%02d:%02d.%03d.%04d \n", 
	    lot.wHour, lot.wMinute , lot.wSecond, milliseconds, microsecond);
	return 0;
}

输出
The system time is: 13:08:30.045.2907
The local  time is: 21:08:30.045.2907

以上是关于获得精确时间到微秒的主要内容,如果未能解决你的问题,请参考以下文章

使用微秒来计算PHP脚本的执行时间

mysql5.5时间戳能不能精确到毫秒?

精确获取函数运行时间,精确到微秒

如何在windows开发环境下得到精确到微秒的系统时间?

mysql时间字段转换为毫秒格式

shell脚本示例:计算毫秒级微秒级时间差