Fixed repr for FixedOffsetTimezone for offsets west of UTC (negative)

The offset displayed was always positive and somewhat confusing. The
offset displayed now is the offset that the instance was created
with.

Also added some tests for initialisation.
This commit is contained in:
Menno Smits 2012-01-17 15:51:32 +00:00 committed by Daniele Varrazzo
parent 35ff2def34
commit 1469a56512
2 changed files with 22 additions and 2 deletions

View File

@ -50,8 +50,9 @@ class FixedOffsetTimezone(datetime.tzinfo):
self._name = name self._name = name
def __repr__(self): def __repr__(self):
offset_mins = self._offset.seconds // 60 + self._offset.days * 24 * 60
return "psycopg2.tz.FixedOffsetTimezone(offset=%r, name=%r)" \ return "psycopg2.tz.FixedOffsetTimezone(offset=%r, name=%r)" \
% (self._offset.seconds // 60, self._name) % (offset_mins, self._name)
def utcoffset(self, dt): def utcoffset(self, dt):
return self._offset return self._offset

View File

@ -25,7 +25,7 @@
import math import math
import unittest import unittest
import psycopg2 import psycopg2
from psycopg2.tz import FixedOffsetTimezone from psycopg2.tz import FixedOffsetTimezone, ZERO
from testconfig import dsn from testconfig import dsn
class CommonDatetimeTestsMixin: class CommonDatetimeTestsMixin:
@ -513,6 +513,25 @@ class FromTicksTestCase(unittest.TestCase):
time(0, 11, 59, 999920)) time(0, 11, 59, 999920))
class FixedOffsetTimezoneTests(unittest.TestCase):
def test_init_with_no_args(self):
tzinfo = FixedOffsetTimezone()
self.assert_(tzinfo._offset is ZERO)
self.assert_(tzinfo._name is None)
def test_repr_with_positive_offset(self):
tzinfo = FixedOffsetTimezone(5 * 60)
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=300, name=None)")
def test_repr_with_negative_offset(self):
tzinfo = FixedOffsetTimezone(-5 * 60)
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)")
def test_repr_with_name(self):
tzinfo = FixedOffsetTimezone(name="FOO")
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=0, name='FOO')")
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)