classification
Title: class timedelta, support the method hours and minutes in field accessors
Type: enhancement Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, elinaldosoft, michaelanckaert, p-ganssle, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-08-22 10:48 by elinaldosoft, last changed 2019-08-25 12:51 by elinaldosoft. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15481 closed hongweipeng, 2019-08-25 03:14
Messages (7)
msg350182 - (view) Author: Elinaldo Monteiro (elinaldosoft) Date: 2019-08-22 10:48
Hello, 

Does it make sense to exist theses 2 methods in class timedelta?

@property
def hours(self):
    return self._seconds // 3600

@property
def minutes(self):
    return self._seconds // 60
msg350187 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-22 11:52
It was already proposed several times before.

The problem is that what do you expect to get from timedelta(hours=24).hours?

The idiomatic way to convert a timedelta object to a number of hours is:

td = timedelta(...)
number_of_hours = td // timedelta(hours=1)
msg350188 - (view) Author: Elinaldo Monteiro (elinaldosoft) Date: 2019-08-22 12:08
Imagine the following scenario.

from datetime import timedelta
diff = timedelta(hours=23, minutes=59) - timedelta(hours=20, minutes=45)

Which is the simplest ?

diff.hours

diff.total_seconds() // 3600
msg350198 - (view) Author: Michael Anckaert (michaelanckaert) * Date: 2019-08-22 15:35
I would support this addition. The timedelta class already has accessors for days and seconds, why not for hours and minutes? 


The implementation should make use of the idiomatic way as Serhiy mentioned.  

>>> timedelta(hours=25).seconds // 3600
1

Is wrong, it should be

>>> timedelta(hours=25) // timedelta(hours=1)
25

>>> timedelta(hours=24) // timedelta(hours=1)
24
msg350200 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2019-08-22 15:46
> I would support this addition. The timedelta class already has accessors for days and seconds, why not for hours and minutes? 

The `timedelta.days` and `timedelta.seconds` accessors do not do what is being requested here. The component accessors just give you a given component of the timedelta in its normalized form, so:

    >>> td = timedelta(hours=25, minutes=1, seconds=2)
    >>> td.days
    1
    >>> td.seconds
    3662
    >>> td // timedelta(seconds=1)
    90062


The reason there is no hours or minutes is that the normalized form of timedelta doesn't have those components. It would be inconsistent to have `hours` and `minutes` give the total duration of the timedelta in the chosen units while `.days` and `.seconds` return just the component of the normalized form.

What's really being asked for here are `total_hours()` and `total_minutes()` methods, and when that has come up in the past (including recently on the python-dev mailing list), we've largely decided that the "divide by the units you want" idiom is sufficient (and in fact better in that you can choose any arbitrary units rather than just the ones that have specific names).
msg350201 - (view) Author: Michael Anckaert (michaelanckaert) * Date: 2019-08-22 15:50
Thank you for the clarification Paul. It makes sense to me now to not
include those accessors.

On Thu, 22 Aug 2019 at 17:46, Paul Ganssle <report@bugs.python.org> wrote:

>
> Paul Ganssle <p.ganssle@gmail.com> added the comment:
>
> > I would support this addition. The timedelta class already has accessors
> for days and seconds, why not for hours and minutes?
>
> The `timedelta.days` and `timedelta.seconds` accessors do not do what is
> being requested here. The component accessors just give you a given
> component of the timedelta in its normalized form, so:
>
>     >>> td = timedelta(hours=25, minutes=1, seconds=2)
>     >>> td.days
>     1
>     >>> td.seconds
>     3662
>     >>> td // timedelta(seconds=1)
>     90062
>
>
> The reason there is no hours or minutes is that the normalized form of
> timedelta doesn't have those components. It would be inconsistent to have
> `hours` and `minutes` give the total duration of the timedelta in the
> chosen units while `.days` and `.seconds` return just the component of the
> normalized form.
>
> What's really being asked for here are `total_hours()` and
> `total_minutes()` methods, and when that has come up in the past (including
> recently on the python-dev mailing list), we've largely decided that the
> "divide by the units you want" idiom is sufficient (and in fact better in
> that you can choose any arbitrary units rather than just the ones that have
> specific names).
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue37914>
> _______________________________________
>
msg350451 - (view) Author: Elinaldo Monteiro (elinaldosoft) Date: 2019-08-25 12:51
I am closed my issue.
History
Date User Action Args
2019-08-25 12:51:46elinaldosoftsetstatus: open -> closed
resolution: rejected
messages: + msg350451

stage: patch review -> resolved
2019-08-25 03:14:11hongweipengsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15167
2019-08-22 15:50:12michaelanckaertsetmessages: + msg350201
2019-08-22 15:46:25p-gansslesetmessages: + msg350200
2019-08-22 15:35:14michaelanckaertsetnosy: + michaelanckaert
messages: + msg350198
2019-08-22 12:08:13elinaldosoftsetmessages: + msg350188
2019-08-22 11:52:11serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg350187
2019-08-22 10:51:30xtreaksetnosy: + belopolsky, p-ganssle
2019-08-22 10:48:13elinaldosoftcreate