From 4a47a9c8d8253d0ae2a233fa8599b1a1c54ec53f Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 18 Dec 2014 12:39:13 -0500 Subject: BF: adjust mktime output for the local offset (Close #218) --- git/objects/util.py | 5 ++++- git/test/test_util.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/git/objects/util.py b/git/objects/util.py index f36bf296..d31c4c93 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -156,7 +156,10 @@ def parse_date(string_date): fstruct = time.struct_time((dtstruct.tm_year, dtstruct.tm_mon, dtstruct.tm_mday, tstruct.tm_hour, tstruct.tm_min, tstruct.tm_sec, dtstruct.tm_wday, dtstruct.tm_yday, tstruct.tm_isdst)) - return int(time.mktime(fstruct)), utctz_to_altz(offset) + utctime = time.mktime(fstruct) + # time.mktime returns local time, so we need to adjust it for local offset + utctime -= time.altzone if time.daylight else time.timezone + return int(utctime), utctz_to_altz(offset) except ValueError: continue # END exception handling diff --git a/git/test/test_util.py b/git/test/test_util.py index d8682030..10d397c3 100644 --- a/git/test/test_util.py +++ b/git/test/test_util.py @@ -102,7 +102,7 @@ class TestUtils(TestBase): iso3 = ("2005.04.07 22:13:11 -0000", 0) alt = ("04/07/2005 22:13:11", 0) alt2 = ("07.04.2005 22:13:11", 0) - veri_time = 1112904791 # the time this represents + veri_time = 1112911991 # the time this represents for date, offset in (rfc, iso, iso2, iso3, alt, alt2): assert_rval(parse_date(date), veri_time, offset) # END for each date type -- cgit v1.2.1 From 3f879c71bdf0aac7af9b01304ff02e94b5af71b7 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 19 Dec 2014 17:42:06 +0100 Subject: Simplified parse_date implementation This allows to deal with the previous UTC issue without manually reversing timezone adjustments --- git/objects/util.py | 14 +++++++------- git/test/test_util.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/git/objects/util.py b/git/objects/util.py index d31c4c93..35073971 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -14,6 +14,7 @@ from collections import deque as Deque from string import digits import time +import calendar import os __all__ = ('get_object_type_by_name', 'parse_date', 'parse_actor_and_date', @@ -106,9 +107,10 @@ def parse_date(string_date): * ISO 8601 2005-04-07T22:13:13 The T can be a space as well - :return: Tuple(int(timestamp), int(offset)), both in seconds since epoch + :return: Tuple(int(timestamp_UTC), int(offset)), both in seconds since epoch :raise ValueError: If the format could not be understood - :note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY""" + :note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY. + """ # git time try: if string_date.count(' ') == 1 and string_date.rfind(':') == -1: @@ -121,6 +123,7 @@ def parse_date(string_date): offset = verify_utctz(string_date[-5:]) string_date = string_date[:-6] # skip space as well # END split timezone info + offset = utctz_to_altz(offset) # now figure out the date and time portion - split time date_formats = list() @@ -153,13 +156,10 @@ def parse_date(string_date): for fmt in date_formats: try: dtstruct = time.strptime(date_part, fmt) - fstruct = time.struct_time((dtstruct.tm_year, dtstruct.tm_mon, dtstruct.tm_mday, + utctime = calendar.timegm((dtstruct.tm_year, dtstruct.tm_mon, dtstruct.tm_mday, tstruct.tm_hour, tstruct.tm_min, tstruct.tm_sec, dtstruct.tm_wday, dtstruct.tm_yday, tstruct.tm_isdst)) - utctime = time.mktime(fstruct) - # time.mktime returns local time, so we need to adjust it for local offset - utctime -= time.altzone if time.daylight else time.timezone - return int(utctime), utctz_to_altz(offset) + return int(utctime), offset except ValueError: continue # END exception handling diff --git a/git/test/test_util.py b/git/test/test_util.py index 10d397c3..1ba855af 100644 --- a/git/test/test_util.py +++ b/git/test/test_util.py @@ -102,9 +102,9 @@ class TestUtils(TestBase): iso3 = ("2005.04.07 22:13:11 -0000", 0) alt = ("04/07/2005 22:13:11", 0) alt2 = ("07.04.2005 22:13:11", 0) - veri_time = 1112911991 # the time this represents + veri_time_utc = 1112911991 # the time this represents, in time since epoch, UTC for date, offset in (rfc, iso, iso2, iso3, alt, alt2): - assert_rval(parse_date(date), veri_time, offset) + assert_rval(parse_date(date), veri_time_utc, offset) # END for each date type # and failure -- cgit v1.2.1