我再次从 Windows 转到 Linux,我必须将一个计算 NTP 时间的函数从 Windows 移植到 Linux。看起来很简单,但格式是 Windows FILETIME
格式。我大概知道有什么区别,但到目前为止我无法正确地将 Linux 时间转换为 Windows FILETIME
格式。有人对如何做到这一点有任何想法吗?
我看过一些关于如何执行此操作的文章,但它们都使用 Win32 函数,而我无法使用它们!如果这没有意义,我可以发布 Windows 代码。
他们还采用当前时间并从 1900 年 1 月 1 日减去它以获得找到 NTP 的增量,我假设在 Linux 中我只需将
const unsigned long EPOCH = 2208988800UL
与我的时间相加即可得到这个结果?
I am once again going from Windows to Linux, I have to port a function from Windows to Linux that calculates NTP time. Seems simple but the format is in Windows FILETIME
format. I sort of have an idea what the differences are but so far I can not correctly convert my Linux time to the Windows FILETIME
format. Does anyone have any ideas on how to do this?
I have seen some articles on how to do this but they all use Win32 functions and I can't use them! I can post the Windows code if this makes no sense.
They also take the current time and subtract it from January 1st 1900 to get the delta to find NTP, I would assume in Linux I just add the
const unsigned long EPOCH = 2208988800UL
to my time to get this result?
发布评论
评论(3)
FILETIME
结构解释了它是什么。基本思想是 WindowsFILETIME
从 1601 年 1 月 1 日开始以 10-7 秒(100 纳秒间隔)为单位进行计数(为什么是 1601 年?不知道... )。在 Linux 中,您可以使用 -6)。 2.html" rel="noreferrer">gettimeofday()
。因此,以下 C 函数可以完成这项工作:The Microsoft documentation for the
FILETIME
structure explains what it is. The basic idea is that a WindowsFILETIME
counts by steps of 10-7 seconds (100-nanosecond intervals) from 1 Jan 1601 (why 1601? no idea...). In Linux you can obtain time in microseconds (10-6) from 1 Jan 1970 usinggettimeofday()
. Thus the following C function does the job:首先,为什么是1601?因为公历每 400 年重演一次,而预推公历是从 0001-01-01 开始的。所以 1601 是 1980 年(1970 年,...)之前的最后一个周期开始,这简化了计算。 就是为什么第三个千年开始于 2001 年 1 月 1 日,而不是开始于 2000 年 1 月 1 日...)
(哦,这 FILETIME,
代表 FILETIME 的刻度。
商)和分数(作为余数)。如果你计算
纯粹是无符号的,简单除法。但你不能代表消极
在这种情况下的值。
1844674407371ull,即2^64 / 10^7(四舍五入)
NTP 时间戳。 (您可能想要从低 32 位开始舍入
产品。请注意,四舍五入进位到整秒不能
只要小数刻度严格低于 10^7,就会发生。)
First, why 1601? Because the Gregorian Calendar repeats itself every 400 years, and the proleptic Gregorian Calendar starts with 0001-01-01. So 1601 was the last cycle start before 1980(1970,...) and that simplifies the calculations. (Oh, and that's why the 3rd millenium started on 2001-01-01, and not on 2000-01-01...)
To create something like a NTP time stamp with a binary fraction from a FILETIME,
represent the FILETIME's ticks.
quotient) and the fraction (as the remainder). If you calculate
purely in unsigned, simply divide. But you cannot represent negative
values in that case.
1844674407371ull, which is 2^64 / 10^7 (rounded)
NTP time stamp. (You might want to round from the lower 32 bits of
the product. Note that a rounding carry into the full seconds cannot
occur as long as the fractional ticks are strict below 10^7.)
假设您从废弃某些网站中获取时间戳值。您定义
您获取的值可能需要除以 1000。
然后您执行以下操作:
Assuming you fetch the timestamp value from scrapping some website. You define
The value you fetch might need to be divided by 1000.
then you do as follow: