freeDiameter - 事件时间戳

时间:2017-01-23 19:21:05

标签: c free-diameter

我刚才有一个关于" Event-Timestamp"的问题。 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"。

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

3 个答案:

答案 0 :(得分:0)

那么,应该重新严格遵循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;

但是你可以在下面的屏幕截图中看到,时间实际上是不正确的:

enter image description here

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

由于

答案 1 :(得分:0)

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

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

time(2)函数为您提供time_t值(某些架构有64位time_t,有些架构有32位,您必须能够知道您拥有哪一个)但是有一个不同的EPOCH 。这就是你做得不好的事。 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,不需要本地转换)

答案 2 :(得分:-1)

“fd_dictfct_Time_encode”功能就是答案:)