TZ determination

Per Hedeland per@REDACTED
Tue Mar 11 13:31:48 CET 2003


"Bruce Fitzsimons" <Bruce@REDACTED> 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;



More information about the erlang-questions mailing list