[erlang-questions] NTP timestamp

Serge Aleynikov saleyn@REDACTED
Fri Dec 28 05:16:57 CET 2007


Here's a snippet that deals with time conversion from/to SNTP 
timestamps.  In the first function Sec and USec are integers in NTP format.

sntp_time_to_now(Sec, USec) ->
     case Sec band 16#80000000 of
     0 -> Time = Sec + 2085978496; % use base: 7-Feb-2036 @ 06:28:16 UTC
     _ -> Time = Sec - 2208988800  % use base: 1-Jan-1900 @ 01:00:00 UTC
     end,
     {Time div 1000000, Time rem 1000000, round((USec * 1000000) / (1 
bsl 32))}.

now_to_sntp_time({_,_,USec} = Now) ->
     SecsSinceJan1900 = 16#80000000 bor
 
(calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now)) 
- 59958230400),
     {SecsSinceJan1900, round(USec * (1 bsl 32) / 1000000)}.

Serge

József Bérces wrote:
> Hi,
> 
> I would like to convert NTP timestamps (seconds elapsed since 00:00:00 January 1st, 1900) to Erlang date/time tuples (UTC). I did not find any such functions in the OTP libraries. It seems that there is no function for converting now() to UTC date and time, either. 
> 
> Any help would be welcome.
> 
> Thanks,
> Jozsef
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list