datetime.timedelta.replace?

Joshua Landau joshua at landau.ws
Fri Oct 11 13:56:18 EDT 2013


On 9 October 2013 16:15, Skip Montanaro <skip at pobox.com> wrote:
> Datetime objects have a replace method, but timedelta objects don't.
> If I take the diff of two datetimes and want to zero out the
> microseconds field, is there some way to do it more cleanly than this?
>
> delta = dt1 - dt2
> zero_delta = datetime.timedelta(days=delta.days, seconds=delta.seconds)
>
> I guess that's not bad, but replace() seems cleaner (or at least more
> congruent with datetime objects).

Maybe one of

    delta - datetime.timedelta(0, 0, delta.microseconds)

or

    delta - delta % datetime.timedelta(seconds=1)

is clearer.



More information about the Python-list mailing list