diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/differenceInDays | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to 'date-fns/src/differenceInDays')
-rw-r--r-- | date-fns/src/differenceInDays/benchmark.js | 23 | ||||
-rw-r--r-- | date-fns/src/differenceInDays/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/differenceInDays/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/differenceInDays/index.ts | 104 | ||||
-rw-r--r-- | date-fns/src/differenceInDays/test.ts | 214 |
5 files changed, 400 insertions, 0 deletions
diff --git a/date-fns/src/differenceInDays/benchmark.js b/date-fns/src/differenceInDays/benchmark.js new file mode 100644 index 0000000..2bc0f44 --- /dev/null +++ b/date-fns/src/differenceInDays/benchmark.js @@ -0,0 +1,23 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import differenceInDays from '.' +import moment from 'moment' + +suite('differenceInDays', function () { + benchmark('date-fns', function () { + return differenceInDays(this.dateA, this.dateB) + }) + + benchmark('Moment.js', function () { + return this.momentA.diff(this.momentB, 'day') + }) +}, { + setup: function () { + this.dateA = new Date() + this.momentA = moment() + this.dateB = new Date(this.dateA.getTime() + 604800000) + this.momentB = this.momentA.clone().add(7, 'days') + } +}) diff --git a/date-fns/src/differenceInDays/index.d.ts b/date-fns/src/differenceInDays/index.d.ts new file mode 100644 index 0000000..de04cde --- /dev/null +++ b/date-fns/src/differenceInDays/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { differenceInDays } from 'date-fns' +export default differenceInDays diff --git a/date-fns/src/differenceInDays/index.js.flow b/date-fns/src/differenceInDays/index.js.flow new file mode 100644 index 0000000..c799819 --- /dev/null +++ b/date-fns/src/differenceInDays/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/differenceInDays/index.ts b/date-fns/src/differenceInDays/index.ts new file mode 100644 index 0000000..5851836 --- /dev/null +++ b/date-fns/src/differenceInDays/index.ts @@ -0,0 +1,104 @@ +import toDate from '../toDate/index' +import differenceInCalendarDays from '../differenceInCalendarDays/index' +import requiredArgs from '../_lib/requiredArgs/index' + +// Like `compareAsc` but uses local time not UTC, which is needed +// for accurate equality comparisons of UTC timestamps that end up +// having the same representation in local time, e.g. one hour before +// DST ends vs. the instant that DST ends. +function compareLocalAsc(dateLeft: Date, dateRight: Date): number { + const diff = + dateLeft.getFullYear() - dateRight.getFullYear() || + dateLeft.getMonth() - dateRight.getMonth() || + dateLeft.getDate() - dateRight.getDate() || + dateLeft.getHours() - dateRight.getHours() || + dateLeft.getMinutes() - dateRight.getMinutes() || + dateLeft.getSeconds() - dateRight.getSeconds() || + dateLeft.getMilliseconds() - dateRight.getMilliseconds() + + if (diff < 0) { + return -1 + } else if (diff > 0) { + return 1 + // Return 0 if diff is 0; return NaN if diff is NaN + } else { + return diff + } +} + +/** + * @name differenceInDays + * @category Day Helpers + * @summary Get the number of full days between the given dates. + * + * @description + * Get the number of full day periods between two dates. Fractional days are + * truncated towards zero. + * + * One "full day" is the distance between a local time in one day to the same + * local time on the next or previous day. A full day can sometimes be less than + * or more than 24 hours if a daylight savings change happens between two dates. + * + * To ignore DST and only measure exact 24-hour periods, use this instead: + * `Math.floor(differenceInHours(dateLeft, dateRight)/24)|0`. + * + * + * ### v2.0.0 breaking changes: + * + * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). + * + * @param {Date|Number} dateLeft - the later date + * @param {Date|Number} dateRight - the earlier date + * @returns {Number} the number of full days according to the local timezone + * @throws {TypeError} 2 arguments required + * + * @example + * // How many full days are between + * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00? + * const result = differenceInDays( + * new Date(2012, 6, 2, 0, 0), + * new Date(2011, 6, 2, 23, 0) + * ) + * //=> 365 + * // How many full days are between + * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00? + * const result = differenceInDays( + * new Date(2011, 6, 3, 0, 1), + * new Date(2011, 6, 2, 23, 59) + * ) + * //=> 0 + * // How many full days are between + * // 1 March 2020 0:00 and 1 June 2020 0:00 ? + * // Note: because local time is used, the + * // result will always be 92 days, even in + * // time zones where DST starts and the + * // period has only 92*24-1 hours. + * const result = differenceInDays( + * new Date(2020, 5, 1), + * new Date(2020, 2, 1) + * ) +//=> 92 + */ +export default function differenceInDays( + dirtyDateLeft: Date | number, + dirtyDateRight: Date | number +): number { + requiredArgs(2, arguments) + + const dateLeft = toDate(dirtyDateLeft) + const dateRight = toDate(dirtyDateRight) + + const sign = compareLocalAsc(dateLeft, dateRight) + const difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight)) + + dateLeft.setDate(dateLeft.getDate() - sign * difference) + + // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full + // If so, result must be decreased by 1 in absolute value + const isLastDayNotFull = Number( + compareLocalAsc(dateLeft, dateRight) === -sign + ) + const result = sign * (difference - isLastDayNotFull) + // Prevent negative zero + return result === 0 ? 0 : result +} diff --git a/date-fns/src/differenceInDays/test.ts b/date-fns/src/differenceInDays/test.ts new file mode 100644 index 0000000..4afa6f8 --- /dev/null +++ b/date-fns/src/differenceInDays/test.ts @@ -0,0 +1,214 @@ +/* eslint-env mocha */ + +import assert from 'power-assert' +import differenceInDays from '.' +import { getDstTransitions } from '../../test/dst/tzOffsetTransitions' + +describe('differenceInDays', function () { + it('returns the number of full days between the given dates', function () { + const result = differenceInDays( + new Date(2012, 6 /* Jul */, 2, 18, 0), + new Date(2011, 6 /* Jul */, 2, 6, 0) + ) + assert(result === 366) + }) + + it('returns a negative number if the time value of the first date is smaller', function () { + const result = differenceInDays( + new Date(2011, 6 /* Jul */, 2, 6, 0), + new Date(2012, 6 /* Jul */, 2, 18, 0) + ) + assert(result === -366) + }) + + it('accepts timestamps', function () { + const result = differenceInDays( + new Date(2014, 8 /* Sep */, 5, 18, 0).getTime(), + new Date(2014, 8 /* Sep */, 4, 6, 0).getTime() + ) + assert(result === 1) + }) + + describe('edge cases', function () { + it('the difference is less than a day, but the given dates are in different calendar days', function () { + const result = differenceInDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 4, 23, 59) + ) + assert(result === 0) + }) + + it('the same for the swapped dates', function () { + const result = differenceInDays( + new Date(2014, 8 /* Sep */, 4, 23, 59), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === 0) + }) + + it('the time values of the given dates are the same', function () { + const result = differenceInDays( + new Date(2014, 8 /* Sep */, 6, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === 1) + }) + + it('the given dates are the same', function () { + const result = differenceInDays( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === 0) + }) + + const dstTransitions = getDstTransitions(2017) + const dstOnly = dstTransitions.start && dstTransitions.end ? it : it.skip + const tz = + Intl.DateTimeFormat().resolvedOptions().timeZone || process.env.tz + dstOnly( + `works across DST start & end in local timezone: ${tz || '(unknown)'}`, + function () { + const { start, end } = dstTransitions + const HOUR = 1000 * 60 * 60 + const MINUTE = 1000 * 60 + function sameTime(t1: Date, t2: Date): boolean { + return ( + t1.getHours() === t2.getHours() && + t1.getMinutes() === t2.getMinutes() && + t1.getSeconds() === t2.getSeconds() && + t1.getMilliseconds() === t2.getMilliseconds() + ) + } + + assert(start !== undefined) + assert(end !== undefined) + + if (start === undefined || end === undefined) { + return + } + + // It's usually 1 hour, but for some timezones, e.g. Australia/Lord_Howe, it is 30 minutes + const dstOffset = + (end.getTimezoneOffset() - start.getTimezoneOffset()) * MINUTE + + // TEST DST START (SPRING) + + // anchor to one hour before the boundary + { + const a = new Date(start.getTime() - HOUR) // 1 hour before DST + const b = new Date(a.getTime() + 24 * HOUR - dstOffset) // 1 day later, same local time + const c = new Date(a.getTime() + 48 * HOUR - dstOffset) // 2 days later, same local time + + assert(sameTime(a, b)) + assert(sameTime(a, c)) + assert(sameTime(b, c)) + assert(differenceInDays(c, b) === 1) // normal 24-hour day + assert(differenceInDays(b, a) === 1) // 23 hours -> 1 day + assert(differenceInDays(c, a) === 2) // 47 hours -> 2 days + } + // anchor exactly, the boundary + { + const a = start // exactly when DST starts + const b = new Date(a.getTime() + 24 * HOUR) // 1 day later, same local time + const c = new Date(a.getTime() + 48 * HOUR) // 2 days later, same local time + + assert(sameTime(a, b)) + assert(sameTime(a, c)) + assert(sameTime(b, c)) + assert(differenceInDays(c, b) === 1) // normal 24-hour day + assert(differenceInDays(b, a) === 1) // normal 24-hour day + assert(differenceInDays(c, a) === 2) // 2 normal 24-hour days + } + + // TEST DST END (FALL) + + // make sure that diffs across a "fall back" DST boundary won't report a full day + // until 25 hours have elapsed. + { + const a = new Date(end.getTime() - HOUR / 2) // 1 hour before Standard Time starts + const b = new Date(a.getTime() + 24 * HOUR + dstOffset - 15 * MINUTE) // 1 day later, 15 mins earlier local time + const c = new Date(a.getTime() + 48 * HOUR + dstOffset - 15 * MINUTE) // 2 days later, 15 mins earlier local time + + assert(differenceInDays(c, b) === 1) // normal 24-hour day + assert(differenceInDays(b, a) === 0) // 24.75 hours -> 0 full days (because hour lost to DST) + assert(differenceInDays(c, a) === 1) // 48.75 hours -> 1 full day (because hour lost to DST) + } + // anchor to one hour before the boundary + { + const a = new Date(end.getTime() - HOUR) // 1 hour before Standard Time start + const b = new Date(a.getTime() + 24 * HOUR + dstOffset) // 1 day later, same local time + const c = new Date(a.getTime() + 48 * HOUR + dstOffset) // 2 days later, same local time + + assert(sameTime(a, b)) + assert(sameTime(a, c)) + assert(sameTime(b, c)) + assert(differenceInDays(c, b) === 1) // normal 24-hour day + assert(differenceInDays(b, a) === 1) // 25 hours -> 1 day + assert(differenceInDays(c, a) === 2) // 49 hours -> 2 days + } + // anchor to one hour after the boundary + { + const a = new Date(end.getTime() + HOUR) // 1 hour after Standard Time start + const b = new Date(a.getTime() + 24 * HOUR) // 1 day later, same local time + const c = new Date(a.getTime() + 48 * HOUR) // 2 days later, same local time + + assert(sameTime(a, b)) + assert(sameTime(a, c)) + assert(sameTime(b, c)) + assert(differenceInDays(c, b) === 1) // normal 24-hour day + assert(differenceInDays(b, a) === 1) // normal 24-hour day + assert(differenceInDays(c, a) === 2) // 2 normal 24-hour days + } + // anchor exactly at the boundary + { + const a = end // exactly when Standard Time starts + const b = new Date(a.getTime() + 24 * HOUR) // 1 day later, same local time + const c = new Date(a.getTime() + 48 * HOUR) // 2 days later, same local time + assert(differenceInDays(b, a) === 1) // normal 24-hour day + assert(differenceInDays(c, a) === 2) // 2 normal 24-hour days + } + } + ) + + it('does not return -0 when the given dates are the same', () => { + function isNegativeZero(x: number): boolean { + return x === 0 && 1 / x < 0 + } + + const result = differenceInDays( + 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 = differenceInDays( + 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 = differenceInDays( + 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 = differenceInDays(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function () { + assert.throws(differenceInDays.bind(null), TypeError) + assert.throws(differenceInDays.bind(null, 1), TypeError) + }) +}) |