mailing list of musl libc
 help / color / mirror / code / Atom feed
* localtime() isn't local
@ 2012-10-26  7:57 Yoran Heling
  2012-10-26  8:15 ` Szabolcs Nagy
  0 siblings, 1 reply; 10+ messages in thread
From: Yoran Heling @ 2012-10-26  7:57 UTC (permalink / raw)
  To: musl

Hello,

I noticed that the localtime() function is equivalent to gmtime() on my
system. Following example illustrates the problem:

  $ cat localtime.c

  #include <stdio.h>
  #include <time.h>

  int main(int argc, char **argv) {
    char local[16], global[16];
    time_t tm = time(NULL);
    strftime(local, 10, "%H:%M:%S ", localtime(&tm));
    strftime(global, 10, "%H:%M:%S ", gmtime(&tm));
    printf("Local:  %s\nGlobal: %s\n", local, global);
    return 0;
  }

  $ musl-gcc localtime.c -o localtime-musl
  $ gcc localtime.c -o localtime-glibc
  $ ./localtime-musl
  Local:  07:53:47 
  Global: 07:53:47
  $ ./localtime-glibc
  Local:  09:53:50 
  Global: 07:53:50
  $ date
  Fri Oct 26 09:53:51 CEST 2012

This is on Arch Linux x86_64 with musl v0.9.6-88-g607b05a (that is, the
current git master).

Yoran.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26  7:57 localtime() isn't local Yoran Heling
@ 2012-10-26  8:15 ` Szabolcs Nagy
  2012-10-26  8:34   ` Yoran Heling
  0 siblings, 1 reply; 10+ messages in thread
From: Szabolcs Nagy @ 2012-10-26  8:15 UTC (permalink / raw)
  To: musl

* Yoran Heling <info@yorhel.nl> [2012-10-26 09:57:37 +0200]:
>   $ musl-gcc localtime.c -o localtime-musl
>   $ gcc localtime.c -o localtime-glibc
>   $ ./localtime-musl
>   Local:  07:53:47 
>   Global: 07:53:47
>   $ ./localtime-glibc
>   Local:  09:53:50 
>   Global: 07:53:50
>   $ date
>   Fri Oct 26 09:53:51 CEST 2012
> 

what is the TZ environment variable?

that's the only method localtime (and tzset)
should support in posix to determine the timezone

you may want to try

TZ=CEST ./localtime-musl


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26  8:15 ` Szabolcs Nagy
@ 2012-10-26  8:34   ` Yoran Heling
  2012-10-26 12:00     ` Szabolcs Nagy
  2012-10-26 21:54     ` Rich Felker
  0 siblings, 2 replies; 10+ messages in thread
From: Yoran Heling @ 2012-10-26  8:34 UTC (permalink / raw)
  To: musl

On 2012-10-26, Szabolcs Nagy wrote:
> * Yoran Heling <info@yorhel.nl> [2012-10-26 09:57:37 +0200]:
> >   $ musl-gcc localtime.c -o localtime-musl
> >   $ gcc localtime.c -o localtime-glibc
> >   $ ./localtime-musl
> >   Local:  07:53:47 
> >   Global: 07:53:47
> >   $ ./localtime-glibc
> >   Local:  09:53:50 
> >   Global: 07:53:50
> >   $ date
> >   Fri Oct 26 09:53:51 CEST 2012
> > 
> 
> what is the TZ environment variable?
> 
> that's the only method localtime (and tzset)
> should support in posix to determine the timezone
> 
> you may want to try
> 
> TZ=CEST ./localtime-musl

Ah, I didn't realize that the use of /etc/localtime wasn't part of
POSIX. I don't have a TZ variable set, so that explains the output.
TZ=CEST didn't work, but TZ=CEST-2 did the trick.

This is quite a bummer, though. I use musl to create static binaries for
Linux, and every glibc-based distribution (i.e. the vast majority) uses
/etc/localtime rather than the TZ variable. :-(

Thanks for the clarification.

Yoran


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26  8:34   ` Yoran Heling
@ 2012-10-26 12:00     ` Szabolcs Nagy
  2012-10-26 12:26       ` Rich Felker
  2012-10-26 21:54     ` Rich Felker
  1 sibling, 1 reply; 10+ messages in thread
From: Szabolcs Nagy @ 2012-10-26 12:00 UTC (permalink / raw)
  To: musl

* Yoran Heling <info@yorhel.nl> [2012-10-26 10:34:06 +0200]:
> Ah, I didn't realize that the use of /etc/localtime wasn't part of
> POSIX. I don't have a TZ variable set, so that explains the output.
> TZ=CEST didn't work, but TZ=CEST-2 did the trick.
> 
> This is quite a bummer, though. I use musl to create static binaries for
> Linux, and every glibc-based distribution (i.e. the vast majority) uses
> /etc/localtime rather than the TZ variable. :-(
> 

i guess you can hack the implementation at
http://cs.ucla.edu/~eggert/tz/tz-link.htm

into musl

note that after a quick look at the impementation
it does not seem to be entirely correct

it invokes undefined behaviour with signed
shift in its decoder of the binary tz data
(this is usual in old code, but bad)

they assume that read returns "enough" data
(which is not guaranteed by posix and it
might not be true on certain filesystems)

etc

glibc seems to have its own implementation
that's even uglier..


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26 12:00     ` Szabolcs Nagy
@ 2012-10-26 12:26       ` Rich Felker
  2012-10-26 14:37         ` Isaac Dunham
  0 siblings, 1 reply; 10+ messages in thread
From: Rich Felker @ 2012-10-26 12:26 UTC (permalink / raw)
  To: musl

On Fri, Oct 26, 2012 at 02:00:27PM +0200, Szabolcs Nagy wrote:
> * Yoran Heling <info@yorhel.nl> [2012-10-26 10:34:06 +0200]:
> > Ah, I didn't realize that the use of /etc/localtime wasn't part of
> > POSIX. I don't have a TZ variable set, so that explains the output.
> > TZ=CEST didn't work, but TZ=CEST-2 did the trick.
> > 
> > This is quite a bummer, though. I use musl to create static binaries for
> > Linux, and every glibc-based distribution (i.e. the vast majority) uses
> > /etc/localtime rather than the TZ variable. :-(
> > 
> 
> i guess you can hack the implementation at
> http://cs.ucla.edu/~eggert/tz/tz-link.htm

Adding zoneinfo support to musl is on the agenda, but not based on the
"official" source which is bloated and invalid C. A while back I was
reading the (very minimal) spec and it doesn't look that bad, but it
was complex enough that I didn't write the support code immediately.
I'll get around to it sometime, sooner rather than later if there's
really a demand for it.

Rich


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26 12:26       ` Rich Felker
@ 2012-10-26 14:37         ` Isaac Dunham
  2012-10-26 16:25           ` Szabolcs Nagy
  0 siblings, 1 reply; 10+ messages in thread
From: Isaac Dunham @ 2012-10-26 14:37 UTC (permalink / raw)
  To: musl

On Fri, 26 Oct 2012 08:26:36 -0400
Rich Felker <dalias@aerifal.cx> wrote:

> On Fri, Oct 26, 2012 at 02:00:27PM +0200, Szabolcs Nagy wrote:
> > * Yoran Heling <info@yorhel.nl> [2012-10-26 10:34:06 +0200]:
> > > Ah, I didn't realize that the use of /etc/localtime wasn't part of
> > > POSIX. I don't have a TZ variable set, so that explains the output.
> > > TZ=CEST didn't work, but TZ=CEST-2 did the trick.
> > > 
> > > This is quite a bummer, though. I use musl to create static binaries for
> > > Linux, and every glibc-based distribution (i.e. the vast majority) uses
> > > /etc/localtime rather than the TZ variable. :-(
> > > 
> > 
> > i guess you can hack the implementation at
> > http://cs.ucla.edu/~eggert/tz/tz-link.htm
> 
> Adding zoneinfo support to musl is on the agenda, but not based on the
> "official" source which is bloated and invalid C. A while back I was
> reading the (very minimal) spec and it doesn't look that bad, but it
> was complex enough that I didn't write the support code immediately.
> I'll get around to it sometime, sooner rather than later if there's
> really a demand for it.
FWIW, the last line in /etc/localtime seems in proper format for the TZ variable (that is, export TZ=`tail -n 1 /etc/localtime` should give you a valid POSIX timezone).  I don't know why there's so much more cruft, and I've only checked with my timezone.

Isaac Dunham <idunham@lavabit.com>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26 14:37         ` Isaac Dunham
@ 2012-10-26 16:25           ` Szabolcs Nagy
  2012-10-26 17:25             ` Isaac Dunham
  2012-10-26 17:41             ` Rich Felker
  0 siblings, 2 replies; 10+ messages in thread
From: Szabolcs Nagy @ 2012-10-26 16:25 UTC (permalink / raw)
  To: musl

* Isaac Dunham <idunham@lavabit.com> [2012-10-26 07:37:20 -0700]:
> FWIW, the last line in /etc/localtime seems in proper format for the TZ variable (that is, export TZ=`tail -n 1 /etc/localtime` should give you a valid POSIX timezone).  I don't know why there's so much more cruft, and I've only checked with my timezone.
> 
> Isaac Dunham <idunham@lavabit.com>

that's not always true as the posix tz format
cannot represent any timezone

in my tzdata files these are counter examples:

/usr/share/zoneinfo/Israel
/usr/share/zoneinfo/Iran
/usr/share/zoneinfo/America/Argentina/San_Luis
/usr/share/zoneinfo/America/Santiago
/usr/share/zoneinfo/Egypt
/usr/share/zoneinfo/Chile/Continental
/usr/share/zoneinfo/Chile/EasterIsland
/usr/share/zoneinfo/Antarctica/Palmer


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26 16:25           ` Szabolcs Nagy
@ 2012-10-26 17:25             ` Isaac Dunham
  2012-10-26 17:41             ` Rich Felker
  1 sibling, 0 replies; 10+ messages in thread
From: Isaac Dunham @ 2012-10-26 17:25 UTC (permalink / raw)
  To: musl

On Fri, 26 Oct 2012 18:25:53 +0200
Szabolcs Nagy <nsz@port70.net> wrote:

> that's not always true as the posix tz format
> cannot represent any timezone
I presume you mean "the posix tz format cannot represent every timezone"?

> in my tzdata files these are counter examples:
> 
> /usr/share/zoneinfo/Israel
> /usr/share/zoneinfo/Iran
> /usr/share/zoneinfo/America/Argentina/San_Luis
> /usr/share/zoneinfo/America/Santiago
> /usr/share/zoneinfo/Egypt
> /usr/share/zoneinfo/Chile/Continental
> /usr/share/zoneinfo/Chile/EasterIsland
> /usr/share/zoneinfo/Antarctica/Palmer
Good to know.

-- 
Isaac Dunham <idunham@lavabit.com>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26 16:25           ` Szabolcs Nagy
  2012-10-26 17:25             ` Isaac Dunham
@ 2012-10-26 17:41             ` Rich Felker
  1 sibling, 0 replies; 10+ messages in thread
From: Rich Felker @ 2012-10-26 17:41 UTC (permalink / raw)
  To: musl

On Fri, Oct 26, 2012 at 06:25:53PM +0200, Szabolcs Nagy wrote:
> * Isaac Dunham <idunham@lavabit.com> [2012-10-26 07:37:20 -0700]:
> > FWIW, the last line in /etc/localtime seems in proper format for the TZ variable (that is, export TZ=`tail -n 1 /etc/localtime` should give you a valid POSIX timezone).  I don't know why there's so much more cruft, and I've only checked with my timezone.
> > 
> > Isaac Dunham <idunham@lavabit.com>
> 
> that's not always true as the posix tz format
> cannot represent any timezone
> 
> in my tzdata files these are counter examples:
> [...]

The zoneinfo format has several advantages over plain POSIX TZ. The
one that affects almost everyone is the ability to represent
historical changes in timezones. With POSIX TZ, times in the past will
be converted to local time based on the _current_ rules for the user's
timezone locality. With zoneinfo, the historical rule in use at the
time being converted gets used.

In addition, some timezones have rules which are sufficiently
arbitrary (and often ridiculous) so as to preclude any simple
algorithmic conversion. For those, even present and future dates need
the zoneinfo rules for conversion.

The format is basically a list of unix time ranges along with offsets
that apply for the ranges. Parsing it is mildly painful, but not too
bad. At first glance, mmapping the zoneinfo file looks like the best
approach.

Rich


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: localtime() isn't local
  2012-10-26  8:34   ` Yoran Heling
  2012-10-26 12:00     ` Szabolcs Nagy
@ 2012-10-26 21:54     ` Rich Felker
  1 sibling, 0 replies; 10+ messages in thread
From: Rich Felker @ 2012-10-26 21:54 UTC (permalink / raw)
  To: musl

On Fri, Oct 26, 2012 at 10:34:06AM +0200, Yoran Heling wrote:
> > you may want to try
> > 
> > TZ=CEST ./localtime-musl
> 
> Ah, I didn't realize that the use of /etc/localtime wasn't part of
> POSIX. I don't have a TZ variable set, so that explains the output.
> TZ=CEST didn't work, but TZ=CEST-2 did the trick.
> 
> This is quite a bummer, though. I use musl to create static binaries for
> Linux, and every glibc-based distribution (i.e. the vast majority) uses
> /etc/localtime rather than the TZ variable. :-(
> 
> Thanks for the clarification.

And thanks for voicing that this is omission is an issue that's
important to you. It's been on my mind for a while, just at low
priority; knowing that somebody cares about it will move it to the
front of my agenda. At the moment I'm trying to get things in order
for a release, but after that I'll take a serious look at what's
involved in adding zoneinfo support.

Rich


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-10-26 21:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-26  7:57 localtime() isn't local Yoran Heling
2012-10-26  8:15 ` Szabolcs Nagy
2012-10-26  8:34   ` Yoran Heling
2012-10-26 12:00     ` Szabolcs Nagy
2012-10-26 12:26       ` Rich Felker
2012-10-26 14:37         ` Isaac Dunham
2012-10-26 16:25           ` Szabolcs Nagy
2012-10-26 17:25             ` Isaac Dunham
2012-10-26 17:41             ` Rich Felker
2012-10-26 21:54     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).