MCPcopy Index your code
hub / github.com/python-pendulum/pendulum / closest

Method closest

src/pendulum/date.py:110–120  ·  view source on GitHub ↗

Get the closest date from the instance.

(self, dt1: date, dt2: date)

Source from the content-addressed store, hash-verified

108 # COMPARISONS
109
110 def closest(self, dt1: date, dt2: date) -> Self:
111 """
112 Get the closest date from the instance.
113 """
114 dt1 = self.__class__(dt1.year, dt1.month, dt1.day)
115 dt2 = self.__class__(dt2.year, dt2.month, dt2.day)
116
117 if self.diff(dt1).in_seconds() < self.diff(dt2).in_seconds():
118 return dt1
119
120 return dt2
121
122 def farthest(self, dt1: date, dt2: date) -> Self:
123 """

Callers 3

test_closestFunction · 0.95
test_closest_with_dateFunction · 0.95
test_closest_with_equalsFunction · 0.95

Calls 2

diffMethod · 0.95
in_secondsMethod · 0.80

Tested by 3

test_closestFunction · 0.76
test_closest_with_dateFunction · 0.76
test_closest_with_equalsFunction · 0.76