Skip to content
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

How to check for holidays within an interval of time #1825

Open
dsoprea opened this issue Jun 10, 2024 · 6 comments
Open

How to check for holidays within an interval of time #1825

dsoprea opened this issue Jun 10, 2024 · 6 comments
Assignees

Comments

@dsoprea
Copy link

dsoprea commented Jun 10, 2024

I'd like to be able to determine if there are any holidays in the near future, expressed as a timedelta. More generally, maybe just between two dates.

Pseudocode:

NOW_DATETIME = datetime.datetime.utcnow()
SIXTY_DAYS_TIMEDELTA = datetime.timedelta(days=60)

holidays_us = holidays.US()
upcoming_holiday_dates = holidays_us.holidays_before(SIXTY_DAYS_TIMEDELTA)
holiday_dates = holidays_us.holidays_between(NOW_DATETIME, NOW_DATETIME + SIXTY_DAYS_TIMEDELTA)

It's not clear if holidays supports just checking certain dates in order for developers to be able to implement this ourselves. It seems sort of there, except that I wonder what happens due to the year handling if the next sixty days includes both December and January. Based on the design of _add_holiday(), they're seemingly stored in a year-specific fashion:
https://github.com/vacanza/python-holidays/blob/fde28db1d17e05e0853585936b8b8f85e9e38d34/holidays/holiday_base.py#L737

Suggestions?

@arkid15r
Copy link
Collaborator

Hi @dsoprea!

I'd like to be able to determine if there are any holidays in the near future, expressed as a timedelta. More generally, maybe just between two dates.

I don't recall holidays having built-in functionality for that. However, it sounds to me like something that could be useful for the end-users.

PRs welcome. Or you may get lucky if @KJhellico or me will be able to find some time this month. Perhaps, by extending holidays.utils.country_holidays() (and similar) with before_date and after_date args.

Thanks for the idea!

@dsoprea
Copy link
Author

dsoprea commented Jun 10, 2024

If you could suggest a straightforward approach then I could do it. If it requires research or rewiring then that'd be best left to the maintainers.

@arkid15r
Copy link
Collaborator

Yeah, it's up to you. I believe any PR would require some research of the existing code base, project architecture and generally used approaches. I suggest looking at some of the most recent PRs (perhaps, changes related to HolidayBase would be the most useful).

Just make sure to let us know if you decide to work on this so no work would be done twice.

Thank you @dsoprea!

@arkid15r arkid15r self-assigned this Jun 15, 2024
@Rosi2143
Copy link
Contributor

Rosi2143 commented Jan 2, 2025

Hi @arkid15r / @dsoprea ,

I'm looking for something similar - but would like to revert the logic.

How about adding a function to get the "next" holiday. Getting the amount of days is easy with existing datetime functions.

e.g. a function returning the date.

`

us_holidays = holidays.US()
next_holiday = us_holidays.getNextHoliday()
print(next_holiday)
2025-01-20
`

I would be willing to create a MR if you agree with the approach,

Thanks

P.S. Another idea for an extention would be to get a function to get the "number of working days" (similar to get_nth_working_day) from now to some other date - e.g. the next holiday returned via getNextHoliday().

@arkid15r
Copy link
Collaborator

arkid15r commented Jan 2, 2025

Hi @arkid15r / @dsoprea ,

I'm looking for something similar - but would like to revert the logic.

How about adding a function to get the "next" holiday. Getting the amount of days is easy with existing datetime functions.

e.g. a function returning the date.

`

us_holidays = holidays.US()
next_holiday = us_holidays.getNextHoliday()
print(next_holiday)
2025-01-20
`

I would be willing to create a MR if you agree with the approach,

Thanks

P.S. Another idea for an extention would be to get a function to get the "number of working days" (similar to get_nth_working_day) from now to some other date - e.g. the next holiday returned via getNextHoliday().

Sounds good, I'd only recommend it to return a tuple

print(next_holiday)
(2025-01-20, "Martin Luther King Jr. Day")

Maybe also add a dt: date as an optional parameter for the next holiday calculation.

@KJhellico @PPsyrius any suggestions?

@Rosi2143
Copy link
Contributor

Rosi2143 commented Jan 4, 2025

OK guys,

I will then give it a try.

And yes - returning a tuple is a good idea. Thanks.

Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 4, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 4, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 4, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 9, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 10, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Rosi2143 added a commit to Rosi2143/python-holidays that referenced this issue Jan 10, 2025
a new function get_next_holiday is added to retrieve the date
of the next known holiday.
Also the name of the holiday is returned.

It is possible to search forward and backward in time.

This should solve vacanza#1825

Signed-off-by: Schrotti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants