|
| 1 | +=========== |
| 2 | +holidays.py |
| 3 | +=========== |
| 4 | + |
| 5 | +Holidays is a fast, efficient Python library for generating country-specific |
| 6 | +sets of holidays on the fly. It aims to make determining whether a specific |
| 7 | +date is a holiday as fast and flexible as possible. |
| 8 | + |
| 9 | + |
| 10 | +Example Usage |
| 11 | +------------- |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + >>> from holidays import Holidays |
| 16 | + >>> us_holidays = Holidays(country='US') |
| 17 | + >>> date(2014,1,1) in us_holidays |
| 18 | + True |
| 19 | + >>> date(2014,1,2) in us_holidays |
| 20 | + False |
| 21 | + >>> us_holidays[date(2014,1,1)] |
| 22 | + "New Year's Day" |
| 23 | + >>> '2014-01-01' in us_holidays |
| 24 | + True |
| 25 | + >>> '1/1/2014' in us_holidays |
| 26 | + True |
| 27 | + >>>1388597445 in us_holidays # Unix timestamp |
| 28 | + True |
| 29 | +
|
| 30 | +
|
| 31 | +Install |
| 32 | +------- |
| 33 | + |
| 34 | +The latest stable version can always be installed or updated via pip: |
| 35 | + |
| 36 | +.. code-block:: bash |
| 37 | +
|
| 38 | + $ pip install holidays |
| 39 | +
|
| 40 | +If the above fails, please use easy_install instead: |
| 41 | + |
| 42 | +.. code-block:: bash |
| 43 | +
|
| 44 | + $ easy_install holidays |
| 45 | +
|
| 46 | +Available Countries |
| 47 | +------------------- |
| 48 | + |
| 49 | +============= ========== ====================================================== |
| 50 | +Country Param Abbr Prov/State Options |
| 51 | +============= ========== ====================================================== |
| 52 | +Canada "CA" AB, BC, MB, NB, NL, NS, NT, NU, **ON**, PE, QC, SK, YU |
| 53 | +United States "US" None |
| 54 | +============= ========== ====================================================== |
| 55 | + |
| 56 | + |
| 57 | +API |
| 58 | +--- |
| 59 | + |
| 60 | +class holidays.Holiday(country="US", prov=None, years=[], expand=True, observed=True) |
| 61 | + The main Holiday class used to create holiday list objects. |
| 62 | + |
| 63 | +Parameters: |
| 64 | + |
| 65 | +country |
| 66 | + A string representing the country to generate the holidays for. (Default: "US") |
| 67 | + |
| 68 | +prov |
| 69 | + A string specifying a prov/state within *country* that has unique statutory |
| 70 | + holidays. (Default: CA->ON, US->None) |
| 71 | + |
| 72 | +years |
| 73 | + An iterable list of integers specifying the years that the Holiday object |
| 74 | + should pre-generate. This would generally only be used if setting *expand* |
| 75 | + to False. (Default: []) |
| 76 | + |
| 77 | +expand |
| 78 | + A boolean value which specifies whether or not to append holidays in new |
| 79 | + years to the holidays object. (Default: True) |
| 80 | + |
| 81 | +observed |
| 82 | + A boolean value which when set to True will include the observed day of a |
| 83 | + holiday that falls on a weekend, when appropriate. (Default: True) |
| 84 | + |
| 85 | + |
| 86 | +More Examples |
| 87 | +------------- |
| 88 | + |
| 89 | +.. code-block:: python |
| 90 | +
|
| 91 | + # Simplest example possible |
| 92 | +
|
| 93 | + >>> from holidays import Holidays |
| 94 | + >>> date(2014,1,1) in Holidays(country='US') |
| 95 | + True |
| 96 | + >> date(2014,1,2) in Holidays(country='US') |
| 97 | + False |
| 98 | +
|
| 99 | + # But this is not efficient because it is initializing a new Holiday object |
| 100 | + # and generating a list of all the holidays in 2014 during each comparison |
| 101 | +
|
| 102 | + # It is more efficient to create the object only once |
| 103 | +
|
| 104 | + >>> us_holidays = Holidays(country='US') |
| 105 | + >>> date(2014,1,1) in us_holidays |
| 106 | + True |
| 107 | + >> date(2014,1,2) in us_holidays |
| 108 | + False |
| 109 | +
|
| 110 | +
|
| 111 | + # So far we've only checked holidays in 2014 so that's the only year the |
| 112 | + # Holidays object has generated |
| 113 | +
|
| 114 | + >>> us_holidays.years |
| 115 | + set([2014]) |
| 116 | + >>> len(us_holidays) |
| 117 | + 10 |
| 118 | +
|
| 119 | + # Because by default the `expand` param is True the Holiday object will add |
| 120 | + # holidays from other years as they are required. |
| 121 | +
|
| 122 | + >>> date(2013,1,1) in us_holidays |
| 123 | + True |
| 124 | + >>> us_holidays.years |
| 125 | + set([2013,2014]) |
| 126 | + >>> len(us_holidays) |
| 127 | + 20 |
| 128 | +
|
| 129 | + # If we change the `expand` param to False the Holiday object will no longer |
| 130 | + # add holidays from new years |
| 131 | +
|
| 132 | + >>> us_holidays.expand = False |
| 133 | + >>> date(2013,1,1) in us_holidays |
| 134 | + False |
| 135 | + >>> us.holidays.expand = True |
| 136 | + >>> date(2013,1,1) in us_holidays |
| 137 | + True |
| 138 | +
|
| 139 | + # January 1st, 2012 fell on a Sunday so the statutory holiday was observed on |
| 140 | + # the 2nd. By default the `observed` param is True so the holiday list will |
| 141 | + # include January 2nd, 2012 as a holiday. |
| 142 | +
|
| 143 | + >>> date(2012,1,1) in us_holidays |
| 144 | + True |
| 145 | + >>> us_holidays[date(2012,1,1)] |
| 146 | + "New Year's Eve" |
| 147 | + >>> date(2012,1,2) in us_holidays |
| 148 | + True |
| 149 | + >>> us_holidays.get(date(2012,1,2)) |
| 150 | + "New Year's Eve (Observed)" |
| 151 | +
|
| 152 | + # The `observed` and `expand` values can both be changed on the fly and the |
| 153 | + # holiday list will be adjusted accordingly |
| 154 | +
|
| 155 | + >>> us_holidays.observed = False |
| 156 | + >>> date(2012,1,2) in us_holidays |
| 157 | + False |
| 158 | + us_holidays.observed = True |
| 159 | + >> date(2012,1,2) in us_holidays |
| 160 | + True |
| 161 | +
|
| 162 | + # Sometimes you may not be able to use the official federal statutory |
| 163 | + # holiday list in your code. Let's pretend you work for a company that |
| 164 | + # does not include Columbus Day as a statutory holiday but does include |
| 165 | + # "Ninja Turtle Day" on July 13th. We can create a new class that inherits |
| 166 | + # the Holidays class and the only method we need to override is _populate() |
| 167 | +
|
| 168 | + >>> from dateutil.relativedelta import relativedelta |
| 169 | + >>> class CorporateHolidays(Holidays): |
| 170 | + >>> def _populate(self, year): |
| 171 | + >>> # Populate the holiday list with the default US holidays |
| 172 | + >>> # If you are creating a brand new holiday list you would |
| 173 | + >>> # skip this line |
| 174 | + >>> Holidays._populate(self, year) |
| 175 | + >>> # Remove Columbus Day |
| 176 | + >>> self.pop(date(year,10,1)+relativedelta(weekday=MO(+2)), None) |
| 177 | + >>> # Add Ninja Turtle Day |
| 178 | + >>> self[date(year,7,13)] = "Ninja Turtle Day" |
| 179 | + >>> date(2014,10,14) in Holidays(country="US") |
| 180 | + True |
| 181 | + >>> date(2014,10,14) in CorporateHolidays(country="US") |
| 182 | + False |
| 183 | + >>> date(2014,7,13) in Holidays(country="US") |
| 184 | + False |
| 185 | + >>> date(2014,7,13) in CorporateHolidays(country="US") |
| 186 | + True |
| 187 | +
|
| 188 | + # If you write the code necessary to create a holiday list for a country not |
| 189 | + # not currently supported please contribute your code to the project! |
| 190 | +
|
| 191 | +
|
| 192 | +Development Version |
| 193 | +------------------- |
| 194 | + |
| 195 | +The latest development version can be installed directly from GitHub: |
| 196 | + |
| 197 | +.. code-block:: bash |
| 198 | +
|
| 199 | + $ pip install --upgrade https://github.com/ryanss/holidays.py/tarball/master |
| 200 | +
|
| 201 | +
|
| 202 | +Running Tests |
| 203 | +------------- |
| 204 | + |
| 205 | +.. code-block:: bash |
| 206 | +
|
| 207 | + $ python tests.py |
| 208 | +
|
| 209 | +
|
| 210 | +Contributions |
| 211 | +------------- |
| 212 | + |
| 213 | +.. _issues: https://github.com/ryanss/holidays.py/issues |
| 214 | +.. __: https://github.com/ryanss/holidays.py/pulls |
| 215 | + |
| 216 | +Issues_ and `Pull Requests`__ are always welcome. |
| 217 | + |
| 218 | + |
| 219 | +License |
| 220 | +------- |
| 221 | + |
| 222 | +.. __: https://github.com/ryanss/holidays.py/raw/master/LICENSE |
| 223 | + |
| 224 | +Code and documentation are available according to the MIT License |
| 225 | +(see LICENSE__). |
0 commit comments