From 5a8298c0c2c2569c3f3c508d1a0df4bca71ddf8a Mon Sep 17 00:00:00 2001 From: bescoto Date: Wed, 6 Apr 2005 05:51:59 +0000 Subject: Another timezone fix, in response to bug Randall Nortman reported git-svn-id: http://svn.savannah.nongnu.org/svn/rdiff-backup/trunk@576 2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109 --- rdiff-backup/CHANGELOG | 3 +++ rdiff-backup/rdiff_backup/Time.py | 20 +++++++++++--------- rdiff-backup/testing/timetest.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/rdiff-backup/CHANGELOG b/rdiff-backup/CHANGELOG index a6f3956..52b2f8b 100644 --- a/rdiff-backup/CHANGELOG +++ b/rdiff-backup/CHANGELOG @@ -1,6 +1,9 @@ New in v0.13.6 (2005/??/??) --------------------------- +Fixed timezone bug. Hopefully this is the last one. (Thanks to +Randall Nortman for bug report.) + Added fix for listing/restoring certain bad archives made when there was a timezone bug. (Thanks to Stephen Isard) diff --git a/rdiff-backup/rdiff_backup/Time.py b/rdiff-backup/rdiff_backup/Time.py index 553dcf7..b096510 100644 --- a/rdiff-backup/rdiff_backup/Time.py +++ b/rdiff-backup/rdiff_backup/Time.py @@ -19,7 +19,7 @@ """Provide time related exceptions and functions""" -import time, types, re, sys +import time, types, re, sys, calendar import Globals @@ -35,7 +35,6 @@ _genstr_date_regexp1 = re.compile("^(?P[0-9]{4})[-/]" _genstr_date_regexp2 = re.compile("^(?P[0-9]{1,2})[-/]" "(?P[0-9]{1,2})[-/](?P[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""" @@ -64,7 +63,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 @@ -83,9 +82,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 @@ -137,13 +135,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 = "+" diff --git a/rdiff-backup/testing/timetest.py b/rdiff-backup/testing/timetest.py index 0ab8514..56052ca 100644 --- a/rdiff-backup/testing/timetest.py +++ b/rdiff-backup/testing/timetest.py @@ -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() -- cgit v1.2.1