time.localtime add a parameter for timezone

Peter J. Holzer hjp-python at hjp.at
Tue Feb 25 09:26:42 EST 2020


On 2020-02-24 17:43:58 +0000, Rhodri James wrote:
> On 24/02/2020 17:21, Dieter Maurer wrote:
> > 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`.
> 
> It can if your calls to localtime() happen either side of a daylight saving
> time switch.
> 
> That said, I agree that the timezone doesn't really belong in the output of
> localtime().

I think it does (that POSIX omits it (and only includes the rather
useless tm_isdst) is a historical oversight which was unfortunately
never corrected).

However, it doesn't have to be added, since it is already there:

https://docs.python.org/3/library/time.html#time.struct_time

1084% python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> t = time.localtime()
>>> t.tm_gmtoff
3600
>>> t.tm_zone
'CET'

However, this not a parameter but a field of the return value.

I'm not sure what a parameter would do: Return the broken-down time for
the specified timezone? If so, an offset is not sufficient: It needs to
be a timezone name (something like "Europe/Vienna").

Proper general handling of timezones should probably be in datetime.

> There are a very few occasions when you want it, but more often you
> should be working in UTC not local time.

If you are working in UTC, you don't need localtime().

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20200225/9bc27c79/attachment.sig>


More information about the Python-list mailing list