I在Convert Windows Filetime中看到了接受的解决方案,在Unix/Linux中,但我应该将其传递给函数
WindowsTickToUnixSeconds()
windowsTicks
。从参数名称来看,我尝试了GetTickCount()
,但是不久之后看到了这是系统启动的MS,但是我需要任何合理的计数,因为WindowsTime(似乎是在1601年?)。
我看到Windows这次具有检索功能:GetSystemTime()
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;
}
SystemTime
是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
GETSYSTEMTIMEASFILETIME Filtimestruct | ||
---|---|---|
GETSYSTEMTIMETITE | 0.001s | |
SYSTEMTIME
设置的GetSystemTime
结构,很容易创建A a(参考结构的引用),然后使用