Coverage for src/dtexp/addition.py: 100%
5 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 14:19 +0100
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 14:19 +0100
1"""Handle addition to datetimes."""
3import datetime
5import pendulum
8def apply_addition_via_pendulum(
9 dt: datetime.datetime,
10 years: int = 0,
11 months: int = 0,
12 weeks: int = 0,
13 days: int = 0,
14 hours: int = 0,
15 minutes: int = 0,
16 seconds: int = 0,
17 microseconds: int = 0,
18) -> datetime.datetime:
19 """Use pendulum to add (or substract) from datetime intelligently.
21 Using pendulum's add method provided human-expected results when
22 adding or substracting e.g. months or years.
23 """
24 pendulum_result = pendulum.instance(dt).add(
25 years=years,
26 months=months,
27 weeks=weeks,
28 days=days,
29 hours=hours,
30 minutes=minutes,
31 seconds=seconds,
32 microseconds=microseconds,
33 )
35 return datetime.datetime(
36 pendulum_result.year,
37 pendulum_result.month,
38 pendulum_result.day,
39 pendulum_result.hour,
40 pendulum_result.minute,
41 pendulum_result.second,
42 pendulum_result.microsecond,
43 tzinfo=dt.tzinfo, # re-attach original timezone info
44 )