[erlang-questions] When the heck is now -
Serge Aleynikov
serge@REDACTED
Fri Oct 21 15:06:57 CEST 2016
Joe,
The following seems to achieve the desired result:
7> f(I), f(S), I = erlang:system_time(micro_seconds), S = I div 1000000,
calendar:now_to_local_time({S div 1000000, S rem 1000000, I rem 1000000}).
{{2016,10,21},{9,2,29}}
Serge
On Fri, Oct 21, 2016 at 6:33 AM, Joe Armstrong <erlang@REDACTED> wrote:
> My problem is converting the return value of erlang:system_time() to a
> local correct printable time that agrees with my clock.
>
> The value of erlang:local_lime() is irrelevant - the program I am talking
> to
> (SonicPi) reads the same clock as the erlang:system() reads - we need
> to clock sync
> on this value in a distributed system where other nodes might have
> badly adjusted
> clocks - so all I want to do is convert the return value of
> erlang:local_time() to local time units for debugging purposes.
>
>
> On Fri, Oct 21, 2016 at 11:21 AM, José Valim
> <jose.valim@REDACTED> wrote:
> > Hello Joe,
> >
> > erlang:localtime() will return the local time as a {date, time} tuple
> > according to your OS clock. As you mentioned, erlang:system_time() is the
> > POSIX time which is UTC (UTC-ish since POSIX time doesn't account for
> leap
> > seconds).
> >
> > Also Erlang 19.1 has added support for second, millisecond, microsecond,
> etc
> > as units. Previously they were seconds, milli_seconds, micro_seconds, etc
> > which did not agree with the SI convention.
>
> > Therefore I believe you are
> > reading the documentation for the most recent Erlang version while using
> an
> > older version.
>
> This could be true - I'll have to check.
>
> Actually my erlang is <the latest>-1 (or 2) and my documentation is
> what google found for me
>
>
> > You could try the latest Erlang or use the now deprecated
> > "seconds" and "micro_seconds" units instead of the correct "second" and
> > "microsecond".
> >
> >
> > José Valim
> > www.plataformatec.com.br
> > Skype: jv.ptec
> > Founder and Director of R&D
> >
> > On Fri, Oct 21, 2016 at 4:30 AM, Joe Armstrong <erlang@REDACTED> wrote:
> >>
> >> When is now?
> >>
> >> I want the raw uncorrected system time - I assume this is
> >>
> >> erlang:system_time()
> >>
> >> Seems to return the raw system clock in nanosconds past Epoch
> >>
> >> I want to convert this to a printable local time that agrees with my
> >> wristwatch (ie what the man on the Clapham omnibus might call the
> >> time)
> >>
> >> So I'd like to convert this to
> >> Year,Month,Day,Hour,Min,Sec,Fraction of sec
> >>
> >> There seems to be no function to do this (strange since,
> >> this seems to me to be a common thing one might want to do).
> >>
> >> In the old days I'd write:
> >>
> >> t1() ->
> >> T1 = erlang:timestamp(),
> >> calendar:now_to_local_time(T1).
> >>
> >> This works fine:
> >>
> >> > t1:t1().
> >> {{2016,10,21},{9,59,59}}
> >>
> >> After scratching my head and reading the manual pages
> >> I ended up with this:
> >>
> >> t2() ->
> >> T1 = erlang:system_time(),
> >> system_time_to_ymdhms(T1).
> >>
> >> system_time_to_ymdhms(T) ->
> >> S = erlang:convert_time_unit(T, native, seconds),
> >> {Days, X} = calendar:seconds_to_daystime(S),
> >> {Y,Month,Day} = calendar:gregorian_days_to_date(Days),
> >> {{Y+1970,Month,Day}, X}.
> >>
> >> Now stunningly obvious - and wrong by 2 hours
> >> (The erlang manual page is WRONG - it's seconds (plural) not second)
> >>
> >> > t1:t1().
> >> {{2016,10,21},{10,18,32}}
> >> > t1:t2().
> >> {{2016,10,21},{8,18,34}}
> >>
> >> The erlang manual page also says
> >>
> >> QUOTE
> >> The erlang:timestamp() BIF is equivalent to:
> >>
> >> timestamp() ->
> >> ErlangSystemTime = erlang:system_time(microsecond),
> >> MegaSecs = ErlangSystemTime div 1000000000000,
> >> Secs = ErlangSystemTime div 1000000 - MegaSecs*1000000,
> >> MicroSecs = ErlangSystemTime rem 1000000,
> >> {MegaSecs, Secs, MicroSecs}.
> >>
> >>
> >> Which is totally incorrect since it crashes
> >>
> >> t3() ->
> >> T1 = timestamp(),
> >> calendar:now_to_local_time(T1).
> >>
> >> timestamp() ->
> >> ErlangSystemTime = erlang:system_time(microsecond),
> >> MegaSecs = ErlangSystemTime div 1000000000000,
> >> Secs = ErlangSystemTime div 1000000 - MegaSecs*1000000,
> >> MicroSecs = ErlangSystemTime rem 1000000,
> >> {MegaSecs, Secs, MicroSecs}.
> >>
> >> > t1:t3().
> >> ** exception error: bad argument
> >> in function erlang:system_time/1
> >> called as erlang:system_time(microsecond)
> >> in call from t1:timestamp/0 (t1.erl, line 26)
> >> in call from t1:t3/0 (t1.erl, line 22)
> >>
> >> At which point I'm flummoxed - the erlang manual page is totally wrong
> >> in several places. The routines in calendar are not in phase with the
> >> changes in time routine.
> >>
> >> And I still can't convert erlang:system_time() to my local time
> >> since it's two hour wrong
> >>
> >> /Joe
> >> _______________________________________________
> >> erlang-questions mailing list
> >> erlang-questions@REDACTED
> >> http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161021/8ef44f08/attachment.htm>
More information about the erlang-questions
mailing list