Skip to content

Commit d37c667

Browse files
committed
Add next_anniversary functions to Date and DateTime
1 parent c99f242 commit d37c667

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

docs/docs/comparison.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ the `now()` is created in the same timezone as the instance.
6767
>>> birthday = pendulum.datetime(2014, 2, 23)
6868
>>> past_birthday = pendulum.now().subtract(years=50)
6969

70-
>>> born.is_birthday(not_birthday)
70+
>>> born.is_anniversary(not_birthday)
7171
False
72-
>>> born.is_birthday(birthday)
72+
>>> born.is_anniversary(birthday)
7373
True
74-
>>> past_birthday.is_birthday()
74+
>>> past_birthday.is_anniversary()
7575
# Compares to now by default
7676
True
77+
>>> born.next_anniversary()
78+
# Compares to now by default
79+
DateTime(2021, 4, 23, 0, 0, 0, tzinfo=Timezone('UTC'))
80+
>>> born.next_anniversary(not_birthday)
81+
DateTime(2015, 4, 23, 0, 0, 0, tzinfo=Timezone('UTC'))
7782
```

pendulum/date.py

+14
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ def is_anniversary(self, dt=None):
217217
# the old name can be completely replaced with the new in one of the future versions
218218
is_birthday = is_anniversary
219219

220+
def next_anniversary(self, dt=None):
221+
"""
222+
Return the next anniversary date, either from today, or the given date
223+
224+
:rtype: Date
225+
"""
226+
if dt is None:
227+
dt = Date.today()
228+
229+
years = dt.year - self.year
230+
this_year = self.add(years=years)
231+
232+
return this_year if this_year.is_future() else self.add(years=years + 1)
233+
220234
# ADDITIONS AND SUBSTRACTIONS
221235

222236
def add(self, years=0, months=0, weeks=0, days=0):

pendulum/datetime.py

+14
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,20 @@ def is_anniversary(self, dt=None):
592592
# the old name can be completely replaced with the new in one of the future versions
593593
is_birthday = is_anniversary
594594

595+
def next_anniversary(self, dt=None):
596+
"""
597+
Return the next anniversary datetime, either from today, or the given datetime
598+
599+
:rtype: DateTime
600+
"""
601+
if dt is None:
602+
dt = self.now(self.tz)
603+
604+
years = dt.year - self.year
605+
this_year = self.add(years=years)
606+
607+
return this_year if this_year.is_future() else self.add(years=years + 1)
608+
595609
# ADDITIONS AND SUBSTRACTIONS
596610

597611
def add(

tests/date/test_comparison.py

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ def test_is_birthday(): # backward compatibility
169169
assert d3.is_birthday(d1)
170170

171171

172+
def test_next_anniversary():
173+
start = pendulum.Date(1987, 6, 23)
174+
leap_start = pendulum.Date(1988, 2, 29)
175+
176+
assert start.next_anniversary() == pendulum.Date(2020, 6, 23)
177+
assert leap_start.next_anniversary() == pendulum.Date(2021, 2, 28)
178+
next_year = pendulum.Date(2021, 1, 1)
179+
assert start.next_anniversary(dt=next_year) == pendulum.Date(2021, 6, 23)
180+
assert leap_start.next_anniversary(dt=next_year) == pendulum.Date(2021, 2, 28)
181+
182+
172183
def test_closest():
173184
instance = pendulum.Date(2015, 5, 28)
174185
dt1 = pendulum.Date(2015, 5, 27)

tests/datetime/test_comparison.py

+11
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ def test_is_birthday(): # backward compatibility
271271
assert d3.is_birthday(d1)
272272

273273

274+
def test_next_anniversary():
275+
start = pendulum.datetime(1987, 6, 23)
276+
leap_start = pendulum.datetime(1988, 2, 29)
277+
278+
assert start.next_anniversary() == pendulum.datetime(2020, 6, 23)
279+
assert leap_start.next_anniversary() == pendulum.datetime(2021, 2, 28)
280+
next_year = pendulum.datetime(2021, 1, 1)
281+
assert start.next_anniversary(dt=next_year) == pendulum.datetime(2021, 6, 23)
282+
assert leap_start.next_anniversary(dt=next_year) == pendulum.datetime(2021, 2, 28)
283+
284+
274285
def test_closest():
275286
instance = pendulum.datetime(2015, 5, 28, 12, 0, 0)
276287
dt1 = pendulum.datetime(2015, 5, 28, 11, 0, 0)

0 commit comments

Comments
 (0)