本文整理匯總了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},
)