@18f/us-federal-holidays
Advanced tools
Comparing version 1.1.1 to 1.2.0
"use strict"; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function getNthDayOf(n, day, month, year) { | ||
@@ -31,4 +33,10 @@ var firstOfMonth = new Date(Date.parse(month + "/1/" + year + " GMT")); | ||
function allFederalHolidaysForYear() { | ||
var year = arguments.length <= 0 || arguments[0] === undefined ? new Date().getFullYear() : arguments[0]; | ||
var year = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date().getFullYear(); | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$shiftSaturdayHol = _ref.shiftSaturdayHolidays, | ||
shiftSaturdayHolidays = _ref$shiftSaturdayHol === undefined ? true : _ref$shiftSaturdayHol, | ||
_ref$shiftSundayHolid = _ref.shiftSundayHolidays, | ||
shiftSundayHolidays = _ref$shiftSundayHolid === undefined ? true : _ref$shiftSundayHolid; | ||
var holidays = []; | ||
@@ -86,31 +94,33 @@ | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
if (shiftSaturdayHolidays || shiftSundayHolidays) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = holidays[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var holiday = _step.value; | ||
try { | ||
for (var _iterator = holidays[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var holiday = _step.value; | ||
var dow = holiday.date.getUTCDay(); | ||
var dow = holiday.date.getUTCDay(); | ||
if (dow == 0) { | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() + 1)); | ||
} else if (dow == 6) { | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() - 1)); | ||
} | ||
if (dow == 0 && shiftSundayHolidays) { | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() + 1)); | ||
} else if (dow == 6 && shiftSaturdayHolidays) { | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() - 1)); | ||
} | ||
holiday.dateString = holiday.date.getUTCFullYear() + "-" + (holiday.date.getUTCMonth() + 1) + "-" + holiday.date.getUTCDate(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
holiday.dateString = holiday.date.getUTCFullYear() + "-" + (holiday.date.getUTCMonth() + 1) + "-" + holiday.date.getUTCDate(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
@@ -125,10 +135,24 @@ } | ||
isAHoliday: function isAHoliday() { | ||
var date = arguments.length <= 0 || arguments[0] === undefined ? new Date() : arguments[0]; | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref2$shiftSaturdayHo = _ref2.shiftSaturdayHolidays, | ||
shiftSaturdayHolidays = _ref2$shiftSaturdayHo === undefined ? true : _ref2$shiftSaturdayHo, | ||
_ref2$shiftSundayHoli = _ref2.shiftSundayHolidays, | ||
shiftSundayHolidays = _ref2$shiftSundayHoli === undefined ? true : _ref2$shiftSundayHoli, | ||
_ref2$utc = _ref2.utc, | ||
utc = _ref2$utc === undefined ? false : _ref2$utc; | ||
var isHoliday = false; | ||
var allForYear = allFederalHolidaysForYear(date.getFullYear()).concat(allFederalHolidaysForYear(date.getFullYear() + 1)); | ||
var mm = date.getMonth(), | ||
dd = date.getDate(); | ||
var year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
var shift = { shiftSaturdayHolidays: shiftSaturdayHolidays, shiftSundayHolidays: shiftSundayHolidays }; | ||
var thisYear = allFederalHolidaysForYear(year, shift); | ||
var nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
var allForYear = [].concat(_toConsumableArray(thisYear), [nextYear[0]]); | ||
var mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
var dd = utc ? date.getUTCDate() : date.getDate(); | ||
var _iteratorNormalCompletion2 = true; | ||
@@ -135,0 +159,0 @@ var _didIteratorError2 = false; |
## Welcome! | ||
We're so glad you're thinking about contributing to an 18F open source project! If you're unsure about anything, just ask -- or submit the issue or pull request anyway. The worst that can happen is you'll be politely asked to change something. We love all friendly contributions. | ||
We're so glad you're thinking about contributing to an 18F open source project! | ||
If you're unsure about anything, just ask -- or submit the issue or pull | ||
request anyway. The worst that can happen is you'll be politely asked to | ||
change something. We love all friendly contributions. | ||
We want to ensure a welcoming environment for all of our projects. Our staff follow the [18F Code of Conduct](https://github.com/18F/code-of-conduct/blob/master/code-of-conduct.md) and all contributors should do the same. | ||
We want to ensure a welcoming environment for all of our projects. Our staff | ||
follow the [18F Code of Conduct](https://github.com/18F/code-of-conduct/blob/master/code-of-conduct.md) | ||
and all contributors should do the same. | ||
We encourage you to read this project's CONTRIBUTING policy (you are here), its [LICENSE](LICENSE.md), and its [README](README.md). | ||
We encourage you to read this project's CONTRIBUTING policy (you are here), its | ||
[LICENSE](LICENSE.md), and its [README](README.md). | ||
If you have any questions or want to read more, check out the [18F Open Source Policy GitHub repository]( https://github.com/18f/open-source-policy), or just [shoot us an email](mailto:18f@gsa.gov). | ||
If you have any questions or want to read more, check out the | ||
[18F Open Source Policy GitHub repository](https://github.com/18f/open-source-policy), | ||
or just [shoot us an email](mailto:18f@gsa.gov). | ||
## Contributing | ||
We take contributions via Github pull requests into this repo. In order to | ||
create a pull request, you should first fork this repo into your own | ||
organization or user account. | ||
Make your changes to `src/index.js`. If you're changing or adding behaviors, | ||
modify or add tests to `index.test.js`. Run your tests with `npm test`. Once | ||
your changes are finished and all tests pass, rebuild with `npm run build`, | ||
which will update `bin/index.js`. Commit all your changes and push them to | ||
your fork and open a pull request into this repo. | ||
When you open your pull request, please tell us a little about what you're | ||
changing or adding so we can know what we're looking at. Someone from the | ||
18F team will have to review the pull request before we can merge it which | ||
might take a little while - please be patient with us! | ||
## Public domain | ||
@@ -12,0 +37,0 @@ |
{ | ||
"name": "@18f/us-federal-holidays", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "All about US federal holidays", | ||
@@ -18,3 +18,4 @@ "main": "bin/index.js", | ||
"scripts": { | ||
"build": "babel src -d bin --no-comments" | ||
"build": "babel src -d bin --no-comments", | ||
"test": "tap --reporter=spec index.test.js" | ||
}, | ||
@@ -29,5 +30,6 @@ "babel": { | ||
"babel-cli": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0" | ||
"babel-preset-es2015": "^6.5.0", | ||
"tap": "^12.0.1" | ||
}, | ||
"dependencies": {} | ||
} |
# US Federal Holidays | ||
Builds and returns a list of all US federal holidays for a given year, and provides a helper method to determine if a given date is a US federal holiday. Handles shifting holidays to the nearest weekday if the holiday falls on a weekend. | ||
Builds and returns a list of all US federal holidays for a given year, and provides a helper method to determine if a given date is a US federal holiday. Handles shifting holidays to the nearest weekday if the holiday falls on a weekend. | ||
@@ -15,3 +15,3 @@ US federal holidays are [as defined by OPM](https://www.opm.gov/fedhol/). | ||
To get a list of all US federal holidays in a given year, use the `allForYear` method. If no year is passed in, uses the current year. | ||
To get a list of all US federal holidays in a given year, use the `allForYear` method. If no year is passed in, uses the current year. | ||
@@ -55,20 +55,40 @@ ```javascript | ||
To determine if a date is a federal holiday, use the `isAHoliday` method. If no argument is provided, defaults to the current date: | ||
To determine if a date is a federal holiday, use the `isAHoliday` method. If no argument is provided, defaults to the current date: | ||
```javascript | ||
var fedHolidays = require('us-federal-holidays'); | ||
var isAHoliday = fedHolidays.isAHoliday(myDate); | ||
var isAHoliday = fedHolidays.isAHoliday(myDate, opts); | ||
// Returns true or false | ||
``` | ||
### Timezones | ||
The options is a plain object: | ||
Internally, all dates are stored in UTC. ***However***, the `isAHoliday` method uses the timezone of the date object passed into it (or the timezone of the machine, if not argument is passed). Thus, if it is 10:00 PM Eastern Standard Time on December 25 and you call `isAHoliday` with no argument, it will return `true`, even though the UTC time would be 3:00 AM on December 26. | ||
```javascript | ||
{ | ||
// Whether or not holidays that fall on Saturdays should be | ||
// shifted to Friday observance. Defaults to true, but if | ||
// you don't follow the US federal standard for observing | ||
// holidays on weekends, you can adjust! | ||
shiftSaturdayHolidays: boolean, | ||
// Whether or not holidays that fall on Sundays should be | ||
// shifted to Monday observance. Defaults to true, but if | ||
// you don't follow the US federal standard for observing | ||
// holidays on weekends, you can adjust! | ||
shiftSundayHolidays: boolean, | ||
// Whether to treat the first argument as a UTC date instead | ||
// of the local time. Defaults to false. This is useful if | ||
// you're generating dates from UTC timestamps or otherwise | ||
// creating objects from UTC-based dates. | ||
utc: boolean | ||
} | ||
``` | ||
### Public domain | ||
This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.md): | ||
This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.md): | ||
> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/). | ||
> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/). | ||
> | ||
> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest. | ||
> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest. |
function getNthDayOf(n, day, month, year) { | ||
const firstOfMonth = new Date(Date.parse(`${month}/1/${year} GMT`)); | ||
let dayOffset = (firstOfMonth.getUTCDay() - day); | ||
if(dayOffset > 0) { | ||
let dayOffset = firstOfMonth.getUTCDay() - day; | ||
if (dayOffset > 0) { | ||
dayOffset = 7 - dayOffset; | ||
@@ -12,3 +12,3 @@ } else { | ||
const finalDay = initialDay + (7 * (n - 1)); | ||
const finalDay = initialDay + 7 * (n - 1); | ||
return new Date(Date.parse(`${month}/${finalDay}/${year} GMT`)); | ||
@@ -19,6 +19,6 @@ } | ||
const firstOfDay = getNthDayOf(1, day, month, year).getUTCDate(); | ||
const daysInMonth = (new Date(year, month, 0)).getUTCDate() - 7; | ||
const daysInMonth = new Date(year, month, 0).getUTCDate() - 7; | ||
let lastOfDay = firstOfDay; | ||
while(lastOfDay <= daysInMonth) { | ||
while (lastOfDay <= daysInMonth) { | ||
lastOfDay += 7; | ||
@@ -30,7 +30,8 @@ } | ||
function allFederalHolidaysForYear(year = (new Date().getFullYear())) { | ||
const holidays = [ ]; | ||
function allFederalHolidaysForYear( | ||
year = new Date().getFullYear(), | ||
{ shiftSaturdayHolidays = true, shiftSundayHolidays = true } = {} | ||
) { | ||
const holidays = []; | ||
//const firstDay = new Date(Date.parse(`1/1/${year} GMT`)); | ||
// New Year's Day | ||
@@ -103,16 +104,31 @@ holidays.push({ | ||
for(let holiday of holidays) { | ||
const dow = holiday.date.getUTCDay(); | ||
if (shiftSaturdayHolidays || shiftSundayHolidays) { | ||
for (let holiday of holidays) { | ||
const dow = holiday.date.getUTCDay(); | ||
if(dow == 0) { | ||
// Actual holiday falls on Sunday. Shift | ||
// the observed date forward to Monday. | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() + 1)); | ||
} else if(dow == 6) { | ||
// Actual holiday falls on Saturday. Shift | ||
// the observed date backward to Friday. | ||
holiday.date = new Date(Date.UTC(holiday.date.getUTCFullYear(), holiday.date.getUTCMonth(), holiday.date.getUTCDate() - 1)); | ||
if (dow == 0 && shiftSundayHolidays) { | ||
// Actual holiday falls on Sunday. Shift | ||
// the observed date forward to Monday. | ||
holiday.date = new Date( | ||
Date.UTC( | ||
holiday.date.getUTCFullYear(), | ||
holiday.date.getUTCMonth(), | ||
holiday.date.getUTCDate() + 1 | ||
) | ||
); | ||
} else if (dow == 6 && shiftSaturdayHolidays) { | ||
// Actual holiday falls on Saturday. Shift | ||
// the observed date backward to Friday. | ||
holiday.date = new Date( | ||
Date.UTC( | ||
holiday.date.getUTCFullYear(), | ||
holiday.date.getUTCMonth(), | ||
holiday.date.getUTCDate() - 1 | ||
) | ||
); | ||
} | ||
holiday.dateString = `${holiday.date.getUTCFullYear()}-${holiday.date.getUTCMonth() + | ||
1}-${holiday.date.getUTCDate()}`; | ||
} | ||
holiday.dateString = `${holiday.date.getUTCFullYear()}-${holiday.date.getUTCMonth() + 1}-${holiday.date.getUTCDate()}`; | ||
} | ||
@@ -124,16 +140,31 @@ | ||
module.exports = { | ||
isAHoliday(date = new Date()) { | ||
isAHoliday( | ||
date = new Date(), | ||
{ | ||
shiftSaturdayHolidays = true, | ||
shiftSundayHolidays = true, | ||
utc = false | ||
} = {} | ||
) { | ||
let isHoliday = false; | ||
// Get this year and next, to handle the case where December 31 is the | ||
// observed holiday for January 1 of the following year. | ||
const allForYear = allFederalHolidaysForYear(date.getFullYear()).concat(allFederalHolidaysForYear(date.getFullYear() + 1)); | ||
const mm = date.getMonth(), dd = date.getDate(); | ||
const year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
const shift = { shiftSaturdayHolidays, shiftSundayHolidays }; | ||
for(let holiday of allForYear) { | ||
if(holiday.date.getUTCMonth() == mm && holiday.date.getUTCDate() == dd) { | ||
// Get this year and the first holiday of the next year, | ||
// to handle the case where December 31 is the observed | ||
// holiday for January 1 of the following year. | ||
const thisYear = allFederalHolidaysForYear(year, shift); | ||
const nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
const allForYear = [...thisYear, nextYear[0]]; | ||
const mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
const dd = utc ? date.getUTCDate() : date.getDate(); | ||
for (let holiday of allForYear) { | ||
if (holiday.date.getUTCMonth() == mm && holiday.date.getUTCDate() == dd) { | ||
isHoliday = true; | ||
break; | ||
} | ||
if(holiday.date.getUTCMonth() > mm) { | ||
if (holiday.date.getUTCMonth() > mm) { | ||
break; | ||
@@ -140,0 +171,0 @@ } |
23637
392
93
3