[Django] #25060: Add support for str(timedelta) representation in parse_duration

12 views
Skip to first unread message

Django

unread,
Jul 3, 2015, 3:13:30 PM7/3/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------
Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------
It's useful for conversions to/from str, so we can convert timedelta value
to str and back without additional work.
The following code can be used instead of original parse_duration function
{{{
# Support str(timedelta) format
str_timedelta_duration_re = re.compile(
r'^'
r'(?:(?P<days>-?\d+) days*, )?'
r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
r'(?:(?P<minutes>\d+):)?'
r'(?P<seconds>\d+)'
r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
r'$'
)

def parse_duration(value):
"""Parses 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 and str(timedelta) representations.
"""
for duration_re in (standard_duration_re, iso8601_duration_re,
str_timedelta_duration_re):
match = duration_re.match(value)
if match:
kw = match.groupdict()
if kw.get('microseconds'):
kw['microseconds'] = kw['microseconds'].ljust(6, '0')
kw = {k: float(v) for k, v in six.iteritems(kw) if v is not
None}
return datetime.timedelta(**kw)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25060>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 3, 2015, 6:38:44 PM7/3/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Can you elaborate on your use case a bit?

`parse_duration()` is currently the inverse of
`django.utils.duration.duration_string()` which is not English specific.
Maybe you can use that function instead of `str()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:1>

Django

unread,
Jul 5, 2015, 10:13:10 AM7/5/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by vmspike):

* cc: vmspike (added)


Comment:

My

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:2>

Django

unread,
Jul 5, 2015, 10:38:58 AM7/5/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by vmspike):

My specific use case is DurationField in [http://djangonauts.github.io
/django-hstore/ django-hstore] schema mode (but I suppose it can be useful
in other situations).

Without this patch `to_python()` raise an error about inappropriate
format. I agree that in my use case it's preferable to use
`duration_string()` instead of `str()` for conversion, but I still didn't
find the place where I can override this behavior on django-hstore side
:).

Seems now I understand why you don't implement it: str(timedelta) output
can be different depends on locale? If so this patch is controversial
point.

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:3>

Django

unread,
Jul 6, 2015, 9:53:30 AM7/6/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by timgraham):

Have you raised the issue with django-hstore? I wonder if it should be
calling the form field
[https://github.com/django/django/blob/69483e022ac3ddd02f086a264c3eb5e3345ccc0c/django/forms/fields.py#L524-L527
prepare_value()] method?

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:4>

Django

unread,
Jul 7, 2015, 6:43:44 AM7/7/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by vmspike):

For now - no. DurationField is not in the list of officially supported
field types, docs say that some other fields can work as well but some of
them need additional work. Seems for other types `str()` method is called
by default. So DurationField support is just new feature for django-
hstore. I'm working on support of additional fields but not as the part of
django.

I've put `parse_duration()` patch here because I thought it can be useful
for django core itself. English specificity can be avoided using more
versatile regex like:
{{{


str_timedelta_duration_re = re.compile(
r'^'

r'(?:(?P<days>-?\d+) \w+, )?'


r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
r'(?:(?P<minutes>\d+):)?'
r'(?P<seconds>\d+)'
r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
r'$'
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:5>

Django

unread,
Jul 7, 2015, 3:05:49 PM7/7/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timgraham):

* cc: mjtamlyn (added)


Comment:

Marc, your thoughts on this?

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:6>

Django

unread,
Jul 8, 2015, 2:36:10 AM7/8/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by mjtamlyn):

I can see the merit in supporting the the str(timedelta) format in
English. The intention was to encourage locale independent representation
of the timedelta. I am completely unfamiliar with what str(timedelta) does
in other locales, if we could support it generically that would be great.

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:7>

Django

unread,
Jul 8, 2015, 4:52:26 AM7/8/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by vmspike):

Seems str(timedelta) output is locale independent. I've checked on several
locales and all the time output is the same.
{{{
>>> for loc in ('ar_AE', 'ar_BH', 'ar_DZ', 'ar_EG', 'ar_IN', 'ar_IQ',
'ar_JO', 'ar_KW', 'ar_LB', 'ar_LY', 'ar_MA', 'ar_OM', 'ar_QA', 'ar_SA',
'ar_SD', 'ar_SY', 'ar_TN', 'ar_YE', 'C', 'de_AT', 'de_BE', 'de_CH',
'de_DE', 'de_LI', 'de_LU', 'en_AG', 'en_AU', 'en_BW', 'en_CA', 'en_DK',
'en_GB', 'en_HK', 'en_IE', 'en_IN', 'en_NG', 'en_NZ', 'en_PH', 'en_SG',
'en_US', 'en_ZA', 'en_ZM', 'en_ZW', 'it_CH', 'it_IT', 'ja_JP', 'ko_KR',
'mr_IN', 'POSIX', 'ru_RU', 'ru_UA', 'sa_IN', 'tr_CY', 'tr_TR', 'zh_HK',
'zh_TW', 'zu_ZA', ):
... locale.setlocale(locale.LC_ALL, (loc, 'utf-8'))
... str(timedelta(777, 0, 1))
...
'ar_AE.UTF-8'
'777 days, 0:00:00.000001'
'ar_BH.UTF-8'
'777 days, 0:00:00.000001'
etc....
}}}
Also there are some topics in web about formating str(timedelta) according
to locale, it shows str(timedelta) is locale independent. So, I suppose,
we can use English regex.

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:8>

Django

unread,
Jul 8, 2015, 7:41:40 AM7/8/15
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+------------------------------------

Reporter: vmspike | Owner: nobody
Type: New feature | Status: new
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by timgraham):

* needs_docs: 0 => 1
* version: 1.8 => master
* needs_tests: 0 => 1
* easy: 1 => 0
* stage: Unreviewed => Accepted


Comment:

The patch also requires tests and documentation updates. See also the
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/submitting-patches/#patch-review-checklist patch review checklist].

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:9>

Django

unread,
Apr 19, 2017, 5:51:22 AM4/19/17
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+------------------------------------
Reporter: Mikhail | Owner: nobody

Type: New feature | Status: new
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by Matthew Schinckel):

I think the parse_duration() function already does this.

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:10>

Django

unread,
Nov 22, 2019, 1:13:32 PM11/22/19
to django-...@googlegroups.com
#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+------------------------------------
Reporter: Mikhail | Owner: nobody
Type: New feature | Status: closed
Component: Utilities | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Baptiste Mispelon):

* status: new => closed
* resolution: => fixed


Comment:

Replying to [comment:10 Matthew Schinckel]:


> I think the parse_duration() function already does this.

That's correct. It looks like this was inadvertently added with #24897
(262d4db8c4c849b0fdd84550fb96472446cf90df).

There are even tests for a variety of different `timedelta` cases:
https://github.com/django/django/blob/a5855c8f0fe17b7e888bd8137874ef78012a7294/tests/utils_tests/test_dateparse.py#L54

--
Ticket URL: <https://code.djangoproject.com/ticket/25060#comment:11>

Reply all
Reply to author
Forward
0 new messages