Python django.utils.dateparse.parse_duration() Examples

The following are 30 code examples of django.utils.dateparse.parse_duration(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.utils.dateparse , or try the search function .
Example #1
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
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},
        ) 
Example #2
Source File: compat.py    From drf-cached-instances with Mozilla Public License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_simple(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #4
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_simple(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #5
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_days(self):
        duration = datetime.timedelta(days=1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #6
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_microseconds(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5, microseconds=12345)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #7
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_negative(self):
        duration = datetime.timedelta(days=-1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #8
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_simple(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_iso_string(duration)), duration) 
Example #9
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_microseconds(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5, microseconds=12345)
        self.assertEqual(parse_duration(duration_iso_string(duration)), duration) 
Example #10
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_negative(self):
        duration = datetime.timedelta(days=-1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_iso_string(duration)).total_seconds(), duration.total_seconds()) 
Example #11
Source File: fields.py    From python2017 with MIT License 5 votes vote down vote up
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 
Example #12
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_days(self):
        duration = datetime.timedelta(days=1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #13
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_microseconds(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5, microseconds=12345)
        self.assertEqual(parse_duration(duration_string(duration)), duration) 
Example #14
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_simple(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_iso_string(duration)), duration) 
Example #15
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_days(self):
        duration = datetime.timedelta(days=1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_iso_string(duration)), duration) 
Example #16
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_microseconds(self):
        duration = datetime.timedelta(hours=1, minutes=3, seconds=5, microseconds=12345)
        self.assertEqual(parse_duration(duration_iso_string(duration)), duration) 
Example #17
Source File: test_duration.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_negative(self):
        duration = datetime.timedelta(days=-1, hours=1, minutes=3, seconds=5)
        self.assertEqual(parse_duration(duration_iso_string(duration)).total_seconds(), duration.total_seconds()) 
Example #18
Source File: operations.py    From python2017 with MIT License 5 votes vote down vote up
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 
Example #19
Source File: fields.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #20
Source File: operations.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
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 
Example #21
Source File: fields.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
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 
Example #22
Source File: operations.py    From python with Apache License 2.0 5 votes vote down vote up
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 
Example #23
Source File: fields.py    From python with Apache License 2.0 5 votes vote down vote up
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 
Example #24
Source File: fields.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
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 
Example #25
Source File: tests_result.py    From ara with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #26
Source File: tests_play.py    From ara with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #27
Source File: tests_playbook.py    From ara with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #28
Source File: tests_task.py    From ara with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #29
Source File: operations.py    From bioforum with MIT License 5 votes vote down vote up
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 
Example #30
Source File: fields.py    From bioforum with MIT License 5 votes vote down vote up
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