本文整理汇总了Python中django.utils.timezone.make_naive方法的典型用法代码示例。如果您正苦于以下问题:Python timezone.make_naive方法的具体用法?Python timezone.make_naive怎么用?Python timezone.make_naive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.timezone
的用法示例。
在下文中一共展示了timezone.make_naive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, 'resolve_expression'):
return value
# SQLite doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
return str(value)
示例2: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
"""
Transform a datetime value to an object compatible with what is expected
by the backend driver for datetime columns.
If naive datetime is passed assumes that is in UTC. Normally Django
models.DateTimeField makes sure that if USE_TZ is True passed datetime
is timezone aware.
"""
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, 'resolve_expression'):
return value
# cx_Oracle doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
return Oracle_datetime.from_datetime(value)
示例3: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, 'resolve_expression'):
return value
# MySQL doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
if not self.connection.features.supports_microsecond_precision:
value = value.replace(microsecond=0)
return str(value)
示例4: get_start_date
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def get_start_date(self, qs):
most_recent_kwargs = self.get_most_recent_kwargs()
last_stat = self.statistic_model.objects.most_recent(
**most_recent_kwargs)
if last_stat:
start_date = last_stat.date
else:
first_instance = qs.order_by(self.date_field).first()
if first_instance is None:
# No data
return
start_date = getattr(first_instance, self.date_field)
if start_date and isinstance(start_date, datetime):
if timezone.is_aware(start_date):
start_date = timezone.make_naive(start_date).date()
else:
start_date = start_date.date()
return start_date
示例5: add_field
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def add_field(self, k, v):
if not isinstance(k, str) or not k:
raise ValueError("Invalid field name {}".format(k))
if k in self.fields:
raise ValueError("Field {} already added".format(k))
if self.is_empty_value(v):
return
elif isinstance(v, int):
v = str(v)
elif isinstance(v, datetime):
if is_aware(v):
v = make_naive(v)
v = v.isoformat()
elif isinstance(v, list):
assert(all([isinstance(e, str) and len(e) == 40 for e in v]))
elif not isinstance(v, str):
raise ValueError("Invalid field value {} for field {}".format(v, k))
self.fields[k] = v
示例6: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, 'resolve_expression'):
return value
# SQLite doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
return six.text_type(value)
示例7: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, 'resolve_expression'):
return value
# MySQL doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
if not self.connection.features.supports_microsecond_precision:
value = value.replace(microsecond=0)
return six.text_type(value)
示例8: localize_event_datetimes
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def localize_event_datetimes(apps, schema_editor):
Event = MyModel = apps.get_model("events", "Event")
for event in Event.objects.all():
utc_tz = pytz.timezone("UTC")
event_tz = get_event_timezone(event)
print("Converting event %s to %s" % (event.id, event_tz))
event.start_time = utc_tz.localize(
timezone.make_naive(
event_tz.localize(timezone.make_naive(event.start_time, pytz.utc))
)
)
event.end_time = utc_tz.localize(
timezone.make_naive(
event_tz.localize(timezone.make_naive(event.end_time, pytz.utc))
)
)
event.save()
示例9: localize_commonevent_datetimes
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def localize_commonevent_datetimes(apps, schema_editor):
CommonEvent = MyModel = apps.get_model("events", "CommonEvent")
for event in CommonEvent.objects.all():
utc_tz = pytz.timezone("UTC")
event_tz = get_event_timezone(event)
print("Converting common event %s to %s" % (event.id, event_tz))
event.start_time = utc_tz.localize(
timezone.make_naive(
event_tz.localize(timezone.make_naive(event.start_time, pytz.utc))
)
)
event.end_time = utc_tz.localize(
timezone.make_naive(
event_tz.localize(timezone.make_naive(event.end_time, pytz.utc))
)
)
event.save()
示例10: to_python
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def to_python(self, value):
if value is None:
return value
if isinstance(value, datetime.datetime):
if settings.USE_TZ and timezone.is_aware(value):
# Convert aware datetimes to the default time zone
# before casting them to dates (#17742).
default_timezone = timezone.get_default_timezone()
value = timezone.make_naive(value, default_timezone)
return value.date()
if isinstance(value, datetime.date):
return value
try:
parsed = parse_date(value)
if parsed is not None:
return parsed
except ValueError:
msg = self.error_messages['invalid_date'] % value
raise exceptions.ValidationError(msg)
msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
示例11: adapt_datetimefield_value
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def adapt_datetimefield_value(self, value):
"""
Transform a datetime value to an object compatible with what is expected
by the backend driver for datetime columns.
If naive datetime is passed assumes that is in UTC. Normally Django
models.DateTimeField makes sure that if USE_TZ is True passed datetime
is timezone aware.
"""
if value is None:
return None
# cx_Oracle doesn't support tz-aware datetimes
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
return Oracle_datetime.from_datetime(value)
示例12: test_get_modified_time
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def test_get_modified_time(self):
naive_date = datetime(2017, 1, 2, 3, 4, 5, 678)
aware_date = timezone.make_aware(naive_date, timezone.utc)
self.storage._bucket = mock.MagicMock()
blob = mock.MagicMock()
blob.updated = aware_date
self.storage._bucket.get_blob.return_value = blob
with self.settings(TIME_ZONE='America/Montreal', USE_TZ=False):
mt = self.storage.get_modified_time(self.filename)
self.assertTrue(timezone.is_naive(mt))
naive_date_montreal = timezone.make_naive(aware_date)
self.assertEqual(mt, naive_date_montreal)
self.storage._bucket.get_blob.assert_called_with(self.filename)
with self.settings(TIME_ZONE='America/Montreal', USE_TZ=True):
mt = self.storage.get_modified_time(self.filename)
self.assertTrue(timezone.is_aware(mt))
self.assertEqual(mt, aware_date)
self.storage._bucket.get_blob.assert_called_with(self.filename)
示例13: test_get_created_time
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def test_get_created_time(self):
naive_date = datetime(2017, 1, 2, 3, 4, 5, 678)
aware_date = timezone.make_aware(naive_date, timezone.utc)
self.storage._bucket = mock.MagicMock()
blob = mock.MagicMock()
blob.time_created = aware_date
self.storage._bucket.get_blob.return_value = blob
with self.settings(TIME_ZONE='America/Montreal', USE_TZ=False):
mt = self.storage.get_created_time(self.filename)
self.assertTrue(timezone.is_naive(mt))
naive_date_montreal = timezone.make_naive(aware_date)
self.assertEqual(mt, naive_date_montreal)
self.storage._bucket.get_blob.assert_called_with(self.filename)
with self.settings(TIME_ZONE='America/Montreal', USE_TZ=True):
mt = self.storage.get_created_time(self.filename)
self.assertTrue(timezone.is_aware(mt))
self.assertEqual(mt, aware_date)
self.storage._bucket.get_blob.assert_called_with(self.filename)
示例14: to_current_timezone
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def to_current_timezone(value):
"""
When time zone support is enabled, convert aware datetimes
to naive dateimes in the current time zone for display.
"""
if settings.USE_TZ and value is not None and timezone.is_aware(value):
current_timezone = timezone.get_current_timezone()
return timezone.make_naive(value, current_timezone)
return value
示例15: to_python
# 需要导入模块: from django.utils import timezone [as 别名]
# 或者: from django.utils.timezone import make_naive [as 别名]
def to_python(self, value):
if value is None:
return value
if isinstance(value, datetime.datetime):
if settings.USE_TZ and timezone.is_aware(value):
# Convert aware datetimes to the default time zone
# before casting them to dates (#17742).
default_timezone = timezone.get_default_timezone()
value = timezone.make_naive(value, default_timezone)
return value.date()
if isinstance(value, datetime.date):
return value
try:
parsed = parse_date(value)
if parsed is not None:
return parsed
except ValueError:
raise exceptions.ValidationError(
self.error_messages['invalid_date'],
code='invalid_date',
params={'value': value},
)
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)