time.localtime add a parameter for timezone

Dieter Maurer dieter at handshake.de
Mon Feb 24 12:21:29 EST 2020


qbit wrote at 2020-2-24 05:18 -0800:
>How about adding a time zone parameter to time.localtime?
>
>A  offset just like the form: ± hh[:mm[:ss]].

Why should this be necessary? `localtime` returns the time
in the "local timezone" -- and the "local timezone" usually
does not change between different calls to `localtime`.
Thus, it is best not to pass the timezone to each call to `localtime`
but use environment information.

If your "local timezone" does change frequently, you have (at least)
the following options:

 * use `time.tzset` to reset the internal variables used by
   `localtime` from the envvar `TZ` (which your have appropriately
   set earlier)

 * use `time.time` (instead of `time.localtime`) and add the offset
   yourself. `time.time` returns the UTC time.


--
Dieter


More information about the Python-list mailing list