Skip to content

Commit

Permalink
Merge pull request #219 from dateutil/tzw-nodt
Browse files Browse the repository at this point in the history
Allow tzwin objects to work with tzinfo
  • Loading branch information
pganssle committed Mar 15, 2016
2 parents 3a9ca9f + 1af6d8c commit fcd9fcd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
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

0 comments on commit fcd9fcd

Please sign in to comment.