[Django] #33349: Add option to produce duration_string without days

8 views
Skip to first unread message

Django

unread,
Dec 8, 2021, 1:14:43 PM12/8/21
to django-...@googlegroups.com
#33349: Add option to produce duration_string without days
----------------------------------------+------------------------
Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
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 have a form with a custom field (inheriting from Django's DurationField)
allowing to input duration in the form "hh:mm".

However, when user inputs durations > "23:59", re-displaying those
durations is kind of broken ("1 ..."). Customizing `prepare_value` is
rather straightforward, however I'd like to avoid rewriting the whole
`django.utils.duration.duration_string`. If that function would accept a
`use_days` argument, it would be a lot easier to achieve my use case.

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

Django

unread,
Dec 10, 2021, 1:30:34 AM12/10/21
to django-...@googlegroups.com
#33349: Add option to produce duration_string without days
------------------------------+--------------------------------------

Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: wontfix

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

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


Comment:

Thanks for this ticket, however `duration_string` is a private
undocumented API and I don't think that adding a new option is justified
here. You can also re-use `_get_duration_components` to simplify your
implementation, e.g.:
{{{
def custom_duration_string(duration):
days, hours, minutes, seconds, microseconds =
_get_duration_components(duration)
if days:
hours += days * 24
string = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
if microseconds:
string += '.{:06d}'.format(microseconds)
return string
}}}

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

Django

unread,
Dec 10, 2021, 1:45:44 PM12/10/21
to django-...@googlegroups.com
#33349: Add option to produce duration_string without days
------------------------------+--------------------------------------

Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Claude Paroz):

OK, thanks for the code proposal.

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

Django

unread,
Dec 10, 2021, 1:55:35 PM12/10/21
to django-...@googlegroups.com
#33349: Add option to produce duration_string without days
------------------------------+--------------------------------------

Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Claude Paroz):

For my use case, I think the shorter would be:
{{{
def prepare_value(self, value):
if isinstance(value, datetime.timedelta):
seconds = value.days * 24 * 3600 + value.seconds
hours = seconds // 3600
minutes = seconds % 3600 // 60
value = '{:02d}:{:02d}'.format(hours, minutes)
return value
}}}

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

Reply all
Reply to author
Forward
0 new messages