gpt4 book ai didi

c - freeDiameter - 事件时间戳

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:16 27 4
gpt4 key购买 nike

我刚刚有一个关于“事件时间戳”AVP 的问题。

我知道我应该把纪元时间放在那里,但我主要关心它的格式,这是我目前在 RFC 中找到的:

8.21.  Event-Timestamp AVP

The Event-Timestamp (AVP Code 55) is of type Time, and MAY be
included in an Accounting-Request and Accounting-Answer messages to
record the time that the reported event occurred, in seconds since
January 1, 1900 00:00 UTC.


Time
The Time format is derived from the OctetString AVP Base Format.
The string MUST contain four octets, in the same format as the
first four bytes are in the NTP timestamp format. The NTP
Timestamp format is defined in chapter 3 of [SNTP].

This represents the number of seconds since 0h on 1 January 1900
with respect to the Coordinated Universal Time (UTC).

On 6h 28m 16s UTC, 7 February 2036 the time value will overflow.
SNTP [SNTP] describes a procedure to extend the time to 2104.
This procedure MUST be supported by all DIAMETER nodes.

所以,问题是我是否应该先获取系统当前时间(以纪元格式),然后将其转换为字符串并直接传递给 Event-Timestamp?

但是标准说:“字符串必须包含四个八位字节”。

我不知道如何实现这一点......请你详细说明一下好吗?

最佳答案

好吧,这里的问题是您在阅读的 RFC 中有一个引用链接到 NTP rfc 以定义 NTP 时间戳。

NTP 时间戳的前 4 个字节显示从纪元到获取时间戳的时间的整数部分(以秒为单位)。

time(2) 函数给你一个 time_t 值(有些架构有 64 位 time_t,有些有 32,你有能够知道你拥有的是哪一个)但属于不同的时代。这就是你做的不好。 NTP 中的纪元是 00:00h 1st/jan/1900,在 UNIX 时间中是 00:00h 1st/jan/1970,所以你必须纠正这个差异秒来弥补这 70 年的差异。

要将 unix time() 时间转换为 NTP 秒,您必须添加常量 2208988800U(注意,因为此常量在 32 位架构中必须是无符号的,因为它有 msb,不被解释为负值)到 time_t 值,或者在 64 位模式下进行计算。

所以,最后,您的代码应该是:

time_t t = time(NULL);
time_t t2 = t + 2208988800UL;

bytes[0] = (t2 >> 24) & 0xFF;
bytes[1] = (t2 >> 16) & 0xFF;
bytes[2] = (t2 >> 8) & 0xFF;
bytes[3] = t2 & 0xFF;

(因为两个时间戳都是 UTC,所以不需要本地转换)

关于c - freeDiameter - 事件时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41814008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com