timedeltacal/tests/test_create_from_datetimes.py

72 lines
2.6 KiB
Python

from datetime import datetime
from zoneinfo import ZoneInfo
from timedeltacal import timedeltacal
# Some basic tests. These serve as a specification. All the tests use timezone
# aware datetimes in a timezone with DST, since this is the most complex case.
# Naive datetimes and dates should work analogously.
def test_create_from_datetimes():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 4, 17, 10, 37, 51, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
assert d.months == 0
assert d.days == 0
assert d.hours == 0
assert d.minutes == 0
assert d.seconds == 30
assert d.microseconds == 0
# Subtract two datetimes - various combinations
def test_sub_same_day():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 4, 17, 13, 12, 17, 789000, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_next_day():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 4, 18, 0, 12, 17, 789000, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_days():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 4, 18, 10, 37, 21, 0, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_days_to_next_month():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 5, 7, 10, 37, 21, 0, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_months():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 5, 17, 10, 37, 21, 0, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_years():
t0 = datetime(2022, 4, 17, 10, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2023, 4, 17, 10, 37, 21, 0, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2
def test_sub_same_day_dst():
t0 = datetime(2022, 3, 27, 1, 37, 21, tzinfo=ZoneInfo("Europe/Vienna"))
t1 = datetime(2022, 3, 27, 5, 12, 17, tzinfo=ZoneInfo("Europe/Vienna"))
d = timedeltacal(t0=t0, t1=t1)
t2 = t0 + d
assert t1 == t2