Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65029 > unrolled thread

Another surprise from the datetime module

Started byroy@panix.com (Roy Smith)
First post2014-01-30 12:32 -0500
Last post2014-01-30 18:36 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Another surprise from the datetime module roy@panix.com (Roy Smith) - 2014-01-30 12:32 -0500
    Re: Another surprise from the datetime module Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-30 18:03 +0000
    Re: Another surprise from the datetime module Neil Cerutti <neilc@norwich.edu> - 2014-01-30 18:36 +0000

#65029 — Another surprise from the datetime module

Fromroy@panix.com (Roy Smith)
Date2014-01-30 12:32 -0500
SubjectAnother surprise from the datetime module
Message-ID<lce2bf$4fo$1@panix2.panix.com>
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.

[toc] | [next] | [standalone]


#65031

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-01-30 18:03 +0000
Message-ID<mailman.6154.1391105019.18130.python-list@python.org>
In reply to#65029
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

[toc] | [prev] | [next] | [standalone]


#65033

FromNeil Cerutti <neilc@norwich.edu>
Date2014-01-30 18:36 +0000
Message-ID<mailman.6155.1391107018.18130.python-list@python.org>
In reply to#65029
On 2014-01-30, Roy Smith <roy@panix.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web