diff options
Diffstat (limited to 'date-fns/src/differenceInBusinessDays')
-rw-r--r-- | date-fns/src/differenceInBusinessDays/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/differenceInBusinessDays/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/differenceInBusinessDays/index.ts | 62 | ||||
-rw-r--r-- | date-fns/src/differenceInBusinessDays/test.ts | 141 |
4 files changed, 262 insertions, 0 deletions
diff --git a/date-fns/src/differenceInBusinessDays/index.d.ts b/date-fns/src/differenceInBusinessDays/index.d.ts new file mode 100644 index 0000000..8decd89 --- /dev/null +++ b/date-fns/src/differenceInBusinessDays/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { differenceInBusinessDays } from 'date-fns' +export default differenceInBusinessDays diff --git a/date-fns/src/differenceInBusinessDays/index.js.flow b/date-fns/src/differenceInBusinessDays/index.js.flow new file mode 100644 index 0000000..c799819 --- /dev/null +++ b/date-fns/src/differenceInBusinessDays/index.js.flow @@ -0,0 +1,55 @@ +// @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: ( + dateLeft: Date | number, + dateRight: Date | number +) => number diff --git a/date-fns/src/differenceInBusinessDays/index.ts b/date-fns/src/differenceInBusinessDays/index.ts new file mode 100644 index 0000000..46fd575 --- /dev/null +++ b/date-fns/src/differenceInBusinessDays/index.ts @@ -0,0 +1,62 @@ +import isValid from '../isValid/index' +import isWeekend from '../isWeekend/index' +import toDate from '../toDate/index' +import differenceInCalendarDays from '../differenceInCalendarDays/index' +import addDays from '../addDays/index' +import isSameDay from '../isSameDay/index' +import toInteger from '../_lib/toInteger/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name differenceInBusinessDays + * @category Day Helpers + * @summary Get the number of business days between the given dates. + * + * @description + * Get the number of business day periods between the given dates. + * Business days being days that arent in the weekend. + * Like `differenceInCalendarDays`, the function removes the times from + * the dates before calculating the difference. + * + * @param {Date|Number} dateLeft - the later date + * @param {Date|Number} dateRight - the earlier date + * @returns {Number} the number of business days + * @throws {TypeError} 2 arguments required + * + * @example + * // How many business days are between + * // 10 January 2014 and 20 July 2014? + * var result = differenceInBusinessDays( + * new Date(2014, 6, 20), + * new Date(2014, 0, 10) + * ) + * //=> 136 + */ +export default function differenceInBusinessDays( + dirtyDateLeft: Date | number, + dirtyDateRight: Date | number +): number { + requiredArgs(2, arguments) + + const dateLeft = toDate(dirtyDateLeft) + let dateRight = toDate(dirtyDateRight) + + if (!isValid(dateLeft) || !isValid(dateRight)) return NaN + + const calendarDifference = differenceInCalendarDays(dateLeft, dateRight) + const sign = calendarDifference < 0 ? -1 : 1 + + const weeks = toInteger(calendarDifference / 7) + + let result = weeks * 5 + dateRight = addDays(dateRight, weeks * 7) + + // the loop below will run at most 6 times to account for the remaining days that don't makeup a full week + while (!isSameDay(dateLeft, dateRight)) { + // sign is used to account for both negative and positive differences + result += isWeekend(dateRight) ? 0 : sign + dateRight = addDays(dateRight, sign) + } + + return result === 0 ? 0 : result +} diff --git a/date-fns/src/differenceInBusinessDays/test.ts b/date-fns/src/differenceInBusinessDays/test.ts new file mode 100644 index 0000000..f30e671 --- /dev/null +++ b/date-fns/src/differenceInBusinessDays/test.ts @@ -0,0 +1,141 @@ +/* eslint-env mocha */ + +import assert from 'power-assert' +import differenceInBusinessDays from '.' + +describe('differenceInBusinessDays', function () { + it('returns the number of business days between the given dates, excluding weekends', function () { + const result = differenceInBusinessDays( + new Date(2014, 6 /* Jul */, 18), + new Date(2014, 0 /* Jan */, 10) + ) + assert(result === 135) + }) + + it('can handle long ranges', function () { + // @ts-ignore + if (typeof this.timeout === 'function') { + // @ts-ignore + this.timeout(500 /* 500 ms test timeout */) + } + const result = differenceInBusinessDays( + new Date(15000, 0 /* Jan */, 1), + new Date(2014, 0 /* Jan */, 1) + ) + assert(result === 3387885) + }) + + it('the same except given first date falls on a weekend', function () { + const result = differenceInBusinessDays( + new Date(2019, 6 /* Jul */, 20), + new Date(2019, 6 /* Jul */, 18) + ) + assert(result === 2) + }) + + it('the same except given second date falls on a weekend', function () { + const result = differenceInBusinessDays( + new Date(2019, 6 /* Jul */, 23), + new Date(2019, 6 /* Jul */, 20) + ) + assert(result === 1) + }) + + it('the same except both given dates fall on a weekend', function () { + const result = differenceInBusinessDays( + new Date(2019, 6 /* Jul */, 28), + new Date(2019, 6 /* Jul */, 20) + ) + assert(result === 5) + }) + + it('returns a negative number if the time value of the first date is smaller', function () { + const result = differenceInBusinessDays( + new Date(2014, 0 /* Jan */, 10), + new Date(2014, 6 /* Jul */, 20) + ) + assert(result === -135) + }) + + it('accepts timestamps', function () { + const result = differenceInBusinessDays( + new Date(2014, 6, 18).getTime(), + new Date(2014, 0, 10).getTime() + ) + assert(result === 135) + }) + + describe('edge cases', function () { + it('the difference is less than a day, but the given dates are in different calendar days', function () { + const result = differenceInBusinessDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 4, 23, 59) + ) + assert(result === 1) + }) + + it('the same for the swapped dates', function () { + const result = differenceInBusinessDays( + new Date(2014, 8 /* Sep */, 4, 23, 59), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === -1) + }) + + it('the time values of the given dates are the same', function () { + const result = differenceInBusinessDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 4, 0, 0) + ) + assert(result === 1) + }) + + it('the given dates are the same', function () { + const result = differenceInBusinessDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === 0) + }) + + it('does not return -0 when the given dates are the same', () => { + function isNegativeZero(x: number) { + return x === 0 && 1 / x < 0 + } + + const result = differenceInBusinessDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + + const resultIsNegative = isNegativeZero(result) + assert(resultIsNegative === false) + }) + + it('returns NaN if the first date is `Invalid Date`', function () { + const result = differenceInBusinessDays( + new Date(NaN), + new Date(2017, 0 /* Jan */, 1) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the second date is `Invalid Date`', function () { + const result = differenceInBusinessDays( + new Date(2017, 0 /* Jan */, 1), + new Date(NaN) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the both dates are `Invalid Date`', function () { + const result = differenceInBusinessDays(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function () { + assert.throws(differenceInBusinessDays.bind(null), TypeError) + assert.throws(differenceInBusinessDays.bind(null, 1), TypeError) + }) + }) +}) |