Skip to content

Commit

Permalink
Merge pull request trigger#236 from trigger/tzfix
Browse files Browse the repository at this point in the history
Attempt to fix various time zone issues so that trigger can use the
  • Loading branch information
jathanism committed Oct 13, 2015
2 parents b1a4a7c + a798bac commit 8992787
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'pyasn1', # Twisted conch needs this, but doesn't say so
'pycrypto',
'pyparsing==1.5.7',
'pytz<=2014.2',
'pytz',
'SimpleParse',
'redis', # The python interface, not the daemon!
]
Expand Down
26 changes: 13 additions & 13 deletions tests/test_changemgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@


# Globals
EST = timezone('US/Eastern')
PST = timezone('US/Pacific')
eastern = timezone('US/Eastern')
pacific = timezone('US/Pacific')


class CheckBounceStatus(unittest.TestCase):
Expand All @@ -48,31 +48,31 @@ def testString(self):

class CheckBounceWindow(unittest.TestCase):
def setUp(self):
self.est = BounceWindow(green='5-7', yellow='8-11')
self.pst = BounceWindow(green='2-4', yellow='5-7')
self.eastern = BounceWindow(green='5-7', yellow='8-11')
self.pacific = BounceWindow(green='2-4', yellow='5-7')

def testStatus(self):
"""Test lookup of bounce window status."""
# 00:00 UTC, 19:00 EST
when = datetime(2006, 1, 3, tzinfo=UTC)
self.assertEquals(self.est.status(when), 'red')
# 06:00 PST, 14:00 UTC
then = datetime(2013, 6, 3, tzinfo=PST)
self.assertEquals(self.pst.status(then), 'green')
self.assertEquals(self.eastern.status(when), 'red')
# 03:00 PST, 14:00 UTC
then = pacific.localize(datetime(2013, 6, 4))
self.assertEquals(self.pacific.status(then), 'green')

def testNextOk(self):
"""Test bounce window next_ok() method."""
when = datetime(2013, 1, 3, 22, 15, tzinfo=UTC)
next_ok = self.pst.next_ok('yellow', when)
next_ok = self.pacific.next_ok('yellow', when)
# Did we get the right answer? (2 am PST the next morning)
self.assertEquals(next_ok.tzinfo, UTC)
self.assertEquals(next_ok.astimezone(EST).hour, 2)
self.assertEquals(next_ok.astimezone(eastern).hour, 2)
self.assertEquals(next_ok, datetime(2013, 1, 4, 7, 0, tzinfo=UTC))
self.assertEquals(self.pst.status(next_ok), 'green')
self.assertEquals(self.pacific.status(next_ok), 'green')
# next_ok() should return current time if already ok.
self.assertEquals(self.pst.next_ok('yellow', next_ok), next_ok)
self.assertEquals(self.pacific.next_ok('yellow', next_ok), next_ok)
then = datetime(2013, 1, 3, 22, 15, tzinfo=UTC)
self.assertEquals(self.pst.next_ok('red', then), then)
self.assertEquals(self.pacific.next_ok('red', then), then)

class CheckWeekend(unittest.TestCase):
def testWeekend(self):
Expand Down
21 changes: 16 additions & 5 deletions trigger/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def pretty_time(t):
2011-07-19 12:40:30.820920-04:00
>>> print pretty_time(t)
09:40 PDT
>>> t = datetime.datetime(2011,07,20,04,13,tzinfo=localzone)
>>> t = localzone.localize(datetime.datetime(2011,07,20,04,13))
>>> print t
2011-07-20 04:13:00-05:00
>>> print pretty_time(t)
Expand All @@ -168,13 +168,24 @@ def pretty_time(t):
from trigger.conf import settings
localzone = timezone(os.environ.get('TZ', settings.BOUNCE_DEFAULT_TZ))
t = t.astimezone(localzone)
midnight = datetime.datetime.combine(datetime.datetime.now(), datetime.time(tzinfo=localzone))
ct = t.replace(tzinfo=None) # convert to naive time
# to make the following calculations easier
# calculate naive 'now' in local time
# passing localzone into datetime.now directly can cause
# problems, see the 'pytz' docs if curious
now = datetime.datetime.now(pytz.UTC)
now = now.astimezone(localzone)
now = now.replace(tzinfo=None)
# and compute midnight
midnight = datetime.datetime.combine(now, datetime.time())
midnight += datetime.timedelta(1)
if t < midnight:
tomorrow = midnight + datetime.timedelta(1)
thisweek = midnight + datetime.timedelta(6)
if ct < midnight:
return t.strftime('%H:%M %Z')
elif t < midnight + datetime.timedelta(1):
elif ct < tomorrow:
return t.strftime('tomorrow %H:%M %Z')
elif t < midnight + datetime.timedelta(6):
elif ct < thisweek:
return t.strftime('%A %H:%M %Z')
else:
return t.strftime('%Y-%m-%d %H:%M %Z')
Expand Down

0 comments on commit 8992787

Please sign in to comment.