简体   繁体   English

获取网络时间协议(NTP)时间戳

[英]Getting a Network Time Protocol (NTP) time stamp

gcc (GCC) 4.6.1 gcc(GCC)4.6.1

I am creating some sdp using RTF 4566 and I want to get the NTP time stamp so that I can use it for the session ID. 我正在使用RTF 4566创建一些sdp,我想获取NTP时间戳,以便可以将其用作会话ID。 It is recommended to use the Network Time Protocol (NTP) format timestamp be used to ensure uniqueness. 建议使用网络时间协议(NTP)格式时间戳以确保唯一性。

However, is there any function that will return the ntp time? 但是,是否有任何函数将返回ntp时间?

Many thanks for any suggestions, 非常感谢您的任何建议,

If anyone is interested, this is how I solved my problem. 如果有人感兴趣,这就是我解决问题的方式。

/* Get the time in micro seconds to create an unique session id */
time_usec = apr_time_usec(apr_time_now());
apr_snprintf(session_id, MAX_STRING_LEN, "%lu", (unsigned long)time_usec);

Hope this helps someone, 希望这对某人有帮助,

The NTP time stamp consists of a 32bit wide value representing the seconds since 1.1.1900 and a 32bit wide value adding a fractional part of value*1/(2^32) seconds to it. NTP时间戳由一个32位宽的值(表示自1.1.1900开始的秒数)和一个32位宽的值加一个值* 1 /(2 ^ 32)秒的小数部分组成。 1/(2^32) of a seconds is less then a nano second (233 pico seconds to be exact). 1 /(2 ^ 32)秒小于一纳秒(准确地说是233皮秒)。

Getting a value of such precision could be achieved using clock_gettime() . 可以使用clock_gettime()获得这种精度的值。 Anyhow one should use clock_getres() to test whether the clocks resolution is high enough to serve valid values. 无论如何,应该使用clock_getres()来测试时钟分辨率是否足够高以提供有效值。

surely there is a better way to do it but this one should works. 当然,有一种更好的方法可以做到这一点,但是这种方法应该可行。

#include <stdio.h>
#include <sys/time.h> //gettimeofday()
    int main()
    {
    struct timeval NTP_value;

    gettimeofday(&NTP_value, NULL);

    printf("\nsince January 1, 1970: %ld.%ld s\n", (long int)NTP_value.tv_sec,(long int)NTP_value.tv_usec);
    // that value is since 1970. So you just have to add 70years and 17 days 
    printf("\nsince January 1, 1900: %.6f s\n", (float)NTP_value.tv_sec+ (float)2208988800 + (float)NTP_value.tv_usec/1000000);
    return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM