當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。