This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Datetime and time doesn't update timezone in a running Win app
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, brian.curtin, cm, lemburg, r.david.murray, sijinjoseph, terry.reedy, tim.golden
Priority: normal Keywords:

Created on 2013-04-03 18:39 by cm, last changed 2022-04-11 14:57 by admin.

Messages (5)
msg185943 - (view) Author: CM (cm) Date: 2013-04-03 18:39
On Windows (tested on XP), the datetime module (and, as reported online in [1], time module) apparently gets the timezone only when a Python instance first starts, but then never updates the timezone during the life of that Python instance.  So, if the user changes the system time zone while an application is running, datetime still uses the time zone that the Python instance started with, and so it is incorrect.

Example, Python 2.7.3:

First, the system time zone is set to Eastern U.S. and the system time shows 14:27.

>>> import datetime
>>> print datetime.datetime.now()
2013-04-03 14:27:43.124000

This is correct, and matches the system clock.

Now user changes time zone on Windows to GMT (and doesn't change the time).  Clock now shows the time as 18:29.

>>> print datetime.datetime.now()
2013-04-03 14:29:02.875000
            ^
This is incorrect and doesn't match the time shown on the system clock.

Note:  if the user changes the system clock time and NOT the time zone, then datetime updates the time fine.  

This has been discussed in a Stack Overflow question, with a proposed workaround[1], but it doesn't fix the fact that this makes datetime incorrect for those that have processes that ought to continue across time zone changes as the user moves.

[1]See accepted answer in particular here:  http://stackoverflow.com/questions/4360981/make-python-respond-to-windows-timezone-changes
msg185959 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-04-03 21:37
I am not sure this is a bug, as I don't think it reasonable to expect every time function call to re-initialize the timezone setting. I think you should change this to a request to see if time.tzset can now be made to work on Windows and not unix-only (as documented, when added in 2.3). The second StackOverflow answer by Lentjes suggests that it can for current Python.

datetime.now(tz=None) has an option to pass a timezone for adjusting the output. An user-friendly app can pass a real timezone instead of the default None and give users means to change what tz they want times displayed for, which may or may not be the current local timezone.
msg185961 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-04-03 21:47
On linux, if you call time.ctime() before and after changing the system /etc/localtime file, the time will be updated without restarting Python.  The same applies to datetime.now().
msg186776 - (view) Author: Sijin Joseph (sijinjoseph) Date: 2013-04-13 18:11
This is the same as issue10634

Problem is with the Windows CRT implementation of localtime which does not seem to be using updated timezone information.

Can this become an issue with technologies like vMotion which allow Live Migration of virtual servers across data centers in different timezones?

One way to address maybe to somehow reset the CRT timezone structures on Windows prior to a call to localtime. Does this seem like a good approach? Is the issue worth pursuing?
msg186783 - (view) Author: Sijin Joseph (sijinjoseph) Date: 2013-04-13 18:27
Some more links discussing similar issues
http://www.sourceware.org/bugzilla/show_bug.cgi?id=154 - tzset not called frequently enough by localtime() and friends

http://stackoverflow.com/questions/12150651/library-code-for-dynamically-reloading-the-usr-share-zoneinfo-database - Workaround in linux for reloading timezone info



http://msdn.microsoft.com/en-US/library/90s5c885(v=vs.100).aspx - Microsoft CRT documentation for _tzset function, which can be called to reset timezone info.



Another possible fix, could be to add an overload for the functions to force reloading of the timezone info.
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61827
2013-04-13 18:27:08sijinjosephsetmessages: + msg186783
2013-04-13 18:11:24sijinjosephsetnosy: + sijinjoseph

messages: + msg186776
versions: + Python 3.4
2013-04-03 21:47:56r.david.murraysetnosy: + r.david.murray

messages: + msg185961
stage: needs patch ->
2013-04-03 21:40:21brian.curtinsetstage: needs patch
2013-04-03 21:37:14terry.reedysetnosy: + terry.reedy, belopolsky, tim.golden, brian.curtin, lemburg
messages: + msg185959
2013-04-03 18:39:11cmcreate