now() -> now_to_tz

Per Hedeland per@REDACTED
Thu Feb 27 22:38:45 CET 2003


"Pierpaolo BERNARDI" <bernardp@REDACTED> wrote:
>
>> 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}).
>
>Oops, I missed the (obvious) function calendar:now_to_datetime.
>So the above can be simplified to:
>
>now_to_local_time_tz({Mega,Sec,Micro},TZ) ->
>    calendar:now_to_datetime({Mega,Sec+TZ*3600,Micro}).

Oh - if you assume that the TZ variable directly specifies a fixed
number of hours of offset from UTC/GMT (or local time), the problem is
indeed trivial, but... - quick now, when does Calcutta switch to
Daylight Savings Time?:-)

The general problem of time zone conversions is actually ridiculously
complex, which is why there is a complete "database" for it, implemented
on all modern Unices (and probably on Windows too, more or less), as
well as a mailing list - see e.g.: http://www.twinsun.com/tz/tz-link.htm
The source files are actually a rather fascinating read (well, I think
so at least:-).

But maybe I just over-generalized the problem...

--Per



More information about the erlang-questions mailing list