summaryrefslogtreecommitdiff
path: root/date-fns/src/isSameHour
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/isSameHour')
-rw-r--r--date-fns/src/isSameHour/benchmark.js23
-rw-r--r--date-fns/src/isSameHour/index.d.ts4
-rw-r--r--date-fns/src/isSameHour/index.js.flow55
-rw-r--r--date-fns/src/isSameHour/index.ts33
-rw-r--r--date-fns/src/isSameHour/test.ts51
5 files changed, 166 insertions, 0 deletions
diff --git a/date-fns/src/isSameHour/benchmark.js b/date-fns/src/isSameHour/benchmark.js
new file mode 100644
index 0000000..c21d8c1
--- /dev/null
+++ b/date-fns/src/isSameHour/benchmark.js
@@ -0,0 +1,23 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import isSameHour from '.'
+import moment from 'moment'
+
+suite('isSameHour', function () {
+ benchmark('date-fns', function () {
+ return isSameHour(this.dateA, this.dateB)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.momentA.isSame(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/isSameHour/index.d.ts b/date-fns/src/isSameHour/index.d.ts
new file mode 100644
index 0000000..78d4662
--- /dev/null
+++ b/date-fns/src/isSameHour/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { isSameHour } from 'date-fns'
+export default isSameHour
diff --git a/date-fns/src/isSameHour/index.js.flow b/date-fns/src/isSameHour/index.js.flow
new file mode 100644
index 0000000..55ea578
--- /dev/null
+++ b/date-fns/src/isSameHour/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
+) => boolean
diff --git a/date-fns/src/isSameHour/index.ts b/date-fns/src/isSameHour/index.ts
new file mode 100644
index 0000000..d6e362e
--- /dev/null
+++ b/date-fns/src/isSameHour/index.ts
@@ -0,0 +1,33 @@
+import startOfHour from '../startOfHour/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name isSameHour
+ * @category Hour Helpers
+ * @summary Are the given dates in the same hour?
+ *
+ * @description
+ * Are the given dates in the same hour?
+ *
+ * ### 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 first date to check
+ * @param {Date|Number} dateRight - the second date to check
+ * @returns {Boolean} the dates are in the same hour
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
+ * var result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 6, 30))
+ * //=> true
+ */
+export default function isSameHour(dirtyDateLeft: Date | number, dirtyDateRight: Date | number): boolean {
+ requiredArgs(2, arguments)
+
+ const dateLeftStartOfHour = startOfHour(dirtyDateLeft)
+ const dateRightStartOfHour = startOfHour(dirtyDateRight)
+
+ return dateLeftStartOfHour.getTime() === dateRightStartOfHour.getTime()
+}
diff --git a/date-fns/src/isSameHour/test.ts b/date-fns/src/isSameHour/test.ts
new file mode 100644
index 0000000..dbbe3e2
--- /dev/null
+++ b/date-fns/src/isSameHour/test.ts
@@ -0,0 +1,51 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import isSameHour from '.'
+
+describe('isSameHour', function() {
+ it('returns true if the given dates have the same hour', function() {
+ const result = isSameHour(
+ new Date(2014, 8 /* Sep */, 4, 6, 0),
+ new Date(2014, 8 /* Sep */, 4, 6, 30)
+ )
+ assert(result === true)
+ })
+
+ it('returns false if the given dates have different hours', function() {
+ const result = isSameHour(
+ new Date(2014, 8 /* Sep */, 4, 6, 0),
+ new Date(2014, 8 /* Sep */, 4, 5, 0)
+ )
+ assert(result === false)
+ })
+
+ it('accepts a timestamp', function() {
+ const result = isSameHour(
+ new Date(2014, 8 /* Sep */, 4, 18, 0).getTime(),
+ new Date(2014, 8 /* Sep */, 4, 18, 45).getTime()
+ )
+ assert(result === true)
+ })
+
+ it('returns false if the first date is `Invalid Date`', function() {
+ const result = isSameHour(new Date(NaN), new Date(1989, 6 /* Jul */, 10))
+ assert(result === false)
+ })
+
+ it('returns false if the second date is `Invalid Date`', function() {
+ const result = isSameHour(new Date(1987, 1 /* Feb */, 11), new Date(NaN))
+ assert(result === false)
+ })
+
+ it('returns false if the both dates are `Invalid Date`', function() {
+ const result = isSameHour(new Date(NaN), new Date(NaN))
+ assert(result === false)
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function() {
+ assert.throws(isSameHour.bind(null), TypeError)
+ assert.throws(isSameHour.bind(null, 1), TypeError)
+ })
+})