def test_get_set_with_tzinfo(self): datetime_str = "1977-05-04 12:34:56.78" datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzutc()) variable = DateTimeVariable(tzinfo=tzutc()) # Naive timezone, from_db=True. variable.set(datetime_str, from_db=True) self.assertEquals(variable.get(), datetime_obj) variable.set(datetime_obj, from_db=True) self.assertEquals(variable.get(), datetime_obj) # Naive timezone, from_db=False (doesn't work). datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000) self.assertRaises(ValueError, variable.set, datetime_obj) # Different timezone, from_db=False. datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzoffset("1h", 3600)) variable.set(datetime_obj, from_db=False) converted_obj = variable.get() self.assertEquals(converted_obj, datetime_obj) self.assertEquals(type(converted_obj.tzinfo), tzutc) # Different timezone, from_db=True. datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzoffset("1h", 3600)) variable.set(datetime_obj, from_db=True) converted_obj = variable.get() self.assertEquals(converted_obj, datetime_obj) self.assertEquals(type(converted_obj.tzinfo), tzutc)
def test_utcdatetime_col(self): class Person(self.SQLObject): ts = UtcDateTimeCol() person = Person.get(2) self.assertEquals(person.ts, datetime.datetime(2007, 2, 5, 20, 53, 15, tzinfo=tzutc()))
def test_datetime_get_set_with_tzinfo(): datetime_str = "1977-05-04 12:34:56.78" datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzutc()) variable = DateTimeVariable(tzinfo=tzutc()) # Naive timezone, from_db=True. variable.set(datetime_str, from_db=True) assert variable.get() == datetime_obj variable.set(datetime_obj, from_db=True) assert variable.get() == datetime_obj # Naive timezone, from_db=False (doesn't work). datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000) with pytest.raises(ValueError): variable.set(datetime_obj) # Different timezone, from_db=False. datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzoffset("1h", 3600)) variable.set(datetime_obj, from_db=False) converted_obj = variable.get() assert converted_obj == datetime_obj assert type(converted_obj.tzinfo) == tzutc # Different timezone, from_db=True. datetime_obj = datetime(1977, 5, 4, 12, 34, 56, 780000, tzinfo=tzoffset("1h", 3600)) variable.set(datetime_obj, from_db=True) converted_obj = variable.get() assert converted_obj == datetime_obj assert type(converted_obj.tzinfo) == tzutc
class UtcDateTimeCol(PropertyAdapter, DateTime): _kwargs = {"tzinfo": tzutc()}