python-holidays icon indicating copy to clipboard operation
python-holidays copied to clipboard

get_closest_holiday() incorrectly handles Unix timestamp 0

Open nayansharma8123-gif opened this issue 2 weeks ago • 2 comments

Describe the bug

HolidayBase.get_closest_holiday() does not correctly handle Unix timestamp 0 as its target_date.

The method currently uses:

dt = self.__keytransform__(target_date or datetime.now().date())

Since 0 and 0.0 are falsy in Python, passing either value causes the method to use the current date instead of converting the supplied Unix timestamp.

To reproduce

from datetime import date

import holidays

by_timestamp = holidays.US(years=1970).get_closest_holiday(0)
by_date = holidays.US(years=1970).get_closest_holiday(date(1970, 1, 1))

print(by_timestamp)
print(by_date)

Expected behavior

Passing 0 should be handled as a valid Unix timestamp and produce the same result as passing its corresponding date.

Actual behavior

The method treats 0 as if no target_date was provided and searches relative to the current date instead.

Suggested fix

Check explicitly whether target_date is None instead of relying on truthiness:

dt = self.__keytransform__(
    target_date if target_date is not None else datetime.now().date()
)

Add regression tests covering both 0 and 0.0.

Scope

This should be a small bug fix limited to get_closest_holiday() and its unit tests.

nayansharma8123-gif avatar Nov 16 '26 22:11 nayansharma8123-gif

@KJhellico @PPsyrius !! If this is considered a valid bug worth addressing. Could you please assign this issue to me?

nayansharma8123-gif avatar Nov 17 '26 18:11 nayansharma8123-gif

Hi, I am Aarambh, a 3rd year CSE student getting ready for GSoC 2027. I reproduced this, passing 0 gives the closest holiday to today instead of 1970-01-01. The fix looks like checking for None instead of truthiness in get_closest_holiday. Is it still free to work on?

Duke2nuke avatar Nov 22 '26 06:11 Duke2nuke