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
%% returns: {date(), time()}
NTPtimestamps_to_datetime(NTPSeconds) ->
%% 1900:01:01 00:00:00 in gregorian seconds
NTPBase = calendar:datetime_to_gregorian_seconds({1900,1,1}, {0,0,0}),
GregorianSec = NTPBase + NTPSeconds,
calendar:gregorian_seconds_to_datetime(Seconds).
the calendar module has the following functions for converting now() to
UTC {date(), time()}:
now_to_universal_time(Now) -> {Date, Time}
now_to_datetime(Now) -> {Date, Time}
> Thanks,
> Jozsef
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
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