Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple tzinfo implementation to support times with timezones.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |