Add get_previous_draw_date function
This commit is contained in:
1 parent
96974cae2c
commit
3b9dee225d
1 file changed
+42
@@ -47,6 +47,48 @@ def get_next_draw_date(game: str | None, from_date: str | None = None) -> str |
|
|||||||
return return_date.strftime(return_date_format)
|
return return_date.strftime(return_date_format)
|
||||||
|
|
||||||
|
|
||||||
|
def get_previous_draw_date(game: str | None, from_date: str | None = None) -> str | None:
|
||||||
|
"""
|
||||||
|
Gets the previous drawing date from the current or specified date for the given game
|
||||||
|
If the current date is a drawing date, the next previous date will be returned
|
||||||
|
|
||||||
|
:param game: megamillions or powerball
|
||||||
|
:param from_date: date to get previous drawing date from, if omitted the current date is used
|
||||||
|
:return: previous drawing date
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> get_previous_draw_date(game='mega-millions', from_date='02/15/2026')
|
||||||
|
'02/13/2026'
|
||||||
|
>>> get_previous_draw_date(game='powerball', from_date='02/15/2026')
|
||||||
|
'02/14/2026'
|
||||||
|
"""
|
||||||
|
target_days = {
|
||||||
|
"powerball": [0, 2, 5],
|
||||||
|
"mega-millions": [1, 4]
|
||||||
|
}
|
||||||
|
|
||||||
|
return_date = None
|
||||||
|
return_date_format = '%m/%d/%Y'
|
||||||
|
|
||||||
|
# if a valid game is not specified, return None
|
||||||
|
if game is None or game not in target_days.keys():
|
||||||
|
return return_date
|
||||||
|
|
||||||
|
# if no date was specified, use today's date
|
||||||
|
if from_date is None:
|
||||||
|
return_date = datetime.today()
|
||||||
|
else:
|
||||||
|
return_date = datetime.strptime(from_date, return_date_format)
|
||||||
|
|
||||||
|
# to ensure the current date is not returned if it is a draw date, subtract a day first
|
||||||
|
return_date -= timedelta(days=1)
|
||||||
|
|
||||||
|
while return_date.weekday() not in target_days[game]:
|
||||||
|
return_date -= timedelta(days=1)
|
||||||
|
|
||||||
|
return return_date.strftime(return_date_format)
|
||||||
|
|
||||||
|
|
||||||
def get_powerball_object(draw_date: str, draw_result: dict) -> PowerballDraw:
|
def get_powerball_object(draw_date: str, draw_result: dict) -> PowerballDraw:
|
||||||
"""
|
"""
|
||||||
:param draw_date: date for the drawing data
|
:param draw_date: date for the drawing data
|
||||||
|
|||||||
Reference in new issue
Block a user