Message31134
It would be nice if the Python time module provided an obvious way to get the local time UTC offset for an arbitrary time stamp. The existing constants included in the module are not sufficient to correctly determine this value.
As context, the Bazaar version control system (written in Python), the local time UTC offset is recorded in a commit.
The method used in releases prior to 0.14 made use of the "daylight", "timezone" and "altzone" constants from the time module like this:
if time.localtime(t).tm_isdst and time.daylight:
return -time.altzone
else:
return -time.timezone
This worked most of the time, but would occasionally give incorrect results.
On Linux, the local time system can handle different daylight saving rules for different spans of years. For years where the rules change, these constants can provide incorrect data. Furthermore, they may be incorrect for time stamps in the past.
I personally ran into this problem last December when Western Australia adopted daylight saving -- time.altzone gave an incorrect value until the start of 2007.
Having a function in the standard library to calculate this offset would solve the problem. The implementation we ended up with for Bazaar was:
offset = datetime.fromtimestamp(t) - datetime.utcfromtimestamp(t)
return offset.days * 86400 + offset.seconds
Another alternative would be to expose tm_gmtoff on time tuples (perhaps using the above code to synthesise it on platforms that don't have the field). |
|
Date |
User |
Action |
Args |
2007-08-23 14:51:37 | admin | link | issue1647654 messages |
2007-08-23 14:51:37 | admin | create | |
|