1

Running Windows XP, in a Python application, I get the current time with:

import datetime
now = datetime.datetime.now()

And the time matches the time on the system clock display. However, now, with the application still running, I go to the Windows clock and select Time Zone and change it. Now I get the time again using the method above, and this time it is incorrect--it is giving me the time as though I had not changed the time zone.

But if I close the application and restart it and then get the time, then it matches the clock. It's as if it is "stuck" in whatever time zone the application loaded in first.

Interestingly, this only happens with a time zone change; if I merely change the time (and not the time zone) that change is changed in the application correctly.

Why is this and what can I do to prevent it?

Chelonian
  • 549
  • 2
  • 5
  • 21
  • Microsoft has never made any serious attempt to handle timezones in a useful way. If this is important to you, I'd advise a different operating system. – John La Rooy Apr 02 '13 at 05:30
  • @gnibbler Thanks, but not possible; the goal is to make an application that runs on Windows (as well as other platforms). – Chelonian Apr 02 '13 at 17:22

1 Answers1

1

My guess is that the Python runtime reads the system timezone on startup, and uses UTC as its basis. So when you change the timezone but not the time, there is no signal from Windows to Python to change anything. I am still digging through the python docs to substantiate this assumption.

Is there a reason why this is important? One would assume that the system timezone doesn't change very often - or at least not often enough that your user couldn't simply restart the application.

Even if it is true, there is probably a call you can make from Python to go update the cached timezone value.

Are you using pytz? Or just straight datetime?

UPDATE

This has been reported before, and the other question has a reasonable solution.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • It's somewhat inconvenient--the app (for which timestamping is critical) is most convenient to just be continually running and for travelers with laptops, this will screw up their timestamps unless they close the app and restart. I'm not using pytz yet, just straight datetime. Thanks for looking into how to get Python to update. – Chelonian Apr 02 '13 at 20:11
  • I think the answer in the dup post will work for you then. – Matt Johnson-Pint Apr 02 '13 at 20:12
  • Great catch on the duplicate! I did search prior to posting but didn't find that one. Helps to see, too, that this isn't a problem of my faulty coding but a more general issue. I'll try the solution there, thanks. – Chelonian Apr 02 '13 at 20:15