Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,36 +452,43 @@ def __repr__(self):
)

# Comparisons
def closest(self, dt1, dt2):
def closest(self, dt1, dt2, *dts):
from functools import reduce
"""
Get the closest date from the instance.
Get the farthest date from the instance.

:type dt1: DateTime or datetime
:type dt2: DateTime or datetime
:type dt1: datetime.datetime
:type dt2: datetime.datetime
:type dts: list[datetime.datetime,]

:rtype: DateTime
"""
if dt1 < dt2:
return pendulum.instance(dt1)
dt1 = pendulum.instance(dt1)
dt2 = pendulum.instance(dt2)
dts = [dt1, dt2] + [pendulum.instance(x) for x in dts]
dts = [(abs(self - dt), dt) for dt in dts]

return pendulum.instance(dt2)
return min(dts)[1]

def farthest(self, dt1, dt2):
def farthest(self, dt1, dt2, *dts):
from functools import reduce
"""
Get the farthest date from the instance.

:type dt1: datetime.datetime
:type dt2: datetime.datetime
:type dts: list[datetime.datetime,]

:rtype: DateTime
"""
dt1 = pendulum.instance(dt1)
dt2 = pendulum.instance(dt2)

if dt1 > dt2:
return dt1
dts = [dt1, dt2] + [pendulum.instance(x) for x in dts]
dts = [(abs(self - dt), dt) for dt in dts]

return max(dts)[1]

return dt2

def is_future(self):
"""
Expand Down
50 changes: 42 additions & 8 deletions tests/datetime/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ def test_closest():
closest = instance.closest(dt2, dt1)
assert closest == dt1

dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
hours=x) for x in range(4)]
closest = instance.closest(*dts)
assert closest == dts[0]

closest = instance.closest(*(dts[::-1]))
assert closest == dts[0]


def test_closest_with_datetime():
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
Expand All @@ -273,6 +281,12 @@ def test_closest_with_datetime():
closest = instance.closest(dt1, dt2)
assert_datetime(closest, 2015, 5, 28, 11, 0, 0)

dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
hours=x) for x in range(4)]
closest = instance.closest(dt1, dt2, *dts)

assert_datetime(closest, 2015, 5, 28, 11, 0, 0)


def test_closest_with_equals():
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
Expand All @@ -286,28 +300,48 @@ def test_farthest():
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
dt1 = pendulum.datetime(2015, 5, 28, 11, 0, 0)
dt2 = pendulum.datetime(2015, 5, 28, 14, 0, 0)
closest = instance.farthest(dt1, dt2)
assert closest == dt2
farthest = instance.farthest(dt1, dt2)
assert farthest == dt2

closest = instance.farthest(dt2, dt1)
assert closest == dt2
farthest = instance.farthest(dt2, dt1)
assert farthest == dt2

dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
hours=x) for x in range(4)]
farthest = instance.farthest(*dts)
assert farthest == dts[-1]

farthest = instance.farthest(*(dts[::-1]))
assert farthest == dts[-1]

f = pendulum.datetime(2010, 1, 1, 0, 0, 0)
assert f == instance.farthest(f, *(dts))


def test_farthest_with_datetime():
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
dt1 = datetime(2015, 5, 28, 11, 0, 0, tzinfo= pendulum.UTC)
dt2 = datetime(2015, 5, 28, 14, 0, 0, tzinfo= pendulum.UTC)
closest = instance.farthest(dt1, dt2)
farthest = instance.farthest(dt1, dt2)
assert_datetime(farthest, 2015, 5, 28, 14, 0, 0)

assert_datetime(closest, 2015, 5, 28, 14, 0, 0)
dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(
hours=x) for x in range(4)]
farthest = instance.farthest(dt1, dt2, *dts)

assert_datetime(farthest, 2015, 5, 28, 19, 0, 0)


def test_farthest_with_equals():
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
dt1 = pendulum.datetime(2015, 5, 28, 12, 0, 0)
dt2 = pendulum.datetime(2015, 5, 28, 14, 0, 0)
closest = instance.farthest(dt1, dt2)
assert closest == dt2
farthest = instance.farthest(dt1, dt2)
assert farthest == dt2

dts = [pendulum.datetime(2015, 5, 28, 16, 0, 0) + pendulum.duration(hours=x) for x in range(4)]
farthest = instance.farthest(dt1, dt2, *dts)
assert farthest == dts[-1]


def test_is_same_day():
Expand Down