classification
Title: Use local timezone offset by default in datetime.timezone
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add aware local time support to datetime module
View: 9527
Assigned To: Nosy List: belopolsky, lemburg, ncoghlan, pitrou
Priority: normal Keywords:

Created on 2012-02-22 02:27 by ncoghlan, last changed 2012-02-22 04:24 by ncoghlan. This issue is now closed.

Messages (3)
msg153923 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-02-22 02:27
Currently, datetime.timezone requires that the offset be specified explicitly.

It would be more convenient if the local offset was used by default.

Since the time module already exposes the offset details for the local timezone, this would just make:

  local_tz = datetime.timezone()

equivalent to the current complex incantation:

  local_tz = datetime.timezone(datetime.timedelta(hours=(time.timezone / -3600 + tm.tm_isdst)))

Then getting a timezone aware version of the local time would just be:

  now = datetime.datetime.now(datetime.timezone())

Similar to the existing spelling for UTC:

  now = datetime.datetime.now(datetime.timezone.utc)

(Inspired by http://hyperpolyglot.org/scripting#timezone-offset-notes)
msg153925 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2012-02-22 03:25
The problem with this idea is that while evaluating datetime.datetime.now(datetime.timezone()), python will have to query the real time clock twice (first in timezone() and then in now()).  At a particularly unfortunate time this may result in an error.  Unlikely? Yes, but I would not board a python powered airplane if this problem was in the standard library.

I gave a lot of thought to the problem of supporting timezone aware local time and issue9527 was the best solution I was able to come up with.
msg153927 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-02-22 04:23
Marking as a duplicate of #9527.

If that API is added, the local fixed offset timezone will be accessible as datetime.datetime.localtime().tzinfo which would be simple enough.
History
Date User Action Args
2012-02-22 04:24:18ncoghlansetstatus: open -> closed
2012-02-22 04:23:55ncoghlansetresolution: duplicate
superseder: Add aware local time support to datetime module
messages: + msg153927
2012-02-22 03:25:15belopolskysetmessages: + msg153925
2012-02-22 02:27:38ncoghlancreate