Skip to content

Commit

Permalink
fix: fix leap year bug in static view. Fixes #3910
Browse files Browse the repository at this point in the history
  • Loading branch information
rpcross committed Jan 10, 2025
1 parent 1338290 commit 52f654c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions backend/mlarchive/archive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def get_this_next_periods(time_period):
"""
if time_period.month:
this_period = datetime.datetime(time_period.year, time_period.month, 1, tzinfo=timezone.utc)
next_period = add_one_month(this_period)
next_period = this_period + relativedelta(months=1)
else:
this_period = datetime.datetime(time_period.year, 1, 1, tzinfo=timezone.utc)
next_period = this_period + datetime.timedelta(days=365)
next_period = this_period + relativedelta(years=1)
return (this_period, next_period)


Expand Down
16 changes: 6 additions & 10 deletions backend/mlarchive/tests/archive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from factories import EmailListFactory, MessageFactory, UserFactory, SubscriberFactory
from mlarchive.archive.models import Message, Attachment, Redirect
from mlarchive.archive.views import (TimePeriod, add_nav_urls, is_small_year,
add_one_month, get_this_next_periods, get_date_endpoints, get_thread_endpoints,
DateStaticIndexView)
get_this_next_periods, get_date_endpoints, get_thread_endpoints, DateStaticIndexView)
from mlarchive.utils.test_utils import login_testing_unauthorized
from mlarchive.utils.test_utils import load_message

Expand Down Expand Up @@ -82,14 +81,11 @@ def test_get_this_next_periods(static_list):
assert get_this_next_periods(time_period) == (
datetime.datetime(2017, 1, 1, tzinfo=timezone.utc),
datetime.datetime(2018, 1, 1, tzinfo=timezone.utc))


@pytest.mark.django_db(transaction=True)
def test_add_one_month():
date = datetime.datetime(2018, 1, 1, tzinfo=timezone.utc)
assert add_one_month(date) == datetime.datetime(2018, 2, 1, tzinfo=timezone.utc)
date = datetime.datetime(2018, 12, 1, tzinfo=timezone.utc)
assert add_one_month(date) == datetime.datetime(2019, 1, 1, tzinfo=timezone.utc)
# test leap year
time_period = TimePeriod(year=2024, month=None)
assert get_this_next_periods(time_period) == (
datetime.datetime(2024, 1, 1, tzinfo=timezone.utc),
datetime.datetime(2025, 1, 1, tzinfo=timezone.utc))


# --------------------------------------------------
Expand Down

0 comments on commit 52f654c

Please sign in to comment.