On 30Jan2014 18:36, Neil Cerutti <ne...@norwich.edu> wrote: > On 2014-01-30, Roy Smith <r...@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 <c...@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. -- https://mail.python.org/mailman/listinfo/python-list