Skip to content
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

Added support for timedelta objects when constructing tzoffsets #277

Merged
merged 1 commit into from Nov 6, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions dateutil/test/test_tz.py
Expand Up @@ -570,6 +570,12 @@ def testTimeOnlyUTC(self):


class TzOffsetTest(unittest.TestCase):
def testTimedeltaOffset(self):
est = tz.tzoffset('EST', timedelta(hours=-5))
est_s = tz.tzoffset('EST', -18000)

self.assertEqual(est, est_s)

def testTzNameNone(self):
gmt5 = tz.tzoffset(None, -18000) # -5:00
self.assertIs(datetime(2003, 10, 26, 0, 0, tzinfo=gmt5).tzname(),
Expand All @@ -586,6 +592,7 @@ def testTzOffsetRepr(self):
tzo = tz.tzoffset(tname, -5 * 3600)
self.assertEqual(repr(tzo), "tzoffset(" + repr(tname) + ", -18000)")


def testEquality(self):
utc = tz.tzoffset('UTC', 0)
gmt = tz.tzoffset('GMT', 0)
Expand Down
9 changes: 8 additions & 1 deletion dateutil/tz/tz.py
Expand Up @@ -69,10 +69,17 @@ class tzoffset(datetime.tzinfo):
The timezone name, to be returned when ``tzname()`` is called.

:param offset:
The time zone offset in seconds.
The time zone offset in seconds, or represented as a
:py:class:`datetime.timedelta` object.
"""
def __init__(self, name, offset):
self._name = name

try:
# Allow a timedelta
offset = _total_seconds(offset)
except (TypeError, AttributeError):
pass
self._offset = datetime.timedelta(seconds=offset)

def utcoffset(self, dt):
Expand Down