Skip to content

Avoid from_timestamp() function to raise an exception when the offset… #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions git/objects/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ def dst(self, dt):
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):
Expand Down
18 changes: 16 additions & 2 deletions git/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tempfile
import time
from unittest import skipIf

from datetime import datetime

import ddt

Expand All @@ -18,7 +18,8 @@
utctz_to_altz,
verify_utctz,
parse_date,
)
tzoffset,
from_timestamp)
from git.test.lib import (
TestBase,
assert_equal
Expand Down Expand Up @@ -260,3 +261,16 @@ def test_iterable_list(self, case):

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))