Skip to content

Commit 41753d1

Browse files
authored
Fixes a bug where Closest/Farthest didn't work (#254)
1 parent 8dc244f commit 41753d1

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

pendulum/datetime.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,36 +452,43 @@ def __repr__(self):
452452
)
453453

454454
# Comparisons
455-
def closest(self, dt1, dt2):
455+
def closest(self, dt1, dt2, *dts):
456+
from functools import reduce
456457
"""
457-
Get the closest date from the instance.
458+
Get the farthest date from the instance.
458459
459-
:type dt1: DateTime or datetime
460-
:type dt2: DateTime or datetime
460+
:type dt1: datetime.datetime
461+
:type dt2: datetime.datetime
462+
:type dts: list[datetime.datetime,]
461463
462464
:rtype: DateTime
463465
"""
464-
if dt1 < dt2:
465-
return pendulum.instance(dt1)
466+
dt1 = pendulum.instance(dt1)
467+
dt2 = pendulum.instance(dt2)
468+
dts = [dt1, dt2] + [pendulum.instance(x) for x in dts]
469+
dts = [(abs(self - dt), dt) for dt in dts]
466470

467-
return pendulum.instance(dt2)
471+
return min(dts)[1]
468472

469-
def farthest(self, dt1, dt2):
473+
def farthest(self, dt1, dt2, *dts):
474+
from functools import reduce
470475
"""
471476
Get the farthest date from the instance.
472477
473478
:type dt1: datetime.datetime
474479
:type dt2: datetime.datetime
480+
:type dts: list[datetime.datetime,]
475481
476482
:rtype: DateTime
477483
"""
478484
dt1 = pendulum.instance(dt1)
479485
dt2 = pendulum.instance(dt2)
480486

481-
if dt1 > dt2:
482-
return dt1
487+
dts = [dt1, dt2] + [pendulum.instance(x) for x in dts]
488+
dts = [(abs(self - dt), dt) for dt in dts]
489+
490+
return max(dts)[1]
483491

484-
return dt2
485492

486493
def is_future(self):
487494
"""

tests/datetime/test_comparison.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ def test_closest():
265265
closest = instance.closest(dt2, dt1)
266266
assert closest == dt1
267267

268+
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
269+
hours=x) for x in range(4)]
270+
closest = instance.closest(*dts)
271+
assert closest == dts[0]
272+
273+
closest = instance.closest(*(dts[::-1]))
274+
assert closest == dts[0]
275+
268276

269277
def test_closest_with_datetime():
270278
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
@@ -273,6 +281,12 @@ def test_closest_with_datetime():
273281
closest = instance.closest(dt1, dt2)
274282
assert_datetime(closest, 2015, 5, 28, 11, 0, 0)
275283

284+
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
285+
hours=x) for x in range(4)]
286+
closest = instance.closest(dt1, dt2, *dts)
287+
288+
assert_datetime(closest, 2015, 5, 28, 11, 0, 0)
289+
276290

277291
def test_closest_with_equals():
278292
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
@@ -286,28 +300,48 @@ def test_farthest():
286300
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
287301
dt1 = pendulum.datetime(2015, 5, 28, 11, 0, 0)
288302
dt2 = pendulum.datetime(2015, 5, 28, 14, 0, 0)
289-
closest = instance.farthest(dt1, dt2)
290-
assert closest == dt2
303+
farthest = instance.farthest(dt1, dt2)
304+
assert farthest == dt2
291305

292-
closest = instance.farthest(dt2, dt1)
293-
assert closest == dt2
306+
farthest = instance.farthest(dt2, dt1)
307+
assert farthest == dt2
308+
309+
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
310+
hours=x) for x in range(4)]
311+
farthest = instance.farthest(*dts)
312+
assert farthest == dts[-1]
313+
314+
farthest = instance.farthest(*(dts[::-1]))
315+
assert farthest == dts[-1]
316+
317+
f = pendulum.datetime(2010, 1, 1, 0, 0, 0)
318+
assert f == instance.farthest(f, *(dts))
294319

295320

296321
def test_farthest_with_datetime():
297322
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
298323
dt1 = datetime(2015, 5, 28, 11, 0, 0, tzinfo= pendulum.UTC)
299324
dt2 = datetime(2015, 5, 28, 14, 0, 0, tzinfo= pendulum.UTC)
300-
closest = instance.farthest(dt1, dt2)
325+
farthest = instance.farthest(dt1, dt2)
326+
assert_datetime(farthest, 2015, 5, 28, 14, 0, 0)
301327

302-
assert_datetime(closest, 2015, 5, 28, 14, 0, 0)
328+
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
329+
hours=x) for x in range(4)]
330+
farthest = instance.farthest(dt1, dt2, *dts)
331+
332+
assert_datetime(farthest, 2015, 5, 28, 19, 0, 0)
303333

304334

305335
def test_farthest_with_equals():
306336
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
307337
dt1 = pendulum.datetime(2015, 5, 28, 12, 0, 0)
308338
dt2 = pendulum.datetime(2015, 5, 28, 14, 0, 0)
309-
closest = instance.farthest(dt1, dt2)
310-
assert closest == dt2
339+
farthest = instance.farthest(dt1, dt2)
340+
assert farthest == dt2
341+
342+
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(hours=x) for x in range(4)]
343+
farthest = instance.farthest(dt1, dt2, *dts)
344+
assert farthest == dts[-1]
311345

312346

313347
def test_is_same_day():

0 commit comments

Comments
 (0)