Skip to content

Commit

Permalink
Add simple tzinfo implementation to support times with timezones.
Browse files Browse the repository at this point in the history
  • Loading branch information
atomatt committed Dec 9, 2009
1 parent 9976fb5 commit 2ab57b1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions convertish/tests/test_util.py
@@ -0,0 +1,28 @@
from datetime import time, timedelta
import unittest

from convertish.util import SimpleTZInfo


class TestSimpleTZInfo(unittest.TestCase):

def test_utcoffset(self):
self.assertEquals(SimpleTZInfo(60).utcoffset(None), timedelta(minutes=60))
self.assertEquals(SimpleTZInfo(-60).utcoffset(None), timedelta(minutes=-60))

def test_dst(self):
tz = SimpleTZInfo(60)
self.assertEquals(tz.dst(None), timedelta())

def test_tzname(self):
self.assertEquals(SimpleTZInfo(0).tzname(None), "+00:00")
self.assertEquals(SimpleTZInfo(60).tzname(None), "+01:00")
self.assertEquals(SimpleTZInfo(90).tzname(None), "+01:30")
self.assertEquals(SimpleTZInfo(-60).tzname(None), "-01:00")
self.assertEquals(SimpleTZInfo(-90).tzname(None), "-01:30")

def test_affect(self):
self.assertEquals(time(1, 2, 3, 0, SimpleTZInfo(0)).isoformat(), '01:02:03+00:00')
self.assertEquals(time(1, 2, 3, 0, SimpleTZInfo(90)).isoformat(), '01:02:03+01:30')
self.assertEquals(time(1, 2, 3, 0, SimpleTZInfo(-90)).isoformat(), '01:02:03-01:30')

30 changes: 30 additions & 0 deletions convertish/util.py
@@ -0,0 +1,30 @@
"""
General support and utility module.
"""

from datetime import timedelta, tzinfo


class SimpleTZInfo(tzinfo):
"""
Simple concrete datetime.tzinfo class that handles only
offset in minutes form UTC.
"""

def __init__(self, minutes):
self.minutes = minutes

def utcoffset(self, dt):
return timedelta(minutes=self.minutes)

def dst(self, dt):
return timedelta()

def tzname(self, dt):
if self.minutes < 0:
sign = '-'
hours, minutes = divmod(-self.minutes, 60)
else:
sign = '+'
hours, minutes = divmod(self.minutes, 60)
return '%s%02d:%02d' % (sign, hours, minutes)

0 comments on commit 2ab57b1

Please sign in to comment.