summaryrefslogtreecommitdiff
path: root/date-fns/src/isSameSecond
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/isSameSecond')
-rw-r--r--date-fns/src/isSameSecond/benchmark.js23
-rw-r--r--date-fns/src/isSameSecond/index.d.ts4
-rw-r--r--date-fns/src/isSameSecond/index.js.flow55
-rw-r--r--date-fns/src/isSameSecond/index.ts37
-rw-r--r--date-fns/src/isSameSecond/test.ts51
5 files changed, 170 insertions, 0 deletions
diff --git a/date-fns/src/isSameSecond/benchmark.js b/date-fns/src/isSameSecond/benchmark.js
new file mode 100644
index 0000000..2bc1b69
--- /dev/null
+++ b/date-fns/src/isSameSecond/benchmark.js
@@ -0,0 +1,23 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import isSameSecond from '.'
+import moment from 'moment'
+
+suite('isSameSecond', function () {
+ benchmark('date-fns', function () {
+ return isSameSecond(this.dateA, this.dateB)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.momentA.isSame(this.momentB, 'second')
+ })
+}, {
+ 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/isSameSecond/index.d.ts b/date-fns/src/isSameSecond/index.d.ts
new file mode 100644
index 0000000..982464e
--- /dev/null
+++ b/date-fns/src/isSameSecond/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { isSameSecond } from 'date-fns'
+export default isSameSecond
diff --git a/date-fns/src/isSameSecond/index.js.flow b/date-fns/src/isSameSecond/index.js.flow
new file mode 100644
index 0000000..55ea578
--- /dev/null
+++ b/date-fns/src/isSameSecond/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/isSameSecond/index.ts b/date-fns/src/isSameSecond/index.ts
new file mode 100644
index 0000000..cc53cf4
--- /dev/null
+++ b/date-fns/src/isSameSecond/index.ts
@@ -0,0 +1,37 @@
+import startOfSecond from '../startOfSecond/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name isSameSecond
+ * @category Second Helpers
+ * @summary Are the given dates in the same second?
+ *
+ * @description
+ * Are the given dates in the same second?
+ *
+ * ### 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 second
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500
+ * // in the same second?
+ * var result = isSameSecond(
+ * new Date(2014, 8, 4, 6, 30, 15),
+ * new Date(2014, 8, 4, 6, 30, 15, 500)
+ * )
+ * //=> true
+ */
+export default function isSameSecond(dirtyDateLeft: Date | number, dirtyDateRight: Date | number): boolean {
+ requiredArgs(2, arguments)
+
+ const dateLeftStartOfSecond = startOfSecond(dirtyDateLeft)
+ const dateRightStartOfSecond = startOfSecond(dirtyDateRight)
+
+ return dateLeftStartOfSecond.getTime() === dateRightStartOfSecond.getTime()
+}
diff --git a/date-fns/src/isSameSecond/test.ts b/date-fns/src/isSameSecond/test.ts
new file mode 100644
index 0000000..4a23595
--- /dev/null
+++ b/date-fns/src/isSameSecond/test.ts
@@ -0,0 +1,51 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import isSameSecond from '.'
+
+describe('isSameSecond', function() {
+ it('returns true if the given dates have the same second', function() {
+ const result = isSameSecond(
+ new Date(2014, 8 /* Sep */, 4, 6, 30, 15),
+ new Date(2014, 8 /* Sep */, 4, 6, 30, 15, 500)
+ )
+ assert(result === true)
+ })
+
+ it('returns false if the given dates have different seconds', function() {
+ const result = isSameSecond(
+ new Date(2014, 8 /* Sep */, 4, 6, 30, 58, 999),
+ new Date(2014, 8 /* Sep */, 4, 6, 30, 59)
+ )
+ assert(result === false)
+ })
+
+ it('accepts a timestamp', function() {
+ const result = isSameSecond(
+ new Date(2014, 8 /* Sep */, 4, 18, 45, 30).getTime(),
+ new Date(2014, 8 /* Sep */, 4, 18, 45, 30, 400).getTime()
+ )
+ assert(result === true)
+ })
+
+ it('returns false if the first date is `Invalid Date`', function() {
+ const result = isSameSecond(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 = isSameSecond(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 = isSameSecond(new Date(NaN), new Date(NaN))
+ assert(result === false)
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function() {
+ assert.throws(isSameSecond.bind(null), TypeError)
+ assert.throws(isSameSecond.bind(null, 1), TypeError)
+ })
+})