Commit 9723e205 authored by bescoto's avatar bescoto

Another timezone fix, in response to bug Randall Nortman reported


git-svn-id: http://svn.savannah.nongnu.org/svn/rdiff-backup@577 2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109
parent 45730a35
......@@ -8,6 +8,9 @@ Added Maximilian Mehnert's fix for too many open files bug.
Added fix for listing/restoring certain bad archives made when there
was a timezone bug. (Thanks to Stephen Isard)
Fixed timezone bug. Hopefully this is the last one. (Thanks to
Randall Nortman for bug report.)
New in v0.12.7 (2004/05/31)
---------------------------
......
......@@ -19,7 +19,7 @@
"""Provide time related exceptions and functions"""
import time, types, re
import time, types, re, calendar
import Globals
......@@ -34,7 +34,6 @@ _genstr_date_regexp1 = re.compile("^(?P<year>[0-9]{4})[-/]"
_genstr_date_regexp2 = re.compile("^(?P<month>[0-9]{1,2})[-/]"
"(?P<day>[0-9]{1,2})[-/](?P<year>[0-9]{4})$")
curtime = curtimestr = None
dst_in_effect = time.daylight and time.localtime()[8]
def setcurtime(curtime = None):
"""Sets the current time in curtime and curtimestr on all systems"""
......@@ -63,7 +62,7 @@ def setprevtime_local(timeinseconds, timestr):
def timetostring(timeinseconds):
"""Return w3 datetime compliant listing of timeinseconds"""
s = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(timeinseconds))
return s + gettzd()
return s + gettzd(timeinseconds)
def stringtotime(timestring):
"""Return time in seconds from w3 timestring
......@@ -82,9 +81,8 @@ def stringtotime(timestring):
assert 0 <= hour <= 23
assert 0 <= minute <= 59
assert 0 <= second <= 61 # leap seconds
timetuple = (year, month, day, hour, minute, second, -1, -1, -1)
if dst_in_effect: utc_in_secs = time.mktime(timetuple) - time.altzone
else: utc_in_secs = time.mktime(timetuple) - time.timezone
timetuple = (year, month, day, hour, minute, second, -1, -1, 0)
utc_in_secs = calendar.timegm(timetuple)
return long(utc_in_secs) + tzdtoseconds(timestring[19:])
except (TypeError, ValueError, AssertionError): return None
......@@ -136,13 +134,17 @@ page for more information.
interval_string = interval_string[match.end(0):]
return total
def gettzd():
def gettzd(timeinseconds = None):
"""Return w3's timezone identification string.
Expresed as [+/-]hh:mm. For instance, PST is -08:00. Zone is
coincides with what localtime(), etc., use.
Expresed as [+/-]hh:mm. For instance, PDT is -07:00 during
dayling savings and -08:00 otherwise. Zone is coincides with what
localtime(), etc., use. If no argument given, use the current
time.
"""
if timeinseconds is None: timeinseconds = time.time()
dst_in_effect = time.daylight and time.localtime(timeinseconds)[8]
if dst_in_effect: offset = -time.altzone/60
else: offset = -time.timezone/60
if offset > 0: prefix = "+"
......
......@@ -94,5 +94,17 @@ class TimeTest(unittest.TestCase):
self.assertRaises(Time.TimeException, g2t, "")
self.assertRaises(Time.TimeException, g2t, "3q")
def testTimeZone(self):
"""Test stringtotime on two strings straddling timezones"""
f = Time.stringtotime
invf = Time.timetostring
s1 = "2005-04-03T03:45:03-03:00"
s2 = "2005-04-03T02:45:03-03:00"
diff = f(s1) - f(s2)
assert diff == 3600, diff
assert f(invf(f(s1))) == f(s1), (s1, invf(f(s1)), f(invf(f(s1))), f(s1))
assert f(invf(f(s2))) == f(s2), (s2, f(invf(f(s2))), f(s2))
if __name__ == '__main__': unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment