From 1469a56512a9776eb87629be7894b3bf2be89183 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 17 Jan 2012 15:51:32 +0000 Subject: [PATCH] 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. --- lib/tz.py | 3 ++- tests/test_dates.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/tz.py b/lib/tz.py index f7d9e451..e353f9b6 100644 --- a/lib/tz.py +++ b/lib/tz.py @@ -50,8 +50,9 @@ class FixedOffsetTimezone(datetime.tzinfo): self._name = name def __repr__(self): + offset_mins = self._offset.seconds // 60 + self._offset.days * 24 * 60 return "psycopg2.tz.FixedOffsetTimezone(offset=%r, name=%r)" \ - % (self._offset.seconds // 60, self._name) + % (offset_mins, self._name) def utcoffset(self, dt): return self._offset diff --git a/tests/test_dates.py b/tests/test_dates.py index b310287d..026561a2 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -25,7 +25,7 @@ import math import unittest import psycopg2 -from psycopg2.tz import FixedOffsetTimezone +from psycopg2.tz import FixedOffsetTimezone, ZERO from testconfig import dsn class CommonDatetimeTestsMixin: @@ -513,6 +513,25 @@ class FromTicksTestCase(unittest.TestCase): 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(): return unittest.TestLoader().loadTestsFromName(__name__)