[Django] #32269: parse_duration() ISO string sign is ignored when the timedelta only has days

20 views
Skip to first unread message

Django

unread,
Dec 15, 2020, 3:34:55 AM12/15/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
--------------------------------------+------------------------
Reporter: wanaryytel | Owner: nobody
Type: Bug | Status: new
Component: Utilities | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
I'm pretty sure that this is a bug even though I'm not an expert on the
ISO 8601 standard. The sign of a timedelta string will be ignored by
`django.utils.dateparse.parse_duration` if the input string only contains
days. Compare the following (notice the minus signs):

{{{
In [4]: timedelta(days=-1)
Out[4]: datetime.timedelta(days=-1)

In [5]: td = timedelta(days=-1)

In [6]: duration_iso_string(td)
Out[6]: '-P1DT00H00M00S'

In [7]: parse_duration(duration_iso_string(td))
Out[7]: datetime.timedelta(days=1) # <-- Why is this 1 and not -1?

In [8]: td = timedelta(days=-1, microseconds=1)

In [9]: duration_iso_string(td)
Out[9]: '-P0DT23H59M59.999999S'

In [10]: parse_duration(duration_iso_string(td))
Out[10]: datetime.timedelta(days=-1, microseconds=1)

}}}


I guess the problem is in django/utils/dateparse.py line 147 that reads
`return days + sign * datetime.timedelta(**kw)`.
However, if `datetime.timedelta(**kw)` ends up being zero (`timedelta(0)`)
then the sign multiplication ends up in zero, not `-0`. This is just a
preliminary quick look though and maybe the problem is something else.

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

Django

unread,
Dec 15, 2020, 6:20:23 AM12/15/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
---------------------------+------------------------------------
Reporter: Hanno | Owner: nobody

Type: Bug | Status: new
Component: Utilities | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

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

* stage: Unreviewed => Accepted


Comment:

Great catch. Would you like to prepare a patch?

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

Django

unread,
Dec 15, 2020, 6:28:40 AM12/15/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
---------------------------+------------------------------------
Reporter: Hanno | Owner: nobody

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

Comment (by Hanno):

I can try! Hopefully I can find the time.

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

Django

unread,
Dec 15, 2020, 6:31:02 AM12/15/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
---------------------------+------------------------------------
Reporter: Hanno | Owner: Hanno
Type: Bug | Status: assigned
Component: Utilities | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+------------------------------------
Changes (by Hanno):

* owner: nobody => Hanno
* status: new => assigned


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

Django

unread,
Dec 21, 2020, 4:17:35 AM12/21/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
---------------------------+---------------------------------------------
Reporter: Hanno | Owner: starryrbs
Type: Bug | Status: assigned
Component: Utilities | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+---------------------------------------------
Changes (by Mariusz Felisiak):

* owner: Hanno => starryrbs
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/13791 PR]

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

Django

unread,
Dec 21, 2020, 6:13:47 AM12/21/20
to django-...@googlegroups.com
#32269: parse_duration() ISO string sign is ignored when the timedelta only has
days
---------------------------+---------------------------------------------
Reporter: Hanno | Owner: starryrbs
Type: Bug | Status: closed
Component: Utilities | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+---------------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"2a76f4313423a3b91caade4fce71790630ef9152" 2a76f43]:
{{{
#!CommitTicketReference repository=""
revision="2a76f4313423a3b91caade4fce71790630ef9152"
Fixed #32269 -- Fixed parse_duration() for negative days in ISO 8601
format.
}}}

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

Reply all
Reply to author
Forward
0 new messages