Merge pull request #272 from yoloseem/timetz

Cast time into timetz when the tzinfo field is set.
This commit is contained in:
Federico Di Gregorio 2014-11-13 10:07:02 +01:00
commit 096e0729d5
2 changed files with 15 additions and 2 deletions

View File

@ -62,7 +62,10 @@ _pydatetime_string_date_time(pydatetimeObject *self)
char *fmt = NULL; char *fmt = NULL;
switch (self->type) { switch (self->type) {
case PSYCO_DATETIME_TIME: case PSYCO_DATETIME_TIME:
fmt = "'%s'::time"; tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
if (!tz) { goto error; }
fmt = (tz == Py_None) ? "'%s'::time" : "'%s'::timetz";
Py_DECREF(tz);
break; break;
case PSYCO_DATETIME_DATE: case PSYCO_DATETIME_DATE:
fmt = "'%s'::date"; fmt = "'%s'::date";

View File

@ -287,7 +287,17 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
def test_type_roundtrip_time(self): def test_type_roundtrip_time(self):
from datetime import time from datetime import time
self._test_type_roundtrip(time(10,20,30)) tm = self._test_type_roundtrip(time(10,20,30))
self.assertEqual(None, tm.tzinfo)
def test_type_roundtrip_timetz(self):
from datetime import time
import psycopg2.tz
tz = psycopg2.tz.FixedOffsetTimezone(8*60)
tm1 = time(10,20,30, tzinfo=tz)
tm2 = self._test_type_roundtrip(tm1)
self.assertNotEqual(None, tm2.tzinfo)
self.assertEqual(tm1, tm2)
def test_type_roundtrip_interval(self): def test_type_roundtrip_interval(self):
from datetime import timedelta from datetime import timedelta