Skip to content

Commit 0e60fda

Browse files
committed
Preserve endpoint folds when calculating interval components
1 parent 7149d52 commit 0e60fda

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/pendulum/interval.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def __init__(self, start: _T, end: _T, absolute: bool = False) -> None:
140140
start.second,
141141
start.microsecond,
142142
tzinfo=start.tzinfo,
143+
fold=start.fold,
143144
),
144145
)
145146
else:
@@ -166,6 +167,7 @@ def __init__(self, start: _T, end: _T, absolute: bool = False) -> None:
166167
end.second,
167168
end.microsecond,
168169
tzinfo=end.tzinfo,
170+
fold=end.fold,
169171
),
170172
)
171173
else:

‎tests/interval/test_construct.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from datetime import datetime
44

5+
import pytest
6+
57
import pendulum
68

79
from tests.conftest import assert_datetime
@@ -119,3 +121,61 @@ def test_different_timezones_same_time():
119121

120122
assert interval.in_words() == "1 day 5 hours"
121123
assert interval.in_hours() == 29
124+
125+
126+
@pytest.mark.parametrize(
127+
"start_hour,start_minute,start_fold,end_hour,end_minute,end_fold,expected",
128+
[
129+
(0, 30, 0, 1, 30, 1, (2, 0)),
130+
(1, 30, 1, 2, 30, 0, (1, 0)),
131+
(1, 15, 0, 1, 45, 1, (1, 30)),
132+
(1, 15, 0, 1, 45, 0, (0, 30)),
133+
(1, 15, 1, 1, 45, 1, (0, 30)),
134+
],
135+
)
136+
@pytest.mark.parametrize("reverse", [False, True])
137+
@pytest.mark.parametrize("absolute", [False, True])
138+
def test_preserves_fold_in_components(
139+
start_hour,
140+
start_minute,
141+
start_fold,
142+
end_hour,
143+
end_minute,
144+
end_fold,
145+
expected,
146+
reverse,
147+
absolute,
148+
):
149+
start = pendulum.datetime(
150+
2024,
151+
11,
152+
3,
153+
start_hour,
154+
start_minute,
155+
tz="America/New_York",
156+
fold=start_fold,
157+
)
158+
end = pendulum.datetime(
159+
2024,
160+
11,
161+
3,
162+
end_hour,
163+
end_minute,
164+
tz="America/New_York",
165+
fold=end_fold,
166+
)
167+
if reverse:
168+
start, end = end, start
169+
interval = pendulum.interval(start, end, absolute=absolute)
170+
sign = -1 if reverse and not absolute else 1
171+
172+
assert (interval.hours, interval.minutes) == tuple(
173+
sign * value for value in expected
174+
)
175+
assert interval.total_seconds() == sign * (expected[0] * 3600 + expected[1] * 60)
176+
assert (
177+
interval.in_words()
178+
== pendulum.duration(
179+
hours=sign * expected[0], minutes=sign * expected[1]
180+
).in_words()
181+
)

0 commit comments

Comments
 (0)