当前时间从Windows到Unix Timestamp?

问题描述 投票:0回答:3

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; }
c++ c windows unix time
3个回答
17
投票
该函数的名称与其他Windows函数使用的命名方案匹配。 Windows

SystemTime

BY定义
UTC.

功能
返回类型分辨率0.0000001s系统时间结构 GETSYSTEMTIMEASUNIXTIME INT641s也许我的问题很糟糕:我想要的只是将当前时间作为Unix时间戳。 我自己现在就知道了(C语言,Code :: Blocks 12.11,Windows 7 64位):#include <stdio.h> #include <time.h> int main(int argc, char** argv) { time_t ltime; time(&ltime); printf("Current local time as unix timestamp: %li\n", ltime); struct tm* timeinfo = gmtime(&ltime); /* 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

5
投票
通过

SYSTEMTIME
设置的

GetSystemTime
结构,很容易创建A a(参考结构的引用),然后使用

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.