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


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

Re: Another surprise from the datetime module

Started byCameron Simpson <cs@zip.com.au>
First post2014-01-31 11:06 +1100
Last post2014-01-31 11:06 +1100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Another surprise from the datetime module Cameron Simpson <cs@zip.com.au> - 2014-01-31 11:06 +1100

#65072 — Re: Another surprise from the datetime module

FromCameron Simpson <cs@zip.com.au>
Date2014-01-31 11:06 +1100
SubjectRe: Another surprise from the datetime module
Message-ID<mailman.6182.1391127922.18130.python-list@python.org>
On 30Jan2014 18:36, Neil Cerutti <neilc@norwich.edu> wrote:
> 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)

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 <cs@zip.com.au>

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.

[toc] | [standalone]


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


csiph-web