fixed definition of year day to match official

This commit is contained in:
PyryL 2022-12-11 18:54:02 +02:00
parent b7404f7741
commit cdff0fd7bb
6 changed files with 31 additions and 28 deletions

View File

@ -72,12 +72,12 @@ jan_first < fixed_date # False
Year day is the day after the last of December and before the first of January.
For that date, `FixedDate` gives the following property values.
* `day_of_year` = 365
* `day_of_month` = 1
* `month` = 14
* `day_of_year` = 365 (366 on leap years)
* `day_of_month` = 29
* `month` = 13
* `year` is obviously the ending year
* `is_year_day` = True
* `week_of_month` = 1
* `weekday` = 1
* `week_of_year` = 53
* `week_of_month` = 4
* `weekday` = None
* `week_of_year` = 52
* `year_quarter` = 4

View File

@ -77,7 +77,7 @@ class FixedDate:
@property
def day_of_month(self):
"""In range 1...29"""
if self.is_leap_day:
if self.is_leap_day or self.is_year_day:
return 29
if self.is_leap_year and self.day_of_year > 169: # leap day past this year
return ((self._day_of_year-2) % 28) + 1
@ -88,6 +88,8 @@ class FixedDate:
"""In range 1...13"""
if self.is_leap_day:
return 6
if self.is_year_day:
return 13
return ((self._day_of_year-1) // 28) + 1
@property
@ -98,19 +100,17 @@ class FixedDate:
@property
def is_year_day(self) -> bool:
if self.is_leap_year:
return self.day_of_year == 366
return self.day_of_year == 365
return self._day_of_year == 366
return self._day_of_year == 365
@property
def week_of_month(self) -> int:
"""The ordinal of the week in month. Value 1 for year day.
"""The ordinal of the week in month.
Returns:
int: In range 1...4
"""
if self.is_year_day:
return 1
if self.is_leap_day:
if self.is_leap_day or self.is_year_day:
return 4
return ((self.day_of_month-1) // 7) + 1
@ -119,26 +119,29 @@ class FixedDate:
"""Ordinal of the day in week. Value 1 for year day.
Returns:
int: 1 for Sunday, 2 for Monday, 7 for Saturday
Optional[int]: 1 for Sunday, 2 for Monday, 7 for Saturday
None for leap day and year day.
"""
if self.is_leap_day:
if self.is_leap_day or self.is_year_day:
return None
return ((self.day_of_month-1) % 7) + 1
@property
def week_of_year(self) -> int:
"""The ordinal of the week in year. Value 53 for year day.
"""The ordinal of the week in year.
Returns:
int: In range 1...53
int: In range 1...52
"""
if self.is_leap_day:
return 24
if self.is_year_day:
return 52
return ((self._day_of_year-1) // 7) + 1
@property
def year_quarter(self) -> int:
"""Quarter of the year. Value 4 for year day.
"""Quarter of the year.
Returns:
int: In range 1...4

View File

@ -68,13 +68,13 @@ class TestBasicDatetimeInit(unittest.TestCase):
fixed_date = FixedDate(date=datetime(2022, 12, 31))
self.assertEqual(fixed_date.datetime, datetime(2022, 12, 31))
self.assertEqual(fixed_date.year, 2022)
self.assertEqual(fixed_date.month, 14)
self.assertEqual(fixed_date.day_of_month, 1)
self.assertEqual(fixed_date.month, 13)
self.assertEqual(fixed_date.day_of_month, 29)
self.assertTrue(fixed_date.is_year_day)
self.assertEqual(fixed_date.day_of_year, 365)
self.assertEqual(fixed_date.week_of_month, 1)
self.assertEqual(fixed_date.weekday, 1)
self.assertEqual(fixed_date.week_of_year, 53)
self.assertEqual(fixed_date.week_of_month, 4)
self.assertIsNone(fixed_date.weekday)
self.assertEqual(fixed_date.week_of_year, 52)
self.assertEqual(fixed_date.year_quarter, 4)
def test_today(self):

View File

@ -31,6 +31,6 @@ class TestBasicDayOfYearInit(unittest.TestCase):
def test_day_of_year_init_year_day(self):
fixed_date = FixedDate(day_of_year=365, year=2022) # 2022-12-31 Gregorian
self.assertEqual(fixed_date.year, 2022)
self.assertEqual(fixed_date.month, 14)
self.assertEqual(fixed_date.day_of_month, 1)
self.assertEqual(fixed_date.month, 13)
self.assertEqual(fixed_date.day_of_month, 29)
self.assertTrue(fixed_date.is_year_day)

View File

@ -38,8 +38,8 @@ class TestLeapYear(unittest.TestCase):
fixed_date = FixedDate(day_of_year=366, year=2024)
self.assertTrue(fixed_date.is_year_day)
self.assertEqual(fixed_date.year, 2024)
self.assertEqual(fixed_date.month, 14)
self.assertEqual(fixed_date.day_of_month, 1)
self.assertEqual(fixed_date.month, 13)
self.assertEqual(fixed_date.day_of_month, 29)
def test_ordinary_date_after_leap_day(self):
fixed_date = FixedDate(datetime(2024, 10, 13))

View File

@ -13,4 +13,4 @@ class TestStringRepresentation(unittest.TestCase):
def test_string_of_year_day(self):
fixed_date = FixedDate(day_of_year=365, year=2022)
self.assertEqual(str(fixed_date), "2022-14-01")
self.assertEqual(str(fixed_date), "2022-13-29")