1👍
✅
What are you actually trying to do? 🙂 I’ll have to guess to begin with…
Remember TimeField
s carry datetime.time
objects, which are “clock time”, i.e. from midnight to midnight.
I’m assuming you want to convert them to a form that you can handle arithmetically — seconds since midnight works fine for that.
I’m using raw datetime.time
objects here for simplicity, but it’s the same thing with Django’s TimeFields.
import datetime, decimal
def to_seconds_after_midnight(t):
return t.hour * 60 * 60 + t.minute * 60 + t.second + (t.microsecond / 1000000.)
hourly_fee = decimal.Decimal(90) # for example.
time_1 = datetime.time(13, 15)
time_2 = datetime.time(16, 20)
second_interval = decimal.Decimal(
to_seconds_after_midnight(time_2) -
to_seconds_after_midnight(time_1)
)
print(hourly_fee * (second_interval / 60 / 60)) # The output is 277.5.
HTH 🙂
👤AKX
Source:stackexchange.com