classification
Title: time.tzset does not reset _strptime's locale time cache
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.2, Python 3.1, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: belopolsky, mibanescu
Priority: normal Keywords: patch

Created on 2009-07-13 17:33 by mibanescu, last changed 2010-07-12 15:05 by belopolsky.

Files
File name Uploaded Description Edit
ttime.py mibanescu, 2009-07-13 17:33 Reproducer for the problem.
_strptime.py.patch mibanescu, 2010-07-12 13:55
Messages (3)
msg90497 - (view) Author: Mihai Ibanescu (mibanescu) Date: 2009-07-13 17:33
If one changes from one timezone to another within the same python
process, and if one tries to parse a time string that includes the
timezone, the library malfunctions.

See attached script for a reproducer.

The problem is that, even though time.tzset() is called, the LocaleTime
persisted in the TimeRE global is not reset. In my example, the EDT
timezone name, compiled from the previous TZ variable, is not valid
anymore in the 'Pacific/Fiji' timezone.

To witness the behavior, run the attached script with no arguments. It
will parse the time in the America/New_York timezone just fine.

Then run it with an argument (like python ttime.py 1). It will first
prime the _strptime cache in the Pacific/Fiji timezone, then attempt to
parse the same time string in the America/New_York timezone.

Finally, you can change the "if 0" to "if 1" for working around the problem.

This has been verified in 2.4.4 and 2.6.1 (did not have a chance to
verify it against python 2.6.2 yet).
msg110086 - (view) Author: Mihai Ibanescu (mibanescu) Date: 2010-07-12 13:55
Proposed patch attached.
msg110091 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-07-12 15:05
Thank you for the bug report and a patch.

This does look like a bug and proposed patch seems to be the simplest way to fix it.  It is unfortunate that the entire TimeRE cache needs to be recalculated when only 'Z' entry is invalidated by TZ change.  Please consider separating lang and TZ change checks and not reinitialize TimeRE cache object if only TZ changes, just replace the 'Z' entry. 

 The patch also needs unit tests.  It should be straightforward to convert ttime.py into a test case.  Please add a test case for both test_time and test_datetime.  Note that in 3.2 datetime.strptime() will process '%Z' when '%z' is also present.  For example,


>>> datetime.strptime('Fri Jul 25 13:26:29 EDT -0500 2008', '%a %b %d %H:%M:%S %Z %z %Y')
datetime.datetime(2008, 7, 25, 13, 26, 29, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400), 'EDT'))


Please make sure to restore environment variables in test cleanup.

Also a nit on the implementation: cls is unused in _get_timezone(cls), so it would be more appropriate to make it staticmethod instead of classmethod.  And tzset should probably be called inside _get_timezone.
History
Date User Action Args
2010-07-12 15:05:47belopolskysetmessages: + msg110091
stage: needs patch -> test needed
2010-07-12 13:58:20belopolskysetassignee: belopolsky
2010-07-12 13:55:15mibanescusetfiles: + _strptime.py.patch
keywords: + patch
messages: + msg110086
2010-07-10 23:53:56BreamoreBoysetnosy: + belopolsky
stage: needs patch

versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 2.4
2009-07-13 17:33:43mibanescucreate