now() -> now_to_tz

Pierpaolo BERNARDI <>
Thu Feb 27 17:45:53 CET 2003


From: "Per Hedeland" <>

> Anastasia Gornostaeva <> wrote:
> >
> >How can I convert time now() to time using appropriate TZ?
> >I found only calendar:now_to_local(), but I want convert to other time
zones.
>
> This is actually not entirely trivial to do inside or outside Erlang,
> since there are no "explicit" ANSI/POSIX/Unix interfaces to get that
> info: The existing interfaces deal only with local time and GMT/UTC,
> obtaining the mapping between them by means of some system-specific
> config file such as /etc/localtime and/or the TZ environment variable.

Perhaps I'm misunderstanding the question, but why not the following:

local_tz() ->
    UT = calendar:universal_time(),
    LT = calendar:universal_time_to_local_time(UT),
    (calendar:datetime_to_gregorian_seconds(LT) -
 calendar:datetime_to_gregorian_seconds(UT)).

now_to_local_time_tz({Mega,Sec,Micro},TZ) ->
    TZSec = TZ * 3600 - local_tz(),
    calendar:now_to_local_time({Mega,Sec+TZSec,Micro}).
----------

47> calendar:now_to_local_time(now()).
{{2003,2,27},{17,35,57}}
48> tz:now_to_local_time_tz(now(),0).
{{2003,2,27},{16,36,8}}
49> tz:now_to_local_time_tz(now(),4).
{{2003,2,27},{20,36,12}}
50> tz:now_to_local_time_tz(now(),-5).
{{2003,2,27},{11,36,18}}

P.




More information about the erlang-questions mailing list