summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorishepard <spadini.davide@gmail.com>2018-04-04 10:04:23 +0200
committerSebastian Thiel <byronimo@gmail.com>2018-04-04 10:15:48 +0200
commitc6e0a6cb5c70efd0899f620f83eeebcc464be05c (patch)
treed8fc4f051490ac5a889c0ac51d2f2069b8ec924c
parent0857d33852b6b2f4d7bc470b4c97502c7f978180 (diff)
downloadgitpython-c6e0a6cb5c70efd0899f620f83eeebcc464be05c.tar.gz
Avoid from_timestamp() function to raise an exception when the offset is greater or lower than 24 hours.
Add tests that exercise the new behaviour
-rw-r--r--git/objects/util.py7
-rw-r--r--git/test/test_util.py18
2 files changed, 21 insertions, 4 deletions
diff --git a/git/objects/util.py b/git/objects/util.py
index f630f966..7b6a2763 100644
--- a/git/objects/util.py
+++ b/git/objects/util.py
@@ -121,8 +121,11 @@ utc = tzoffset(0, 'UTC')
def from_timestamp(timestamp, tz_offset):
"""Converts a timestamp + tz_offset into an aware datetime instance."""
utc_dt = datetime.fromtimestamp(timestamp, utc)
- local_dt = utc_dt.astimezone(tzoffset(tz_offset))
- return local_dt
+ try:
+ local_dt = utc_dt.astimezone(tzoffset(tz_offset))
+ return local_dt
+ except ValueError:
+ return utc_dt
def parse_date(string_date):
diff --git a/git/test/test_util.py b/git/test/test_util.py
index b7925c84..9c993205 100644
--- a/git/test/test_util.py
+++ b/git/test/test_util.py
@@ -7,7 +7,7 @@
import tempfile
import time
from unittest import skipIf
-
+from datetime import datetime
import ddt
@@ -18,7 +18,8 @@ from git.objects.util import (
utctz_to_altz,
verify_utctz,
parse_date,
-)
+ tzoffset,
+ from_timestamp)
from git.test.lib import (
TestBase,
assert_equal
@@ -260,3 +261,16 @@ class TestUtils(TestBase):
self.failUnlessRaises(IndexError, ilist.__delitem__, 0)
self.failUnlessRaises(IndexError, ilist.__delitem__, 'something')
+
+ def test_from_timestamp(self):
+ # Correct offset: UTC+2, should return datetime + tzoffset(+2)
+ altz = utctz_to_altz('+0200')
+ self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(altz)), from_timestamp(1522827734, altz))
+
+ # Wrong offset: UTC+58, should return datetime + tzoffset(UTC)
+ altz = utctz_to_altz('+5800')
+ self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(0)), from_timestamp(1522827734, altz))
+
+ # Wrong offset: UTC-9000, should return datetime + tzoffset(UTC)
+ altz = utctz_to_altz('-9000')
+ self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(0)), from_timestamp(1522827734, altz))