Skip to content

Delete other/date_to_weekday.py as a how-to-use, not an algorithm #5591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 29, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion other/date_to_weekday.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from calendar import day_name
from datetime import datetime
from typing import Union


def date_to_weekday(inp_date: str) -> str:
Expand All @@ -14,11 +15,12 @@ def date_to_weekday(inp_date: str) -> str:
>>> date_to_weekday("1/1/2021")
'Friday'
"""
year: Union[int, str]
Copy link
Member

@cclauss cclauss Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of:

from typing import Union
year: Union[int, str]
# this repo has been moving to the new syntax by doing
from __future__ import annotations
year: int | str  # or year: [int | str]

However, I question the base logic of the year % 100 thing because not all years that are evenly divisible by 100 are the same year.

>>> [(i, datetime(i, 1, 1).weekday()) for i in range(200, 2200, 100)]
[ (200, 2),  (300, 0),  (400, 5),  (500, 4),
  (600, 2),  (700, 0),  (800, 5),  (900, 4),
 (1000, 2), (1100, 0), (1200, 5), (1300, 4),
 (1400, 2), (1500, 0), (1600, 5), (1700, 4),
 (1800, 2), (1900, 0), (2000, 5), (2100, 4)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The algorithm was definitely flawed. I just wanted to add types :)

I've rewritten the algo, which boils it down to two calls to the standard lib. If we want to manually parse a string by splitting on /, that can be added back in.

day, month, year = (int(x) for x in inp_date.split("/"))
if year % 100 == 0:
year = "00"
new_base_date: str = f"{day}/{month}/{year%100} 0:0:0"
date_time_obj: datetime.date = datetime.strptime(new_base_date, "%d/%m/%y %H:%M:%S")
date_time_obj: datetime = datetime.strptime(new_base_date, "%d/%m/%y %H:%M:%S")
out_put_day: int = date_time_obj.weekday()
return day_name[out_put_day]

Expand Down