async def test_update(self): obj0 = await testmodels.DatetimeFields.create(datetime=datetime( 2019, 9, 1, 0, 0, 0, tzinfo=get_default_timezone())) await testmodels.DatetimeFields.filter(id=obj0.id).update( datetime=datetime( 2019, 9, 1, 6, 0, 8, tzinfo=get_default_timezone())) obj = await testmodels.DatetimeFields.get(id=obj0.id) self.assertEqual( obj.datetime, datetime(2019, 9, 1, 6, 0, 8, tzinfo=get_default_timezone())) self.assertEqual(obj.datetime_null, None)
def ms_to_datetime(ms: int) -> datetime: """ ms to datetime :param ms: :return: """ return datetime.fromtimestamp(ms / 1000, timezone.get_default_timezone())
def to_db_value( self, value: Optional[Union[datetime.time, datetime.timedelta]], instance: "Union[Type[Model], Model]", ) -> Optional[Union[datetime.time, datetime.timedelta]]: # Only do this if it is a Model instance, not class. Test for guaranteed instance var if hasattr(instance, "_saved_in_db") and ( self.auto_now or (self.auto_now_add and getattr(instance, self.model_field_name) is None)): now = timezone.now().time() setattr(instance, self.model_field_name, now) return now if value is not None: if isinstance(value, datetime.timedelta): return value if get_use_tz(): if timezone.is_naive(value): warnings.warn( "TimeField %s received a naive time (%s)" " while time zone support is active." % (self.model_field_name, value), RuntimeWarning, ) value = value.replace(tzinfo=get_default_timezone()) self.validate(value) return value
def to_python_value( self, value: Any) -> Optional[Union[datetime.time, datetime.timedelta]]: if value is not None: if isinstance(value, str): value = datetime.time.fromisoformat(value) if isinstance(value, datetime.timedelta): return value if timezone.is_naive(value): value = value.replace(tzinfo=get_default_timezone()) self.validate(value) return value
def to_db_datetime(self: DatetimeField, value: Optional[datetime.datetime], instance: Union[Type[Model], Model]) -> Optional[str]: # Only do this if it is a Model instance, not class. Test for guaranteed instance var if hasattr(instance, "_saved_in_db") and ( self.auto_now or (self.auto_now_add and getattr(instance, self.model_field_name, None) is None)): if timezone.get_use_tz(): value = datetime.datetime.now(tz=pytz.utc) else: value = datetime.datetime.now(tz=timezone.get_default_timezone()) setattr(instance, self.model_field_name, value) return value.isoformat(" ") if isinstance(value, datetime.datetime): return value.isoformat(" ") return None
def to_db_time( self: TimeField, value: Optional[datetime.time], instance: Union[Type[Model], Model] ) -> Optional[str]: if hasattr(instance, "_saved_in_db") and ( self.auto_now or (self.auto_now_add and getattr(instance, self.model_field_name, None) is None) ): if timezone.get_use_tz(): value = datetime.datetime.now(tz=pytz.utc).time() else: value = datetime.datetime.now(tz=timezone.get_default_timezone()).time() setattr(instance, self.model_field_name, value) return value.isoformat() if isinstance(value, datetime.time): return value.isoformat() return None
async def test_get_utcnow(self): now = datetime.utcnow().replace(tzinfo=get_default_timezone()) await testmodels.DatetimeFields.create(datetime=now) obj = await testmodels.DatetimeFields.get(datetime=now) self.assertEqual(obj.datetime, now)