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

Allow tzwin objects to work with tzinfo #219

Merged
merged 1 commit into from
Mar 15, 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
74 changes: 74 additions & 0 deletions dateutil/test/test_tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def testTZSetDoesntCorrupt(self):
dt = parse('2014-07-20T12:34:56+00:00')
self.assertEqual(str(dt), '2014-07-20 12:34:56+00:00')


@unittest.skipUnless(IS_WIN, "Requires Windows")
class TzWinTest(unittest.TestCase):
def testTzResLoadName(self):
Expand Down Expand Up @@ -458,6 +459,37 @@ def testTzWinInequality(self):

self.assertNotEqual(tw1, tw2)

def testTzwinTimeOnlyDST(self):
# For zones with DST, .dst() should return None
tw_est = tz.tzwin('Eastern Standard Time')
self.assertIs(dt_time(14, 10, tzinfo=tw_est).dst(), None)

# This zone has no DST, so .dst() can return 0
tw_sast = tz.tzwin('South Africa Standard Time')
self.assertEqual(dt_time(14, 10, tzinfo=tw_sast).dst(),
timedelta(0))

def testTzwinTimeOnlyUTCOffset(self):
# For zones with DST, .utcoffset() should return None
tw_est = tz.tzwin('Eastern Standard Time')
self.assertIs(dt_time(14, 10, tzinfo=tw_est).utcoffset(), None)

# This zone has no DST, so .utcoffset() returns standard offset
tw_sast = tz.tzwin('South Africa Standard Time')
self.assertEqual(dt_time(14, 10, tzinfo=tw_sast).utcoffset(),
timedelta(hours=2))

def testTzwinLocalTimeOnlyTZName(self):
# For zones with DST, the name defaults to standard time
tw_est = tz.tzwin('Eastern Standard Time')
self.assertEqual(dt_time(14, 10, tzinfo=tw_est).tzname(),
'Eastern Standard Time')

# For zones with no DST, this should work normally.
tw_sast = tz.tzwin('South Africa Standard Time')
self.assertEqual(dt_time(14, 10, tzinfo=tw_sast),
'South Africa Standard Time')

@unittest.skipUnless(TZWinContext.tz_change_allowed(),
'Skipping unless tz changes are allowed.')
def testTzwinLocalName(self):
Expand Down Expand Up @@ -523,3 +555,45 @@ def testTzwinLocalEquality(self):
self.assertEqual(twl1, tw_pst)
self.assertNotEqual(twl1, tw_est)

@unittest.skipUnless(TZWinContext.tz_change_allowed(),
'Skipping unless tz changes are allowed.')
def testTzwinLocalTimeOnlyDST(self):
# For zones with DST, .dst() should return None
with TZWinContext('Eastern Standard Time'):
twl = tz.tzwinlocal()
self.assertIs(dt_time(14, 10, tzinfo=twl).dst(), None)

# This zone has no DST, so .dst() can return 0
with TZWinContext('South Africa Standard Time'):
twl = tz.tzwinlocal()
self.assertEqual(dt_time(14, 10, tzinfo=twl).dst(), timedelta(0))

@unittest.skipUnless(TZWinContext.tz_change_allowed(),
'Skipping unless tz changes are allowed.')
def testTzwinLocalTimeOnlyUTCOffset(self):
# For zones with DST, .utcoffset() should return None
with TZWinContext('Eastern Standard Time'):
twl = tz.tzwinlocal()
self.assertIs(dt_time(14, 10, tzinfo=twl).utcoffset(), None)

# This zone has no DST, so .utcoffset() returns standard offset
with TZWinContext('South Africa Standard Time'):
twl = tz.tzwinlocal()
self.assertEqual(dt_time(14, 10, tzinfo=twl).utcoffset(),
timedelta(hours=2))

@unittest.skipUnless(TZWinContext.tz_change_allowed(),
'Skipping unless tz changes are allowed.')
def testTzwinLocalTimeOnlyTZName(self):
# For zones with DST, the name defaults to standard time
with TZWinContext('Eastern Standard Time'):
twl = tz.tzwinlocal()
self.assertEqual(dt_time(14, 10, tzinfo=twl).tzname(),
'Eastern Standard Time')

# For zones with no DST, this should work normally.
with TZWinContext('South Africa Standard Time'):
twl = tz.tzwinlocal()
self.assertEqual(dt_time(14, 10, tzinfo=twl).tzname(),
'South Africa Standard Time')

15 changes: 13 additions & 2 deletions dateutil/tz/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,21 @@ def __ne__(self, other):
return not self.__eq__(other)

def utcoffset(self, dt):
if self._isdst(dt):
isdst = self._isdst(dt)

if isdst is None:
return None
elif isdst:
return datetime.timedelta(minutes=self._dstoffset)
else:
return datetime.timedelta(minutes=self._stdoffset)

def dst(self, dt):
if self._isdst(dt):
isdst = self._isdst(dt)

if isdst is None:
return None
elif isdst:
minutes = self._dstoffset - self._stdoffset
return datetime.timedelta(minutes=minutes)
else:
Expand Down Expand Up @@ -171,6 +179,9 @@ def _isdst(self, dt):
if not self._dstmonth:
# dstmonth == 0 signals the zone has no daylight saving time
return False
elif dt is None:
return None

dston = picknthweekday(dt.year, self._dstmonth, self._dstdayofweek,
self._dsthour, self._dstminute,
self._dstweeknumber)
Expand Down