本文整理汇总了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)