Skip to content

Commit aea14b9

Browse files
committed
Adds keep_time keyword argument to next() and previous()
1 parent d389210 commit aea14b9

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Unreleased]
44

5+
### Added
6+
7+
- Added the `keep_time` keyword argument to `next()`/`previous()` methods to keep time information.
8+
59
### Changed
610

711
- Improved `diff_for_humans()` method to display more intuitive strings on edge cases.

pendulum/pendulum.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ def _end_of_week(self):
14211421

14221422
return dt.end_of('day')
14231423

1424-
def next(self, day_of_week=None):
1424+
def next(self, day_of_week=None, keep_time=False):
14251425
"""
14261426
Modify to the next occurrence of a given day of the week.
14271427
If no day_of_week is provided, modify to the next occurrence
@@ -1431,18 +1431,26 @@ def next(self, day_of_week=None):
14311431
:param day_of_week: The next day of week to reset to.
14321432
:type day_of_week: int or None
14331433
1434+
:param keep_time: Whether to keep the time information or not.
1435+
:type keep_time: bool
1436+
14341437
:rtype: Pendulum
14351438
"""
14361439
if day_of_week is None:
14371440
day_of_week = self.day_of_week
14381441

1439-
dt = self.start_of('day').add(days=1)
1442+
if keep_time:
1443+
dt = self
1444+
else:
1445+
dt = self.start_of('day')
1446+
1447+
dt = dt.add(days=1)
14401448
while dt.day_of_week != day_of_week:
14411449
dt = dt.add(days=1)
14421450

14431451
return dt
14441452

1445-
def previous(self, day_of_week=None):
1453+
def previous(self, day_of_week=None, keep_time=False):
14461454
"""
14471455
Modify to the previous occurrence of a given day of the week.
14481456
If no day_of_week is provided, modify to the previous occurrence
@@ -1452,12 +1460,20 @@ def previous(self, day_of_week=None):
14521460
:param day_of_week: The previous day of week to reset to.
14531461
:type day_of_week: int or None
14541462
1463+
:param keep_time: Whether to keep the time information or not.
1464+
:type keep_time: bool
1465+
14551466
:rtype: Pendulum
14561467
"""
14571468
if day_of_week is None:
14581469
day_of_week = self.day_of_week
14591470

1460-
dt = self.start_of('day').subtract(days=1)
1471+
if keep_time:
1472+
dt = self
1473+
else:
1474+
dt = self.start_of('day')
1475+
1476+
dt = dt.subtract(days=1)
14611477
while dt.day_of_week != day_of_week:
14621478
dt = dt.subtract(days=1)
14631479

tests/pendulum_tests/test_day_of_week_modifiers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ def test_next_saturday(self):
7676
d = Pendulum.create(1975, 5, 21).next(6)
7777
self.assertPendulum(d, 1975, 5, 24, 0, 0, 0)
7878

79+
def test_next_keep_time(self):
80+
d = Pendulum.create(1975, 5, 21, 12).next()
81+
self.assertPendulum(d, 1975, 5, 28, 0, 0, 0)
82+
83+
d = Pendulum.create(1975, 5, 21, 12).next(keep_time=True)
84+
self.assertPendulum(d, 1975, 5, 28, 12, 0, 0)
85+
7986
def test_previous(self):
8087
d = Pendulum.create(1975, 5, 21).previous()
8188
self.assertPendulum(d, 1975, 5, 14, 0, 0, 0)
@@ -88,6 +95,13 @@ def test_previous_saturday(self):
8895
d = Pendulum.create(1975, 5, 21).previous(6)
8996
self.assertPendulum(d, 1975, 5, 17, 0, 0, 0)
9097

98+
def test_previous_keep_time(self):
99+
d = Pendulum.create(1975, 5, 21, 12).previous()
100+
self.assertPendulum(d, 1975, 5, 14, 0, 0, 0)
101+
102+
d = Pendulum.create(1975, 5, 21, 12).previous(keep_time=True)
103+
self.assertPendulum(d, 1975, 5, 14, 12, 0, 0)
104+
91105
def test_first_day_of_month(self):
92106
d = Pendulum.create(1975, 11, 21).first_of('month', )
93107
self.assertPendulum(d, 1975, 11, 1, 0, 0, 0)

0 commit comments

Comments
 (0)