arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Holiday support for Arrow

Open b10011 opened this issue 5 years ago • 2 comments

Feature Request

It would be nice to be able to call the following methods on an arrow object:

  • .is_holiday(locale="FI") to get boolean telling whether the date is a holiday or not
  • .holiday(locale="FI") to get the name of the holiday (or None if it's not a holiday)

This would be easy to implement using python-holidays. The library includes lots of countries. I would suggest making the dependency optional so that the installation size doesn't increase a lot.

The default value for locale could be read using the current locale of the system. Below is a rough idea translated into code.

try:
    import holidays
except ModuleNotFoundError:
    holidays = None
import locale

default_locale = locale.getdefaultlocale()[0].split("_")[1]

def is_holiday(arrowobj, locale=None, **kwargs):
    if holidays is None:
        raise ModuleNotFoundError('Arrow .is_holiday() requires installation of "holidays" package (PyPI holidays)')
    holidays_country = getattr(holidays, locale or default_locale)(**kwargs)
    return arrowobj.date() in holidays_country

def holiday(arrowobj, locale=None, **kwargs):
    if holidays is None:
        raise ModuleNotFoundError('Arrow .holiday() requires installation of "holidays" package (PyPI holidays)')
    holidays_country = getattr(holidays, locale or default_locale)(**kwargs)
    date_ = arrowobj.date()
    if date_ in holidays_country:
        return holidays_country[date_]
    else:
        return None

In case this feature request gets support, I am willing to code the implementation, so this wouldn't be a burden on the active dev team.

b10011 avatar Jan 11 '21 10:01 b10011

Hi @b10011 apologies for never getting back to you on this! @systemcatch what are your thoughts on this? Do you think it is worth introducing the hard holiday dependency to Arrow? It seems actively maintained, so it might be a cool feature addition.

jadchaar avatar Feb 22 '21 04:02 jadchaar

@jadchaar reading through the holidays module (which is a very neat idea) it seems easy to feed in dates/strings from arrow already, I'm not sure adding a dependency gains much here.

systemcatch avatar Feb 23 '21 21:02 systemcatch