Skip to content

Commit

Permalink
While going forward/backward in time, cap dt.day to the last day of t…
Browse files Browse the repository at this point in the history
…he new month.

Fixes #95.
  • Loading branch information
nmlorg committed Jun 19, 2024
1 parent 60aa485 commit 43fce01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
19 changes: 15 additions & 4 deletions metabot/util/humanize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Quick routines to convert machine-friendly data types to human-friendly strings."""

import calendar
import datetime
import time as _time

Expand Down Expand Up @@ -46,18 +47,28 @@ def dayofmonth(dom):


def _nextmonth(dt, months=1):
day = dt.day

while months > 0:
months -= 1
if dt.month == 12:
dt = dt.replace(year=dt.year + 1, month=1)
year = dt.year + 1
month = 1
else:
dt = dt.replace(month=dt.month + 1)
year = dt.year
month = dt.month + 1
_, ndays = calendar.monthrange(year, month)
dt = dt.replace(year=year, month=month, day=min(day, ndays))
while months < 0:
months += 1
if dt.month == 1:
dt = dt.replace(year=dt.year - 1, month=12)
year = dt.year - 1
month = 12
else:
dt = dt.replace(month=dt.month - 1)
year = dt.year
month = dt.month - 1
_, ndays = calendar.monthrange(year, month)
dt = dt.replace(year=year, month=month, day=min(day, ndays))
return dt


Expand Down
20 changes: 20 additions & 0 deletions metabot/util/test_humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ def test_humanize_dayofmonth():
assert humanize.dayofmonth(21) == '21ˢᵗ'


def test_nextmonth():
"""Quick tests for humanize._nextmonth."""

# pylint: disable=protected-access
assert humanize._nextmonth(datetime.date(2024, 11, 1)) == datetime.date(2024, 12, 1)
assert humanize._nextmonth(datetime.date(2024, 12, 1), -1) == datetime.date(2024, 11, 1)
assert humanize._nextmonth(datetime.date(2024, 12, 1)) == datetime.date(2025, 1, 1)
assert humanize._nextmonth(datetime.date(2025, 1, 1), -1) == datetime.date(2024, 12, 1)

assert humanize._nextmonth(datetime.date(2025, 1, 31), 1) == datetime.date(2025, 2, 28)
assert humanize._nextmonth(datetime.date(2025, 1, 31), 2) == datetime.date(2025, 3, 31)
assert humanize._nextmonth(datetime.date(2025, 1, 31), 3) == datetime.date(2025, 4, 30)
assert humanize._nextmonth(datetime.date(2025, 1, 31), 4) == datetime.date(2025, 5, 31)

assert humanize._nextmonth(datetime.date(2025, 5, 31), -1) == datetime.date(2025, 4, 30)
assert humanize._nextmonth(datetime.date(2025, 5, 31), -2) == datetime.date(2025, 3, 31)
assert humanize._nextmonth(datetime.date(2025, 5, 31), -3) == datetime.date(2025, 2, 28)
assert humanize._nextmonth(datetime.date(2025, 5, 31), -4) == datetime.date(2025, 1, 31)


def test_humanize_howrecent():
"""Quick tests for humanize.howrecent."""

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = 'metabot'
version = '0.3.20'
version = '0.3.20.1'
description = 'Modularized, multi-account bot.'
readme = 'README.md'
authors = [
Expand Down

0 comments on commit 43fce01

Please sign in to comment.