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/differenceInHours | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to 'date-fns/src/differenceInHours')
-rw-r--r-- | date-fns/src/differenceInHours/benchmark.js | 23 | ||||
-rw-r--r-- | date-fns/src/differenceInHours/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/differenceInHours/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/differenceInHours/index.ts | 38 | ||||
-rw-r--r-- | date-fns/src/differenceInHours/test.ts | 107 |
5 files changed, 227 insertions, 0 deletions
diff --git a/date-fns/src/differenceInHours/benchmark.js b/date-fns/src/differenceInHours/benchmark.js new file mode 100644 index 0000000..895a26b --- /dev/null +++ b/date-fns/src/differenceInHours/benchmark.js @@ -0,0 +1,23 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import differenceInHours from '.' +import moment from 'moment' + +suite('differenceInHours', function () { + benchmark('date-fns', function () { + return differenceInHours(this.dateA, this.dateB) + }) + + benchmark('Moment.js', function () { + return this.momentA.diff(this.momentB, 'hour') + }) +}, { + 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/differenceInHours/index.d.ts b/date-fns/src/differenceInHours/index.d.ts new file mode 100644 index 0000000..4559ab9 --- /dev/null +++ b/date-fns/src/differenceInHours/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { differenceInHours } from 'date-fns' +export default differenceInHours diff --git a/date-fns/src/differenceInHours/index.js.flow b/date-fns/src/differenceInHours/index.js.flow new file mode 100644 index 0000000..c799819 --- /dev/null +++ b/date-fns/src/differenceInHours/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/differenceInHours/index.ts b/date-fns/src/differenceInHours/index.ts new file mode 100644 index 0000000..3c940b1 --- /dev/null +++ b/date-fns/src/differenceInHours/index.ts @@ -0,0 +1,38 @@ +import differenceInMilliseconds from '../differenceInMilliseconds/index' +import requiredArgs from '../_lib/requiredArgs/index' + +const MILLISECONDS_IN_HOUR = 3600000 + +/** + * @name differenceInHours + * @category Hour Helpers + * @summary Get the number of hours between the given dates. + * + * @description + * Get the number of hours between the given dates. + * + * ### 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 hours + * @throws {TypeError} 2 arguments required + * + * @example + * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00? + * var result = differenceInHours( + * new Date(2014, 6, 2, 19, 0), + * new Date(2014, 6, 2, 6, 50) + * ) + * //=> 12 + */ +export default function differenceInHours(dirtyDateLeft: Date | number, dirtyDateRight: Date | number): number { + requiredArgs(2, arguments) + + const diff = + differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / + MILLISECONDS_IN_HOUR + return diff > 0 ? Math.floor(diff) : Math.ceil(diff) +} diff --git a/date-fns/src/differenceInHours/test.ts b/date-fns/src/differenceInHours/test.ts new file mode 100644 index 0000000..303b7fa --- /dev/null +++ b/date-fns/src/differenceInHours/test.ts @@ -0,0 +1,107 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import differenceInHours from '.' + +describe('differenceInHours', function() { + it('returns the number of hours between the given dates', function() { + const result = differenceInHours( + new Date(2014, 6 /* Jul */, 2, 20, 0), + new Date(2014, 6 /* Jul */, 2, 6, 0) + ) + assert(result === 14) + }) + + it('returns a negative number if the time value of the first date is smaller', function() { + const result = differenceInHours( + new Date(2014, 6 /* Jul */, 2, 6, 0), + new Date(2014, 6 /* Jul */, 2, 20, 0) + ) + assert(result === -14) + }) + + it('accepts timestamps', function() { + const result = differenceInHours( + new Date(2014, 8 /* Sep */, 5, 18, 0).getTime(), + new Date(2014, 8 /* Sep */, 5, 6, 0).getTime() + ) + assert(result === 12) + }) + + describe('edge cases', function() { + it('the difference is less than an hour, but the given dates are in different calendar hours', function() { + const result = differenceInHours( + new Date(2014, 8 /* Sep */, 5, 12), + new Date(2014, 8 /* Sep */, 5, 11, 59) + ) + assert(result === 0) + }) + + it('the same for the swapped dates', function() { + const result = differenceInHours( + new Date(2014, 8 /* Sep */, 5, 11, 59), + new Date(2014, 8 /* Sep */, 5, 12) + ) + assert(result === 0) + }) + + it('the difference is an integral number of hours', function() { + const result = differenceInHours( + new Date(2014, 8 /* Sep */, 5, 13, 0), + new Date(2014, 8 /* Sep */, 5, 12, 0) + ) + assert(result === 1) + }) + + it('the given dates are the same', function() { + const result = differenceInHours( + 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): boolean { + return x === 0 && 1 / x < 0 + } + + const result = differenceInHours( + 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 = differenceInHours( + 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 = differenceInHours( + 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 = differenceInHours(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + //@ts-expect-error + assert.throws(differenceInHours.bind(null), TypeError) + //@ts-expect-error + assert.throws(differenceInHours.bind(null, 1), TypeError) + }) +}) |