当前位置: 首页>>代码示例>>Python>>正文


Python dateparse.parse_duration方法代码示例

本文整理汇总了Python中django.utils.dateparse.parse_duration方法的典型用法代码示例。如果您正苦于以下问题:Python dateparse.parse_duration方法的具体用法?Python dateparse.parse_duration怎么用?Python dateparse.parse_duration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.utils.dateparse的用法示例。


在下文中一共展示了dateparse.parse_duration方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: to_python

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, datetime.timedelta):
            return value
        try:
            parsed = parse_duration(value)
        except ValueError:
            pass
        else:
            if parsed is not None:
                return parsed

        raise exceptions.ValidationError(
            self.error_messages['invalid'],
            code='invalid',
            params={'value': value},
        ) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:20,代码来源:__init__.py

示例2: parse_duration

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def parse_duration(value):
        """Parse a duration string and returns a datetime.timedelta.

        The preferred format for durations in Django is '%d %H:%M:%S.%f'.

        Also supports ISO 8601 representation.
        """
        match = standard_duration_re.match(value)
        if not match:
            match = iso8601_duration_re.match(value)
        if match:
            kw = match.groupdict()
            if kw.get('microseconds'):
                kw['microseconds'] = kw['microseconds'].ljust(6, '0')
            kw = dict([
                (k, float(v)) for k, v in six.iteritems(kw)
                if v is not None])
            return timedelta(**kw) 
开发者ID:jwhitlock,项目名称:drf-cached-instances,代码行数:20,代码来源:compat.py

示例3: to_python

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def to_python(self, value):
        if value in self.empty_values:
            return None
        if isinstance(value, datetime.timedelta):
            return value
        value = parse_duration(value)
        if value is None:
            raise ValidationError(self.error_messages['invalid'], code='invalid')
        return value 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:11,代码来源:fields.py

示例4: convert_durationfield_value

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def convert_durationfield_value(self, value, expression, connection, context):
        if value is not None:
            value = str(decimal.Decimal(value) / decimal.Decimal(1000000))
            value = parse_duration(value)
        return value 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:7,代码来源:operations.py

示例5: _sqlite_format_dtdelta

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def _sqlite_format_dtdelta(conn, lhs, rhs):
    """
    LHS and RHS can be either:
        - An integer number of microseconds
        - A string representing a timedelta object
        - A string representing a datetime
    """
    try:
        if isinstance(lhs, six.integer_types):
            lhs = str(decimal.Decimal(lhs) / decimal.Decimal(1000000))
        real_lhs = parse_duration(lhs)
        if real_lhs is None:
            real_lhs = backend_utils.typecast_timestamp(lhs)
        if isinstance(rhs, six.integer_types):
            rhs = str(decimal.Decimal(rhs) / decimal.Decimal(1000000))
        real_rhs = parse_duration(rhs)
        if real_rhs is None:
            real_rhs = backend_utils.typecast_timestamp(rhs)
        if conn.strip() == '+':
            out = real_lhs + real_rhs
        else:
            out = real_lhs - real_rhs
    except (ValueError, TypeError):
        return None
    # typecast_timestamp returns a date or a datetime without timezone.
    # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
    return str(out) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:29,代码来源:base.py

示例6: to_python

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def to_python(self, value):
        if value in self.empty_values:
            return None
        if isinstance(value, datetime.timedelta):
            return value
        value = parse_duration(str(value))
        if value is None:
            raise ValidationError(self.error_messages['invalid'], code='invalid')
        return value 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:11,代码来源:fields.py

示例7: convert_durationfield_value

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def convert_durationfield_value(self, value, expression, connection):
        if value is not None:
            value = str(decimal.Decimal(value) / decimal.Decimal(1000000))
            value = parse_duration(value)
        return value 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:7,代码来源:operations.py

示例8: test_get_task_duration

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_get_task_duration(self):
        started = timezone.now()
        ended = started + datetime.timedelta(hours=1)
        task = factories.TaskFactory(started=started, ended=ended)
        request = self.client.get("/api/v1/tasks/%s" % task.id)
        self.assertEqual(parse_duration(request.data["duration"]), ended - started) 
开发者ID:ansible-community,项目名称:ara,代码行数:8,代码来源:tests_task.py

示例9: test_get_playbook_duration

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_get_playbook_duration(self):
        started = timezone.now()
        ended = started + datetime.timedelta(hours=1)
        playbook = factories.PlaybookFactory(started=started, ended=ended)
        request = self.client.get("/api/v1/playbooks/%s" % playbook.id)
        self.assertEqual(parse_duration(request.data["duration"]), ended - started) 
开发者ID:ansible-community,项目名称:ara,代码行数:8,代码来源:tests_playbook.py

示例10: test_get_play_duration

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_get_play_duration(self):
        started = timezone.now()
        ended = started + datetime.timedelta(hours=1)
        play = factories.PlayFactory(started=started, ended=ended)
        request = self.client.get("/api/v1/plays/%s" % play.id)
        self.assertEqual(parse_duration(request.data["duration"]), ended - started) 
开发者ID:ansible-community,项目名称:ara,代码行数:8,代码来源:tests_play.py

示例11: test_get_result_duration

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_get_result_duration(self):
        started = timezone.now()
        ended = started + datetime.timedelta(hours=1)
        result = factories.ResultFactory(started=started, ended=ended)
        request = self.client.get("/api/v1/results/%s" % result.id)
        self.assertEqual(parse_duration(request.data["duration"]), ended - started) 
开发者ID:ansible-community,项目名称:ara,代码行数:8,代码来源:tests_result.py

示例12: to_python

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def to_python(self, value):
        if value in self.empty_values:
            return None
        if isinstance(value, datetime.timedelta):
            return value
        try:
            value = parse_duration(str(value))
        except OverflowError:
            raise ValidationError(self.error_messages['overflow'].format(
                min_days=datetime.timedelta.min.days,
                max_days=datetime.timedelta.max.days,
            ), code='overflow')
        if value is None:
            raise ValidationError(self.error_messages['invalid'], code='invalid')
        return value 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:17,代码来源:fields.py

示例13: to_python

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def to_python(self, value):
        if value in self.empty_values:
            return None
        if isinstance(value, datetime.timedelta):
            return value
        value = parse_duration(force_text(value))
        if value is None:
            raise ValidationError(self.error_messages['invalid'], code='invalid')
        return value 
开发者ID:Yeah-Kun,项目名称:python,代码行数:11,代码来源:fields.py

示例14: test_simple

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_simple(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
开发者ID:nesdis,项目名称:djongo,代码行数:5,代码来源:test_duration.py

示例15: test_days

# 需要导入模块: from django.utils import dateparse [as 别名]
# 或者: from django.utils.dateparse import parse_duration [as 别名]
def test_days(self):
        duration = datetime.timedelta(days=1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
开发者ID:nesdis,项目名称:djongo,代码行数:5,代码来源:test_duration.py


注:本文中的django.utils.dateparse.parse_duration方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。