本文整理匯總了Python中django.forms.utils.from_current_timezone方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.from_current_timezone方法的具體用法?Python utils.from_current_timezone怎麽用?Python utils.from_current_timezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms.utils
的用法示例。
在下文中一共展示了utils.from_current_timezone方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compress
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def compress(self, data_list):
"""
On the DateTime widget, we don't want the time to be required, only the date:
- if the time is not set on a form, we set it to 0:00:00,
- if only the time is set, we ignore the value.
"""
if data_list:
if data_list[0] in self.empty_values:
# If there is no date, we ignore the time and act as if nothing was submitted
return None
if data_list[1] in self.empty_values:
# If there is a date but no time, we take midnight as default time
data_list[1] = time(0)
result = datetime.combine(*data_list)
return from_current_timezone(result)
return None
示例2: to_python
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def to_python(self, value):
"""
Validates that the input can be converted to a datetime. Returns a
Python datetime.datetime object.
"""
if value in self.empty_values:
return None
if isinstance(value, datetime.datetime):
return from_current_timezone(value)
if isinstance(value, datetime.date):
result = datetime.datetime(value.year, value.month, value.day)
return from_current_timezone(result)
if isinstance(value, list):
# Input comes from a SplitDateTimeWidget, for example. So, it's two
# components: date and time.
warnings.warn(
'Using SplitDateTimeWidget with DateTimeField is deprecated. '
'Use SplitDateTimeField instead.',
RemovedInDjango19Warning, stacklevel=2)
if len(value) != 2:
raise ValidationError(self.error_messages['invalid'], code='invalid')
if value[0] in self.empty_values and value[1] in self.empty_values:
return None
value = '%s %s' % tuple(value)
result = super(DateTimeField, self).to_python(value)
return from_current_timezone(result)
示例3: compress
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def compress(self, data_list):
if data_list:
# Raise a validation error if time or date is empty
# (possible if SplitDateTimeField has required=False).
if data_list[0] in self.empty_values:
raise ValidationError(self.error_messages['invalid_date'], code='invalid_date')
if data_list[1] in self.empty_values:
raise ValidationError(self.error_messages['invalid_time'], code='invalid_time')
result = datetime.datetime.combine(*data_list)
return from_current_timezone(result)
return None
示例4: parse_datetime_value
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def parse_datetime_value(value: Any) -> datetime:
parsed_value = parse_value(value, datetime_input_formats)
if not parsed_value:
raise ValueError(_("Enter a valid date/time."))
return from_current_timezone(parsed_value)
示例5: to_python
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def to_python(self, value):
"""
Validate that the input can be converted to a datetime. Return a
Python datetime.datetime object.
"""
if value in self.empty_values:
return None
if isinstance(value, datetime.datetime):
return from_current_timezone(value)
if isinstance(value, datetime.date):
result = datetime.datetime(value.year, value.month, value.day)
return from_current_timezone(result)
result = super().to_python(value)
return from_current_timezone(result)
示例6: to_python
# 需要導入模塊: from django.forms import utils [as 別名]
# 或者: from django.forms.utils import from_current_timezone [as 別名]
def to_python(self, value):
"""
Validates that the input can be converted to a datetime. Returns a
Python datetime.datetime object.
"""
if value in self.empty_values:
return None
if isinstance(value, datetime.datetime):
return from_current_timezone(value)
if isinstance(value, datetime.date):
result = datetime.datetime(value.year, value.month, value.day)
return from_current_timezone(result)
result = super(DateTimeField, self).to_python(value)
return from_current_timezone(result)