lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] SNTP Problem


From: Sylvain Rochet
Subject: Re: [lwip-users] SNTP Problem
Date: Sat, 28 Apr 2012 15:36:09 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Nick,

On Sat, Apr 28, 2012 at 04:17:31PM +0300, N.Karakotas wrote:
> Hi,
> 
> Im trying to update the time with SNTP but the timestamp received is 
> wrong. Then ctime printf rubbish. I also checked the received 
> timestamp is seems wrong. This is strange. I cant understand why its 
> not wornking. Any pointers?

What Wireshark says about those SNTP packets ?

Beware, NTP Era 0 origin is 1st Jan 1900 at midnight UTC, not 1970 like 
the unix timestamp.

Here is what you need to do to convert from Unix to NTP timestamp:

/* NTP timestamp to UNIX timestamp
 *
 * NTP timestamp limits:
 * - Era 0
 *   - Epoch is Jan 1 00:00:00 UTC 1900
 *   - Last value given 32 bits (4294967295) is Feb 7 06:28:15 UTC 2036
 * - Era 1
 *   - Epoch is Feb 7 06:28:16 UTC 2036
 *   - Last value given 32 bits (4294967295) is Mar 15 12:56:31 UTC 2172
 *
 * Today is Jul 28 UTC 2011, Unix timestamp at midnight is 1311811200,
 * NTP timestamp Era 0 at midnight is 3520800000
 *
 * NTP wrap coverage is 136 years, we don't except this product to live
 * this long, we consider NTP values after 3520800000 as Era 0, and
 * values before 3520800000 as Era 1
 *
 */
datetime_t ntp_to_datetime_timestamp(ntpts ts) {

        ts >>= 32; // We don't care about second fraction

        // Era 0
        // - NTP Timestamp 3520800000 to 4294967295
        // - Jul 28 00:00:00 UTC 2011 to Feb 7 06:28:15 UTC 2036
        // - UNIX Timestamp 1311811200 to 2085978495
        if( ts >= 3520800000UL )
                return ts - 2208988800LL;

        // Era 1
        // - NTP Timestamp 0 to 3520799999
        // - Feb 7 06:28:16 UTC 2036 to Sep 3 06:28:15 UTC 2147
        // - Unix Timestamp 2085978496 to 5606778495
        return ts + 2085978496LL;
}


/* UNIX timestamp to NTP timestamp
 *
 */
ntpts datetime_to_ntp_timestamp(datetime_t dt) {

        dt += 2208988800UL;
        return dt<<32;
}

Sylvain

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]