Fixing bug in gametime module

Previously, gametime used the length of the current year and
current month to calculate the future date in the real_seconds_until
function. This caused a small bug when passing over a leap day from
a year without a leap day.

This change uses the datetime module to just increment the year
or month, sidestepping the issue.
This commit is contained in:
Chason Chaffin 2019-04-19 16:19:15 +09:00
parent 7ebd6a9f0e
commit 8fc7815189
2 changed files with 5 additions and 5 deletions

View file

@ -189,12 +189,13 @@ def real_seconds_until(sec=None, min=None, hour=None,
if projected <= current:
# We increase one unit of time depending on parameters
days_in_month = monthrange(s_year, s_month)[1]
days_in_year = sum(monthrange(s_year, m + 1)[1] for m in range(12))
if month is not None:
projected += timedelta(days=days_in_year)
projected = projected.replace(year=s_year+1)
elif day is not None:
projected += timedelta(days=days_in_month)
try:
projected = projected.replace(month=s_month+1)
except ValueError:
projected = projected.replace(month=1)
elif hour is not None:
projected += timedelta(days=1)
elif min is not None:

View file

@ -78,7 +78,6 @@ class TestGametime(TestCase):
self.assertAlmostEqual(gametime.real_seconds_until(day=17), 501120)
self.assertAlmostEqual(gametime.real_seconds_until(month=1), 4752000)
@unittest.expectedFailure
def test_real_seconds_until_leap_year(self):
self.assertAlmostEqual(gametime.real_seconds_until(month=3), 5788800)