Skip to content

Commit c6e0a6c

Browse files
ishepardByron
authored andcommitted
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
1 parent 0857d33 commit c6e0a6c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

git/objects/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ def dst(self, dt):
121121
def from_timestamp(timestamp, tz_offset):
122122
"""Converts a timestamp + tz_offset into an aware datetime instance."""
123123
utc_dt = datetime.fromtimestamp(timestamp, utc)
124-
local_dt = utc_dt.astimezone(tzoffset(tz_offset))
125-
return local_dt
124+
try:
125+
local_dt = utc_dt.astimezone(tzoffset(tz_offset))
126+
return local_dt
127+
except ValueError:
128+
return utc_dt
126129

127130

128131
def parse_date(string_date):

git/test/test_util.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import tempfile
88
import time
99
from unittest import skipIf
10-
10+
from datetime import datetime
1111

1212
import ddt
1313

@@ -18,7 +18,8 @@
1818
utctz_to_altz,
1919
verify_utctz,
2020
parse_date,
21-
)
21+
tzoffset,
22+
from_timestamp)
2223
from git.test.lib import (
2324
TestBase,
2425
assert_equal
@@ -260,3 +261,16 @@ def test_iterable_list(self, case):
260261

261262
self.failUnlessRaises(IndexError, ilist.__delitem__, 0)
262263
self.failUnlessRaises(IndexError, ilist.__delitem__, 'something')
264+
265+
def test_from_timestamp(self):
266+
# Correct offset: UTC+2, should return datetime + tzoffset(+2)
267+
altz = utctz_to_altz('+0200')
268+
self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(altz)), from_timestamp(1522827734, altz))
269+
270+
# Wrong offset: UTC+58, should return datetime + tzoffset(UTC)
271+
altz = utctz_to_altz('+5800')
272+
self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(0)), from_timestamp(1522827734, altz))
273+
274+
# Wrong offset: UTC-9000, should return datetime + tzoffset(UTC)
275+
altz = utctz_to_altz('-9000')
276+
self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(0)), from_timestamp(1522827734, altz))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy