TZ determination

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

TZ determination

Bruce Fitzsimons-2
Can anyone suggest some cleaner, more efficient code than this for
determining the offset from UTC/GMT? I *know* the bif's have access to this
information, but I can't see any way to get it out. It just seems awful to
have to reverse-engineer the difference, and its certainly inefficient.

Suggestions on how to better drive io_lib would be appreciated too - I ended
up with 0-100 under some formatting/padding combinations, which seemed like
a bug.

zone() ->
    Time = erlang:universaltime(),
    LocalTime = calendar:universal_time_to_local_time(Time),
    DiffSecs = calendar:datetime_to_gregorian_seconds(LocalTime) -
calendar:datetime_to_gregorian_seconds(Time),
    zone((DiffSecs/3600)*100).

%% Ugly reformatting code to get times like +0000 and -0200

zone(Val) when Val < 0 ->
    io_lib:format("-~4..0w", [trunc(abs(Val))]);
zone(Val) when Val >= 0 ->
    io_lib:format("+~4..0w", [trunc(abs(Val))]).


Thanks,
Bruce




Reply | Threaded
Open this post in threaded view
|

TZ determination

Per Hedeland-4
"Bruce Fitzsimons" <Bruce> wrote:
>
>Can anyone suggest some cleaner, more efficient code than this for
>determining the offset from UTC/GMT? I *know* the bif's have access to this
>information, but I can't see any way to get it out. It just seems awful to
>have to reverse-engineer the difference, and its certainly inefficient.

Hm, why the sudden interest in time zones (well, second question in two
weeks:-)? But anyway, no, there is no easier way to access this info
AFAIK, and it's not Erlang's fault - see below for how it's done with
"all the power of C" in a rather well-known open-source program that
needs to do such things... You want to watch out for non-integral-hours
offsets though:

$ env TZ=Australia/Adelaide date +%z
+1030
$ env TZ=Australia/Adelaide erl -noshell -s foo przone -s erlang halt
+1050

:-)

>Suggestions on how to better drive io_lib would be appreciated too - I ended
>up with 0-100 under some formatting/padding combinations, which seemed like
>a bug.

Doesn't seem to happen with what you have now - though maybe
io*:format() is "overkill", you could do something like

  L = integer_to_list(trunc(abs(Val))),
  "+" ++ lists:nthtail(length(L), "0000") ++ L.

(at least you don't have to read the io man page to figure out what it
does:-).

--Per Hedeland

-------------

        gmt = *gmtime(&t);
        lt = localtime(&t);

        off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;

        /* assume that offset isn't more than a day ... */
        if (lt->tm_year < gmt.tm_year)
                off -= 24 * 60;
        else if (lt->tm_year > gmt.tm_year)
                off += 24 * 60;
        else if (lt->tm_yday < gmt.tm_yday)
                off -= 24 * 60;
        else if (lt->tm_yday > gmt.tm_yday)
                off += 24 * 60;


Reply | Threaded
Open this post in threaded view
|

TZ determination

Bruce Fitzsimons-2
Hi Per,

----- Original Message -----
From: "Per Hedeland" <per>
To: <Bruce>
Cc: <erlang-questions>
Sent: Wednesday, March 12, 2003 1:31 AM
Subject: Re: TZ determination


> Hm, why the sudden interest in time zones (well, second question in two
> weeks:-)

I'm making the Yaws logging more Apache compliant so log analysers will work
over it. It works well with Analog now.

> needs to do such things... You want to watch out for non-integral-hours
> offsets though:
>
> $ env TZ=Australia/Adelaide date +%z
> +1030
> $ env TZ=Australia/Adelaide erl -noshell -s foo przone -s erlang halt
> +1050

I eventually decided that inets must be doing something similar, looked,
and it is. Almost exactly the same except without the bug you've noted. :-)

> >Suggestions on how to better drive io_lib would be appreciated too - I
ended
> >up with 0-100 under some formatting/padding combinations, which seemed
like
> >a bug.
>
> Doesn't seem to happen with what you have now - though maybe
> io*:format() is "overkill", you could do something like

4> io_lib:format("~6..0w", [-33]).
[["000","-33"]]

Which looks buggy, but it may be explainable.

>
>   L = integer_to_list(trunc(abs(Val))),
>   "+" ++ lists:nthtail(length(L), "0000") ++ L.

Better...

> (at least you don't have to read the io man page to figure out what it
> does:-).

Even after reading it, I'm still confused. Every time I need to use for some
special formatting it it takes me a few cycles to make it work. Is it just
me?

Thanks for the comments.

/Bruce