Skip to content

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

Closed

Description

@brianquinlan
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']

    Activity

    1. brianquinlan commented on Apr 18, 2009

      @brianquinlan
      ContributorAuthor

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

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Apr 18, 2009
    3. birkenfeld commented on Apr 18, 2009

      @birkenfeld
      Member

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

    4. brianquinlan commented on Apr 18, 2009

      @brianquinlan
      ContributorAuthor

      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
    5. birkenfeld commented on Apr 18, 2009

      @birkenfeld
      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.

    6. brianquinlan commented on Apr 18, 2009

      @brianquinlan
      ContributorAuthor

      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

    7. pitrou commented on Apr 18, 2009

      @pitrou
      Member

      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.

    8. pitrou commented on Apr 18, 2009

      @pitrou
      Member

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

    9. brianquinlan commented on Apr 19, 2009

      @brianquinlan
      ContributorAuthor

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

    10. pitrou commented on May 14, 2009

      @pitrou
      Member

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

    11. pitrou commented on Nov 25, 2009

      @pitrou
      Member

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

    12. mdickinson commented on Nov 26, 2009

      @mdickinson
      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.

    13. mdickinson commented on Nov 26, 2009

      @mdickinson
      Member

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

    14. pitrou commented on Nov 26, 2009

      @pitrou
      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)

      It would be less obvious, though.

    15. giampaolo commented on Oct 19, 2011

      @giampaolo
      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.

    16. mdickinson commented on Oct 21, 2011

      @mdickinson
      Member

      What about

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

      ?

    17. brianquinlan commented on Oct 21, 2011

      @brianquinlan
      ContributorAuthor

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

    18. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions