2

我对“事件时间戳”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 吗?

但标准说:“ The string MUST contain four octets”。

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

4

3 回答 3

2

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

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

time(2)函数为您提供了一个time_t值(有些架构有 64bit time_t,有些架构有 32 位,您必须能够知道您拥有哪一个)但有不同的 EPOCH。这就是你做的不好。NTP 中的纪元是 00:00h 1st/jan/ 1900,而 UNIX 时间是 00:00h 1st/jan/ 1970,因此您必须以秒为单位校正该差异以补偿这 70 年的差异。

要将 unixtime()时间转换为 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,所以不需要本地转换)

于 2017-01-25T09:27:06.317 回答
1

好吧,应该重新严格遵循 RFC :) 将纪元时间转换为字节(长度 = 4)就可以了。

更新:它并没有真正解决:/

我的意思是现在,从 AVP 格式的角度来看,消息没问题.. 但是时间不正确..

我使用下面的代码来创建纪元时间并转换为字节数组:

time_t t = time(NULL);
tm tmp;
tmp.tm_isdst = -1;
tmp = *localtime( &t );
time_t t2 = mktime( &tmp );

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

但正如您在下面的屏幕截图中看到的那样,时间实际上是不正确的:

在此处输入图像描述

你能告诉我我在这里做错了什么吗?

谢谢

于 2017-01-23T20:18:48.740 回答
-1

“fd_dictfct_Time_encode”函数就是答案:)

于 2017-01-24T01:56:56.757 回答