I understand that seconds and microseconds are probably represented separately in datetime.timedelta for efficiency reasons, but I just wrote this simple function:
def to_seconds_float(timedelta):
"""Calculate floating point representation of combined
seconds/microseconds attributes in :param:`timedelta`.
:raise ValueError: If :param:`timedelta.days` is truthy.
>>> to_seconds_float(datetime.timedelta(seconds=1, milliseconds=500))
1.5
>>> too_big = datetime.timedelta(days=1, seconds=12)
>>> to_seconds_float(too_big) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: ('Must not have days', datetime.timedelta(1, 12))
"""
if timedelta.days:
raise ValueError('Must not have days', timedelta)
return timedelta.seconds + timedelta.microseconds / 1E6
This is useful for things like passing a value to time.sleep or select.select. Why isn’t something like this part of the datetime.timedelta interface? I may be missing some corner case. Time representation seems to have so many non-obvious corner cases…
I rejected days right out to have a reasonable shot at some precision (I’m too lazy to actually work out the math ATM, so this seems like a reasonable compromise ;-).
A Python float has about 15 significant digits, so with seconds being up to 86400 (5 digits to the left of the decimal point) and microseconds needing 6 digits, you could well include the days (up to several years’ worth) without loss of precision.
A good mantra is “pi seconds is a nanocentury” — about 3.14E9 seconds per 100 years, i.e. 3E7 per year, so 3E13 microseconds per year. The mantra is good because it’s memorable, even though it does require you to do a little mental arithmetic afterwards (but, like spinach, it’s GOOD for you — keeps you nimble and alert!-).
The design philosophy of
datetimeis somewhat minimalist, so it’s not surprising it omits many possible helper methods that boil down to simple arithmetic expressions.