Another surprise from the datetime module

Discussion in 'Python' started by Roy Smith, Jan 30, 2014.

  1. Roy Smith

    Roy Smith Guest

    I was astounded just now to discover that datetime.timedelta doesn't
    have a replace() method (at least not in Python 2.7). Is there some
    fundamental reason why it shouldn't, or is this just an oversight?

    My immediate use case was wanting to print a timedelta without the
    fractions of seconds. The most straight-forward is:

    print td.replace(microseconds=0)

    but that doesn't work. Yes, I know I can use strftime, but (as I've
    mentioned before :)), that requires dragging up the reference page to
    figure out what grotty little format string I need. The brute-force

    print timedelta(seconds=int(td.total_seconds()))

    is easier than that, but plain old replace() would be even easier.
    Roy Smith, Jan 30, 2014
    #1
    1. Advertising

  2. On 30/01/2014 17:32, Roy Smith wrote:
    > I was astounded just now to discover that datetime.timedelta doesn't
    > have a replace() method (at least not in Python 2.7). Is there some
    > fundamental reason why it shouldn't, or is this just an oversight?
    >
    > My immediate use case was wanting to print a timedelta without the
    > fractions of seconds. The most straight-forward is:
    >
    > print td.replace(microseconds=0)
    >
    > but that doesn't work. Yes, I know I can use strftime, but (as I've
    > mentioned before :)), that requires dragging up the reference page to
    > figure out what grotty little format string I need. The brute-force
    >
    > print timedelta(seconds=int(td.total_seconds()))
    >
    > is easier than that, but plain old replace() would be even easier.
    >


    datetime.timedelta doesn't have a strftime method either.

    AttributeError: 'datetime.timedelta' object has no attribute 'strftime'

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Jan 30, 2014
    #2
    1. Advertising

  3. Roy Smith

    Neil Cerutti Guest

    On 2014-01-30, Roy Smith <> wrote:
    > I was astounded just now to discover that datetime.timedelta
    > doesn't have a replace() method (at least not in Python 2.7).
    > Is there some fundamental reason why it shouldn't, or is this
    > just an oversight?
    >
    > My immediate use case was wanting to print a timedelta without
    > the fractions of seconds. The most straight-forward is:
    >
    > print td.replace(microseconds=0)


    That would be nice.

    In the meantime, this works for your use case:

    td -= td % timedelta(seconds=1)

    --
    Neil Cerutti
    Neil Cerutti, Jan 30, 2014
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    685
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    517
    Tim Peters
    Sep 9, 2003
  3. Cameron Simpson

    Re: Another surprise from the datetime module

    Cameron Simpson, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    48
    Cameron Simpson
    Jan 31, 2014
  4. Cameron Simpson

    Re: Another surprise from the datetime module

    Cameron Simpson, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    59
    Cameron Simpson
    Jan 31, 2014
  5. Steven D'Aprano

    Re: Another surprise from the datetime module

    Steven D'Aprano, Jan 31, 2014, in forum: Python
    Replies:
    1
    Views:
    61
    Roy Smith
    Jan 31, 2014
Loading...

Share This Page