本文整理汇总了Python中django.utils.dateparse.parse_time方法的典型用法代码示例。如果您正苦于以下问题:Python dateparse.parse_time方法的具体用法?Python dateparse.parse_time怎么用?Python dateparse.parse_time使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.dateparse
的用法示例。
在下文中一共展示了dateparse.parse_time方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_values
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def convert_values(self, value, field):
"""SQLite returns floats when it should be returning decimals,
and gets dates and datetimes wrong.
For consistency with other backends, coerce when required.
"""
internal_type = field.get_internal_type()
if internal_type == 'DecimalField':
return util.typecast_decimal(field.format_number(value))
elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
return int(value)
elif internal_type == 'DateField':
return parse_date(value)
elif internal_type == 'DateTimeField':
return parse_datetime_with_timezone_support(value)
elif internal_type == 'TimeField':
return parse_time(value)
# No field, or the field isn't known to be a decimal or integer
return value
示例2: to_python
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def to_python(self, value):
if value is None:
return None
if isinstance(value, datetime.time):
return value
if isinstance(value, datetime.datetime):
# Not usually a good idea to pass in a datetime here (it loses
# information), but this can be a side-effect of interacting with a
# database backend (e.g. Oracle), so we'll be accommodating.
return value.time()
try:
parsed = parse_time(value)
if parsed is not None:
return parsed
except ValueError:
msg = self.error_messages['invalid_time'] % value
raise exceptions.ValidationError(msg)
msg = self.error_messages['invalid'] % value
raise exceptions.ValidationError(msg)
示例3: convert_timefield_value
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def convert_timefield_value(self, value, expression, connection, context):
if value is not None and not isinstance(value, datetime.time):
value = parse_time(value)
return value
示例4: to_python
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def to_python(self, value):
if value is None:
return None
if isinstance(value, datetime.time):
return value
if isinstance(value, datetime.datetime):
# Not usually a good idea to pass in a datetime here (it loses
# information), but this can be a side-effect of interacting with a
# database backend (e.g. Oracle), so we'll be accommodating.
return value.time()
try:
parsed = parse_time(value)
if parsed is not None:
return parsed
except ValueError:
raise exceptions.ValidationError(
self.error_messages['invalid_time'],
code='invalid_time',
params={'value': value},
)
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
示例5: build_sub_event_time_ranges
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def build_sub_event_time_ranges(self, start_date, end_date, time_tables):
sub_event_time_ranges = []
for time_table in time_tables:
current_date = start_date
weekday = int(time_table.get('weekday'))
start_time = parse_time(time_table.get('starttime'))
end_time = parse_time(time_table.get('endtime'))
repetition = int(time_table.get('repetition'))
if repetition == 0:
repetition = 7 # assume repetition 0 and 7 mean the same thing
if not (weekday and repetition) or start_time >= end_time:
continue
while current_date.isoweekday() != weekday:
current_date += timedelta(days=1)
while current_date <= end_date:
sub_event_time_ranges.append(SubEventTimeRange(
datetime.combine(current_date, start_time).astimezone(TIMEZONE),
datetime.combine(current_date, end_time).astimezone(TIMEZONE),
))
current_date += timedelta(days=repetition)
return sub_event_time_ranges
示例6: convert_timefield_value
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def convert_timefield_value(self, value, expression, connection):
if value is not None:
if not isinstance(value, datetime.time):
value = parse_time(value)
return value
示例7: to_python
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def to_python(self, value):
if value is None or isinstance(value, datetime.time):
return value
else:
return parse_time(value)
示例8: convert_timefield_value
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def convert_timefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.time):
value = parse_time(value)
return value
示例9: from_native
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def from_native(self, value):
if value in validators.EMPTY_VALUES:
return None
if isinstance(value, datetime.time):
return value
for fmt in self.input_formats:
if fmt.lower() == ISO_8601:
try:
parsed = parse_time(value)
except (ValueError, TypeError):
pass
else:
if parsed is not None:
return parsed
else:
try:
parsed = datetime.datetime.strptime(value, fmt)
except (ValueError, TypeError):
pass
else:
return parsed.time()
msg = self.error_messages['invalid'] % readable_time_formats(self.input_formats)
raise ValidationError(msg)
示例10: parse_simple_token
# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_time [as 别名]
def parse_simple_token(token, manager, related_models, left=False, sibling_token=None):
"""
Parse the simple token dict.
:param token: a dict with 'tokenType'=='PROPERTY'|'NUMBER'|'STRING'
and 'tokenValue'==a value of the corresponding type.
:param manager: The django model manager
:param related_models: Related models joined to the manager model
:param left: Default False. If set then the token is an assignee and shouldn't be wrapped in
an F() expression if it's a property
:param sibling_token: the left side token if we are evaluating a right side token (left=False)
"""
if token['tokenType'] == 'PROPERTY':
# Resolve the field path in case it's relative to a joined related_model
field_path = resolve_field_path_via_geographies('__'.join(token['tokenValue'].split('.')), manager, related_models)
# Wrap in an F() expression if field is a right-side argument (the thing being assigned)
return field_path if left else F(field_path)
elif token['tokenType'] == 'NUMBER':
return float(token['tokenValue'])
elif token['tokenType'] in ['STRING', 'null', 'undefined']:
if token['tokenType'] in ['null', 'undefined']:
# Accept the 'null' or 'undefined' tokenType to mean None
value = None
else:
value = token['tokenValue']
if sibling_token and sibling_token.get('tokenType', None) == 'PROPERTY':
# If we are evaluating a right-side token, inspect the sibling_token (the left-side token)
# to find out what type the property is. This only matters for Dates and Times
# where we need to modify the resolved right-side value to be have a time zone
# if one isn't specified
# Resolve the possibly chained property
field_path = resolve_field_path_via_geographies('__'.join(sibling_token['tokenValue'].split('.')), manager, related_models)
field = resolve_field_of_path(manager, field_path)
if not field:
return
parser_lookup = {DateTimeField: parse_datetime, DateField: parse_date, TimeField: parse_time}
if isinstance(field, (DateTimeField, DateField, TimeField)):
date_time = parser_lookup[field.__class__](value)
if not date_time and isinstance(field, DateTimeField):
# Accept dates without times for datetimes
date_time = timezone.utc.localize(datetime.combine(parser_lookup[DateField](value), datetime.min.time()))
if isinstance(field, (DateTimeField, TimeField)) and not date_time.tzinfo:
# Default the timezone to UTC
return date_time.replace(tzinfo=timezone.utc)
return date_time
return value
# TODO handle booleans and other types
return token['tokenType']