s。从参数名称
windowsticks来看,我尝试了GetTickCount,但不久之后看到了这是系统启动的MS,但是我需要任何合理的计数,因为Windows Time(似乎是1601年?)。 我看到Windows这次具有检索功能:GETSYSTIMETIMETITER。我无法将结果结构传递给1中提出的函数,因为它不是一个长的长度值。 没有人可以为C或C ++提供一个完整的工作示例,而不会忽略此类疯狂的细节? 对于Windows上的人:
Int64 GetSystemTimeAsUnixTime()
{
//Get the number of seconds since January 1, 1970 12:00am UTC
//Code released into public domain; no attribution required.
const Int64 UNIX_TIME_START = 0x019DB1DED53E8000; //January 1, 1970 (start of Unix epoch) in "ticks"
const Int64 TICKS_PER_SECOND = 10000000; //a tick is 100ns
FILETIME ft;
GetSystemTimeAsFileTime(&ft); //returns ticks in UTC
//Copy the low and high parts of FILETIME into a LARGE_INTEGER
//This is so we can access the full 64-bits as an Int64 without causing an alignment fault
LARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
//Convert ticks since 1/1/1970 into seconds
return (li.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND;
}
该函数的名称与其他Windows函数使用的命名方案匹配。 WindowsSystemTime是BY定义
UTC.
功能
返回类型
分辨率
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv) {
time_t ltime;
time(<ime);
printf("Current local time as unix timestamp: %li\n", ltime);
struct tm* timeinfo = gmtime(<ime); /* Convert to UTC */
ltime = mktime(timeinfo); /* Store as unix timestamp */
printf("Current UTC time as unix timestamp: %li\n", ltime);
return 0;
}
示例输出:Current local time as unix timestamp: 1386334692
Current UTC time as unix timestamp: 1386331092
通过SYSTEMTIME
设置的GETSYSTEMTIMETITE | 0.001s | |
---|---|---|
GetSystemTime
struct tm
函数将其转换为“ UNIX时空邮票”。