time.strftime()

jsaul jsaul at gmx.de
Mon Mar 10 16:54:28 EST 2003


* Tim Peters [2003-03-10 19:38]:
> strftime can't be passed time zone info directly, no matter how it's called:
> its struct tm input simply doesnt't have any time zone info beyond the
> tm_isdst flag.  strftime gets all time zone info from the current locale;
> the tuple passed to it determines only whether daylight time is in effect
> (via the tm_isdst flag).

Sorry, but I have to contradict. What you say is of course right
w.r.t. the standardized members of struct tm; however, it does
have an extra tm_gmtoff member on some Unices. This field contains
seconds east of UTC and has the value 3600 for my time zone (CET)
if I call time.strftime(fmt) without time tuple, whereas it is 0
if a time tuple is supplied.

> > The way this is describge in the html-docs is a bit misleading,
> > IMHO: "If tuple is not provided, the current time as returned by
> > localtime() is used." But both cases are *not* equivalent, as the
> > tuple lacks time zone information.
>
> Read the C code if you really want to know what's going on.  You're going to
> have to anyway to figure out what's going wrong on your box <0.9 wink>.  The
> cases should indeed be equivalent, but only God knows how locale can get
> screwed up on your box.

Not the locale, fortunately... Sometimes one has to read the
system header files as well. But it turned out that actually it
*is* a bug in... timemodule.c? GNU-libc's strftime?

It's more of a subtle inconsistency than a bug perhaps; however,
on systems where struct time has that member tm_gmtoff, it is
apparently (generally?) necessary to fill it with the appropriate
offset value to obtain the correct output for the "%z" specifier.
C99 requires an 'extern long timezone;' in time.h; this value
represents seconds *west* of UTC, i.e. '-timezone' would have to
be used to set tm_gmtoff, I believe. Or one simply initializes it
in a way like:

        time_t tt = time(NULL);
        buf = *localtime(&tt);
        if (tup != NULL) {
                if (!gettmarg(tup, &buf))
                        return NULL;
        }

*and* removal of the memset in gettmarg()
instead of

        if (tup == NULL) {
                time_t tt = time(NULL);
                buf = *localtime(&tt);
        } else if (!gettmarg(tup, &buf))
                return NULL;

in "timemodule.c". Any comments?

Cheers, jsaul
-- 
Palo pa'que aprenda que aquí sí hay honor!
[Rubén Blades]




More information about the Python-list mailing list