Re: Another surprise from the datetime module

Discussion in 'Python' started by Cameron Simpson, Jan 31, 2014.

  1. On 30Jan2014 18:36, Neil Cerutti <> wrote:
    > 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)


    Hmm. I do not like the replace() as suggested.

    Firstly, replace is a verb, and I would normally read
    td.replace(microseconds=0) as an instruction to modify td in place.
    Traditionally, such methods in python return None.
    So you would need:

    td.replace(microseconds=0)
    print td

    Then, if the intent is to modify td in place, I would far prefer a system of
    properties on timedelta objects, eg:

    # print the microseconds part
    print td.microseconds

    # set the microseconds part to zero
    td.microseconds = 0

    # print the modified timedelta
    print td

    Also, clearly, such a system needs definition: is "microseconds"
    the sub-millisecond fraction or the sub-second fraction, expressed
    in microsecond units?

    Alternatively, if td.replace() is intened to return a new timedelta
    with the specified properties, perhaps another factory would be
    cleaner:

    td2 = datetime.timedelta(td, microseconds=0)

    with a bunch of optional parameters like microseconds for modifying
    the initial value (used at call, as above).

    Finally, how much of Roy's original wish is addressable by format
    strings to print with a specified precision? No good for arithmetic,
    but perhaps ok for presentation.

    Cheers,
    --
    Cameron Simpson <>

    I knew I was a real biker when I pulled up beside a car at a stoplight and
    the people inside locked all the doors.
    Cameron Simpson, Jan 31, 2014
    #1
    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:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. Roy Smith
    Replies:
    2
    Views:
    69
    Neil Cerutti
    Jan 30, 2014
  4. Cameron Simpson

    Re: Another surprise from the datetime module

    Cameron Simpson, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    67
    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:
    69
    Roy Smith
    Jan 31, 2014
Loading...

Share This Page