Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime.timedelta is inconvenient to use... #50038

Closed
brianquinlan opened this issue Apr 18, 2009 · 16 comments
Closed

datetime.timedelta is inconvenient to use... #50038

brianquinlan opened this issue Apr 18, 2009 · 16 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@brianquinlan
Copy link
Contributor

BPO 5788
Nosy @birkenfeld, @brianquinlan, @mdickinson, @pitrou, @giampaolo
Files
  • totalseconds.diff: Adds a datetime.total_seconds attribute
  • totalseconds2.diff: implement total_seconds as a method
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-11-25.23:04:49.969>
    created_at = <Date 2009-04-18.18:37:39.661>
    labels = ['type-feature', 'library']
    title = 'datetime.timedelta is inconvenient to use...'
    updated_at = <Date 2011-10-21.20:34:13.603>
    user = 'https://github.com/brianquinlan'

    bugs.python.org fields:

    activity = <Date 2011-10-21.20:34:13.603>
    actor = 'bquinlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-11-25.23:04:49.969>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2009-04-18.18:37:39.661>
    creator = 'bquinlan'
    dependencies = []
    files = ['13719', '13721']
    hgrepos = []
    issue_num = 5788
    keywords = ['patch']
    message_count = 16.0
    messages = ['86132', '86133', '86135', '86136', '86138', '86147', '86148', '86167', '87768', '95726', '95732', '95733', '95740', '145928', '146115', '146131']
    nosy_count = 6.0
    nosy_names = ['georg.brandl', 'bquinlan', 'mark.dickinson', 'pitrou', 'giampaolo.rodola', 'mw44118']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5788'
    versions = ['Python 2.7', 'Python 3.2']

    @brianquinlan
    Copy link
    Contributor Author

    ...in seconds-based library functions (e.g. time.sleep) and calculations
    (e.g. distance = velocity * ?).

    @brianquinlan brianquinlan added the stdlib Python modules in the Lib dir label Apr 18, 2009
    @birkenfeld
    Copy link
    Member

    Please include a proper description of your problem, and a patch
    description when you post a patch.

    @brianquinlan
    Copy link
    Contributor Author

    I did add a patch description: "Adds a datetime.total_seconds attribute"

    • is that unclear?
    The idea is that you should be able to extract the total number of
    seconds in the duration i.e.
    >>> dt = datetime.timedelta(seconds=1234567.89)
    >>> dt.total_seconds
    1234567.89

    @birkenfeld
    Copy link
    Member

    I saw the patch description as well, but usually you put that
    description, and perhaps a motivation as well, in the comment. That way
    it's easier for people to directly see what an issue is about.

    @brianquinlan
    Copy link
    Contributor Author

    OK, a bit on motivation:

    1. datetime.timedelta instances are a convenient way of representing
      durations
    2. datetime.timedelta instances cannot be conveniently used in many
      calculations e.g. calculating distance based on velocity and time
    3. datetime.timedelta instances cannot be conveniently used in many
      library functions e.g. time.sleep(), urllib2.urlopen(timeout=)

    I propose to fix that by adding a timedelta.total_seconds attribute that
    equals:
    timedelta.days * 3600 * 24 + timedelta.seconds + timedelta.microseconds
    / 100000.0

    @pitrou
    Copy link
    Member

    pitrou commented Apr 18, 2009

    The addition looks quite legitimate to me.
    The only thing is that it may be better as a method (total_seconds())
    rather than an attribute, given the other APIs in the datetime module.
    Also, the patch lacks some unit tests.

    @pitrou pitrou added the type-feature A feature request or enhancement label Apr 18, 2009
    @pitrou
    Copy link
    Member

    pitrou commented Apr 18, 2009

    Sorry for the last comment about unit tests, they are here actually :-)

    @brianquinlan
    Copy link
    Contributor Author

    Attached is a patch that implements .total_seconds as an instance method

    @pitrou
    Copy link
    Member

    pitrou commented May 14, 2009

    Given the timing, I fear this will have to wait for 3.2.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 25, 2009

    The patch is committed in r76529 (trunk) and r76530 (py3k). Thank you!

    @pitrou pitrou closed this as completed Nov 25, 2009
    @mdickinson
    Copy link
    Member

    A late note: this would be redundant if the oft-requested division of
    timedeltas were implemented: t.total_seconds could then be spelt

    t/timedelta(seconds=1)

    with the advantage that there would then be a natural way to spell
    t.total_days or t.total_hours as well.

    @mdickinson
    Copy link
    Member

    That should be t.total_seconds(), of course.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 26, 2009

    A late note: this would be redundant if the oft-requested division of
    timedeltas were implemented: t.total_seconds could then be spelt

    t/timedelta(seconds=1)

    It would be less obvious, though.

    @giampaolo
    Copy link
    Contributor

    Sorry for commenting on a closed issue but I just bumped into a problem requiring a total_minute() method I ended up implementing by doing some raw math by hand.
    Would it be a reasonable addition?
    If so I'll open a separate issue.

    @mdickinson
    Copy link
    Member

    What about

    def total_minutes(td):
        return td / datetime.timedelta(minutes=1)

    ?

    @brianquinlan
    Copy link
    Contributor Author

    You'll probably get more traction if you file a new bug.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants