Skip to content

Commit 109fcae

Browse files
committed
fix: compare precise distances in Time selection
1 parent 7a2fb2a commit 109fcae

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎src/pendulum/time.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def closest(self, dt1: Time | time, dt2: Time | time) -> Self:
6969
dt1 = self.__class__(dt1.hour, dt1.minute, dt1.second, dt1.microsecond)
7070
dt2 = self.__class__(dt2.hour, dt2.minute, dt2.second, dt2.microsecond)
7171

72-
if self.diff(dt1).in_seconds() < self.diff(dt2).in_seconds():
72+
if self.diff(dt1).total_seconds() < self.diff(dt2).total_seconds():
7373
return dt1
7474

7575
return dt2
@@ -81,7 +81,7 @@ def farthest(self, dt1: Time | time, dt2: Time | time) -> Self:
8181
dt1 = self.__class__(dt1.hour, dt1.minute, dt1.second, dt1.microsecond)
8282
dt2 = self.__class__(dt2.hour, dt2.minute, dt2.second, dt2.microsecond)
8383

84-
if self.diff(dt1).in_seconds() > self.diff(dt2).in_seconds():
84+
if self.diff(dt1).total_seconds() > self.diff(dt2).total_seconds():
8585
return dt1
8686

8787
return dt2

‎tests/time/test_comparison.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,56 @@ def test_farthest_with_equals():
178178
assert t2 == farthest
179179

180180

181+
def test_closest_with_microseconds():
182+
instance = pendulum.time(12, 0, 0, 900000)
183+
t1 = pendulum.time(12, 0, 0, 900000)
184+
t2 = pendulum.time(12, 0, 1, 100000)
185+
186+
assert instance.closest(t1, t2) == t1
187+
assert instance.closest(t2, t1) == t1
188+
189+
190+
def test_closest_with_microseconds_across_a_second():
191+
instance = pendulum.time(12, 0, 0, 900000)
192+
t1 = pendulum.time(12, 0, 1)
193+
t2 = pendulum.time(12, 0, 0)
194+
195+
assert instance.closest(t1, t2) == t1
196+
assert instance.closest(t2, t1) == t1
197+
198+
199+
def test_closest_with_microseconds_of_native_times():
200+
instance = pendulum.time(12, 0, 0, 900000)
201+
closest = instance.closest(time(12, 0, 0, 900000), time(12, 0, 1, 100000))
202+
203+
assert_time(closest, 12, 0, 0, 900000)
204+
205+
206+
def test_farthest_with_microseconds():
207+
instance = pendulum.time(12, 0, 0, 900000)
208+
t1 = pendulum.time(12, 0, 0, 900000)
209+
t2 = pendulum.time(12, 0, 1, 100000)
210+
211+
assert instance.farthest(t1, t2) == t2
212+
assert instance.farthest(t2, t1) == t2
213+
214+
215+
def test_farthest_with_microseconds_across_a_second():
216+
instance = pendulum.time(12, 0, 0, 900000)
217+
t1 = pendulum.time(12, 0, 1)
218+
t2 = pendulum.time(12, 0, 0)
219+
220+
assert instance.farthest(t1, t2) == t2
221+
assert instance.farthest(t2, t1) == t2
222+
223+
224+
def test_farthest_with_microseconds_of_native_times():
225+
instance = pendulum.time(12, 0, 0, 900000)
226+
farthest = instance.farthest(time(12, 0, 1, 100000), time(12, 0, 0, 900000))
227+
228+
assert_time(farthest, 12, 0, 1, 100000)
229+
230+
181231
def test_comparison_to_unsupported():
182232
t1 = pendulum.now().time()
183233

0 commit comments

Comments
 (0)