【发布时间】:2018-03-06 05:06:15
【问题描述】:
将 NTP 时间戳转换为 UTC 的最简单方法是什么?我知道它是 NTP 格式的,我可以将它转换成任何其他格式。
谢谢。 鲍勃。
【问题讨论】:
-
根据此链接souptonuts.sourceforge.net/readme_working_with_time.html,自 01-01-1990 以来的秒数在长 ntp 的高位 int 中。
将 NTP 时间戳转换为 UTC 的最简单方法是什么?我知道它是 NTP 格式的,我可以将它转换成任何其他格式。
谢谢。 鲍勃。
【问题讨论】:
正如 rene 指出的那样,NTP 时间戳由整数和小数部分组成。整数部分表示自基准时间(即 1900 年 1 月 1 日)以来的秒数。小数部分表示秒中小数单位的数量(单位为 1/((2^32)-1))。
此外,时间表示为 UTC。
因此,如果您的 NTP 时间戳为 14236589681638796952。NTP 是一个 64 位无符号定点数。我们可以说:
UInt64 ntpTimestamp = 14236589681638796952;
高 32 位由下式给出:
UInt32 秒 = (UInt32)((ntpTimestamp >> 32) & 0xFFFFFFFF);
低 32 位由下式给出:
UInt32 分数 = (UInt32)(ntpTimestamp & 0xFFFFFFFF);
以秒为单位的数字等于最重要的单词,或者在这种情况下: 秒 == 3314714339
可以使用这个计算从分数中计算出毫秒数:
Int32 毫秒 = (Int32)(((Double)fraction / UInt32.MaxValue) * 1000);
在这种情况下是 12。
因此 DateTime 值来自:
DateTime BaseDate = new DateTime(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime dt = BaseDate.AddSeconds(seconds).AddMilliseconds(milliseconds);
因此 NTP 时间戳 14236589681638796952 等于 2005 年 1 月 14 日 17:58:59 和 12 毫秒 UTC。
【讨论】:
这对我来说很可靠:
#define NTP_TIMESTAMP_DIFF (2208988800) // 1900 to 1970 in seconds
#define NTP_MAX_INT_AS_DOUBLE (4294967295.0) // Max value of frac
// take care of the endianness
reply_pkt.tx_time_sec = ntohl( reply_pkt.tx_time_sec ) ;
reply_pkt.tx_time_frac = ntohl( reply_pkt.tx_time_frac ) ;
// parse
time_t tx_time = ( time_t ) ( reply_pkt.tx_time_sec - NTP_TIMESTAMP_DIFF );
double frac = ((double)reply_pkt.tx_time_frac) / NTP_MAX_INT_AS_DOUBLE ; // 2^32 -1
struct tm *tm = gmtime(&tx_time) ;
char ts[49];
strftime(ts,48,"[%Y-%m-%d %H:%M:%S]",tm);
printf("NTP query: reply was %s\n",ts);
ntp_time_seconds = ((double)tx_time) + frac ;
【讨论】:
试试这样的?我不确定“自 1900 年 1 月 1 日以来的秒数”的格式,但您可以根据需要进行修改。
long ntp = 3490905600;
DateTime start = new DateTime(1900, 1, 1);
DateTime dt = start.AddSeconds(ntp);
Console.WriteLine(dt.ToString());
Console.WriteLine(dt.ToUniversalTime().ToString());
【讨论】: