Вы можете использовать datetime.timedelta
вместо datetime.time
:
import datetime
import numpy as np
start_per_day = np.array([datetime.timedelta(hours=9, minutes=43), datetime.timedelta(hours=9, minutes=51)])
end_per_day = np.array([datetime.timedelta(hours=18, minutes=22), datetime.timedelta(hours=18, minutes=29)])
delta_per_day = end_per_day - start_per_day
print(delta_per_day[0])
print(delta_per_day[1])
Я искал способ вычесть datetime.time
объектов, но не смог найти ни одного.
Тестирование преобразования (без al oop):
t1 = np.array([datetime.time(9, 43),datetime.time(9, 51)])
t2 = np.array([datetime.time(18, 22), datetime.time(18, 29)])
start_per_day = np.array([datetime.timedelta(hours=t1[0].hour, minutes=t1[0].minute), datetime.timedelta(hours=t1[1].hour, minutes=t1[1].minute)])
end_per_day = np.array([datetime.timedelta(hours=t2[0].hour, minutes=t2[0].minute), datetime.timedelta(hours=t2[1].hour, minutes=t2[1].minute)])
delta_per_day = end_per_day - start_per_day
print(delta_per_day[0])
print(delta_per_day[1])
Преобразование массива datetime.time
объектов в массив datetime.timedelta
объектов:
def time_to_delat(t):
"""Convert datetime.time object with hour and minute to datetime.timedelta object"""
dt = datetime.timedelta(hours=t.hour, minutes=t.minute)
return dt
start_per_day = np.array([datetime.time(9, 43),datetime.time(9, 51)])
end_per_day = np.array([datetime.time(18, 22), datetime.time(18, 29)])
# Convert array of datetime.time objects to array of datetime.timedelta objects
start_per_day_dt = np.array([time_to_delat(x) for x in start_per_day])
end_per_day_dt = np.array([time_to_delat(x) for x in end_per_day])
delta_per_day = end_per_day_dt - start_per_day_dt
print(delta_per_day[0])
print(delta_per_day[1])