使用场景:在做上课记录系统的时候,有上课开始时间和上课结束时间,需要计算这两个时间的插值,以分钟为单位。
封装方法如下:
from datetime import datetimedef sub_seconds(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:date1 = datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")date2 = datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")duration = date1 - date2return duration.secondsdef sub_minute(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:return sub_seconds(date1, date2) / 60def sub_hour(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:return sub_minute(date1, date2) / 60def sub_day(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:return sub_hour(date1, date2) / 60
经过实测,不能使用duration.seconds,而是要改成duration.total_seconds()
def sub_seconds(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:date1 = datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")date2 = datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")duration = date1 - date2return duration.total_seconds()
最终代码封装如下:
from datetime import datetime, timedeltadef sub_duration(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> timedelta:date1 = datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")date2 = datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")return date1 - date2def sub_seconds(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.total_seconds()def sub_minutes(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.total_seconds() / 60def sub_hours(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.total_seconds() / 3600def sub_days(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.daysdef sub_months(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.days / 30def sub_years(date1: str = "2024-07-11 12:33:33",date2: str = "2024-07-10 12:33:33",
) -> float:duration = sub_duration(date1, date2)return duration.days / 365
测试代码如下:
import unittest
from datetime import datetimeimport ztimeclass TestCalcSub(unittest.TestCase):def test_basic(self):date1 = "2024-07-11 01:03:04"date2 = "2021-01-01 02:01:01"date1 = datetime.strptime(date1, "%Y-%m-%d %H:%M:%S")date2 = datetime.strptime(date2, "%Y-%m-%d %H:%M:%S")d = date1 - date2print(d.total_seconds() / (24 * 60 * 60 * 365))def test_sub_seconds(self):date1 = "2024-07-11 01:03:04"date2 = "2021-01-01 02:01:01"print(ztime.calc.sub_seconds(date1, date2))print(ztime.calc.sub_minutes(date1, date2))print(ztime.calc.sub_hours(date1, date2))print(ztime.calc.sub_days(date1, date2))print(ztime.calc.sub_months(date1, date2))print(ztime.calc.sub_years(date1, date2))
测试结果如下: