diff options
Diffstat (limited to 'date-fns/src/addBusinessDays')
-rw-r--r-- | date-fns/src/addBusinessDays/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/addBusinessDays/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/addBusinessDays/index.ts | 67 | ||||
-rw-r--r-- | date-fns/src/addBusinessDays/test.ts | 95 |
4 files changed, 218 insertions, 0 deletions
diff --git a/date-fns/src/addBusinessDays/index.d.ts b/date-fns/src/addBusinessDays/index.d.ts new file mode 100644 index 0000000..b1c084e --- /dev/null +++ b/date-fns/src/addBusinessDays/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { addBusinessDays } from 'date-fns' +export default addBusinessDays diff --git a/date-fns/src/addBusinessDays/index.js.flow b/date-fns/src/addBusinessDays/index.js.flow new file mode 100644 index 0000000..f568f0d --- /dev/null +++ b/date-fns/src/addBusinessDays/index.js.flow @@ -0,0 +1,52 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: (date: Date | number, amount: number) => Date diff --git a/date-fns/src/addBusinessDays/index.ts b/date-fns/src/addBusinessDays/index.ts new file mode 100644 index 0000000..0cfdf5c --- /dev/null +++ b/date-fns/src/addBusinessDays/index.ts @@ -0,0 +1,67 @@ +import isWeekend from '../isWeekend/index' +import toDate from '../toDate/index' +import toInteger from '../_lib/toInteger/index' +import requiredArgs from '../_lib/requiredArgs/index' +import isSunday from '../isSunday/index' +import isSaturday from '../isSaturday/index' + +/** + * @name addBusinessDays + * @category Day Helpers + * @summary Add the specified number of business days (mon - fri) to the given date. + * + * @description + * Add the specified number of business days (mon - fri) to the given date, ignoring weekends. + * + * @param {Date|Number} date - the date to be changed + * @param {Number} amount - the amount of business days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. + * @returns {Date} the new date with the business days added + * @throws {TypeError} 2 arguments required + * + * @example + * // Add 10 business days to 1 September 2014: + * const result = addBusinessDays(new Date(2014, 8, 1), 10) + * //=> Mon Sep 15 2014 00:00:00 (skipped weekend days) + */ +export default function addBusinessDays( + dirtyDate: Date | number, + dirtyAmount: number +): Date { + requiredArgs(2, arguments) + + const date = toDate(dirtyDate) + const startedOnWeekend = isWeekend(date) + const amount = toInteger(dirtyAmount) + + if (isNaN(amount)) return new Date(NaN) + + const hours = date.getHours() + const sign = amount < 0 ? -1 : 1 + const fullWeeks = toInteger(amount / 5) + + date.setDate(date.getDate() + fullWeeks * 7) + + // Get remaining days not part of a full week + let restDays = Math.abs(amount % 5) + + // Loops over remaining days + while (restDays > 0) { + date.setDate(date.getDate() + sign) + if (!isWeekend(date)) restDays -= 1 + } + + // If the date is a weekend day and we reduce a dividable of + // 5 from it, we land on a weekend date. + // To counter this, we add days accordingly to land on the next business day + if (startedOnWeekend && isWeekend(date) && amount !== 0) { + // If we're reducing days, we want to add days until we land on a weekday + // If we're adding days we want to reduce days until we land on a weekday + if (isSaturday(date)) date.setDate(date.getDate() + (sign < 0 ? 2 : -1)) + if (isSunday(date)) date.setDate(date.getDate() + (sign < 0 ? 1 : -2)) + } + + // Restore hours to avoid DST lag + date.setHours(hours) + + return date +} diff --git a/date-fns/src/addBusinessDays/test.ts b/date-fns/src/addBusinessDays/test.ts new file mode 100644 index 0000000..e23bc11 --- /dev/null +++ b/date-fns/src/addBusinessDays/test.ts @@ -0,0 +1,95 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import addBusinessDays from '.' + +describe('addBusinessDays', function() { + it('adds the given number of business days', function() { + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), 10) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15)) + }) + + it('handles negative amount', function() { + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 15), -10) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1)) + }) + + it('returns the Monday when 1 day is added on the Friday', () => { + assert.deepStrictEqual( + addBusinessDays(new Date(2020, 0 /* Jan */, 10), 1), // Friday + new Date(2020, 0 /* Jan */, 13) // Monday + ) + }) + + it('returns the Monday when 1 day is added on the Satuday', () => { + assert.deepStrictEqual( + addBusinessDays(new Date(2020, 0 /* Jan */, 11), 1), // Saturday + new Date(2020, 0 /* Jan */, 13) // Monday + ) + }) + + it('returns the Monday when 1 day is added on the Sunday', () => { + assert.deepStrictEqual( + addBusinessDays(new Date(2020, 0 /* Jan */, 12), 1), // Sunday + new Date(2020, 0 /* Jan */, 13) // Monday + ) + }) + + it('can handle a large number of business days', function() { + // @ts-ignore + if (typeof this.timeout === 'function') { + // @ts-ignore + this.timeout(500 /* 500 ms test timeout */) + } + + const result = addBusinessDays(new Date(2014, 0 /* Jan */, 1), 3387885) + assert.deepStrictEqual(result, new Date(15000, 0 /* Jan */, 1)) + }) + + it('accepts a timestamp', function() { + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1).getTime(), 10) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15)) + }) + + it('converts a fractional number to an integer', function() { + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), 10.5) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15)) + }) + + it('implicitly converts number arguments', function() { + // @ts-expect-error + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), '10') + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 1) + addBusinessDays(date, 11) + assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = addBusinessDays(new Date(NaN), 10) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), NaN) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + // @ts-expect-error + assert.throws(addBusinessDays.bind(null), TypeError) + // @ts-expect-error + assert.throws(addBusinessDays.bind(null, 1), TypeError) + }) + it('starting from a weekend day should land on a weekday when reducing a divisible by 5', function() { + const substractResult = addBusinessDays(new Date(2019, 7, 18), -5) + assert.deepStrictEqual(substractResult, new Date(2019, 7, 12)) + + const addResult = addBusinessDays(new Date(2019, 7, 18), 5) + assert.deepStrictEqual(addResult, new Date(2019, 7, 23)) + }) +}) |