YADTR (Yet Another DateTime Rant)

Skip Montanaro skip at pobox.com
Thu Mar 27 14:01:39 CET 2014


>> There are,
>> as I see it, two common cases where t is negative:
>>
>>   -1 day < t < 0
>>
>> and
>>
>>   t <= -1 day
>
> There are two types of negative numbers: Those closer to zero than -1,
> and those not closer to zero than -1. Yeah, I think those are the most
> common cases. :)

Sorry I wasn't clear. I see two cases w.r.t. *display*, those where
you need to display the number of days back (possibly accounting for
even larger time intervals to make it more human readable), those
where you don't.  Yes, as you observed, they completely cover the
negative timedelta space with two non-overlapping intervals.

Roy's original rant was about how a negative timedelta was
stringified, when what he seemed to care about was mostly the time
interval between now and the previous sunset (which will be less than
a day, unless your day length is advancing -- between the winter and
summer solstices -- and you compute that interval a few seconds before
sunset). The common "how long ago was sunset?" (|delta| < 1 day) case
is handled just fine by my little negate-the-timedelta hack, e.g.:

>>> "-%s" % (-datetime.timedelta(hours=-17, minutes=-12, seconds=-25.234))
'-17:12:25.234000'

Negative timedeltas with a magnitude of one day or more require
something else, perhaps like the ISO8601 duration stuff (which I
personally find distasteful, but that might be the best you can do in
the absence of an anchor datetime).

Skip



More information about the Python-list mailing list