summaryrefslogtreecommitdiff
path: root/date-fns/src/roundToNearestMinutes
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2021-08-23 16:46:06 -0300
committerSebastian <sebasjm@gmail.com>2021-08-23 16:48:30 -0300
commit38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch)
tree453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/roundToNearestMinutes
parentf26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff)
downloadnode-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip
added web vendorsHEADmaster
Diffstat (limited to 'date-fns/src/roundToNearestMinutes')
-rw-r--r--date-fns/src/roundToNearestMinutes/index.d.ts4
-rw-r--r--date-fns/src/roundToNearestMinutes/index.js61
-rw-r--r--date-fns/src/roundToNearestMinutes/index.js.flow57
-rw-r--r--date-fns/src/roundToNearestMinutes/test.js70
4 files changed, 192 insertions, 0 deletions
diff --git a/date-fns/src/roundToNearestMinutes/index.d.ts b/date-fns/src/roundToNearestMinutes/index.d.ts
new file mode 100644
index 0000000..8e56491
--- /dev/null
+++ b/date-fns/src/roundToNearestMinutes/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { roundToNearestMinutes } from 'date-fns'
+export default roundToNearestMinutes
diff --git a/date-fns/src/roundToNearestMinutes/index.js b/date-fns/src/roundToNearestMinutes/index.js
new file mode 100644
index 0000000..4a94c75
--- /dev/null
+++ b/date-fns/src/roundToNearestMinutes/index.js
@@ -0,0 +1,61 @@
+import toDate from '../toDate/index'
+import toInteger from '../_lib/toInteger/index'
+
+/**
+ * @name roundToNearestMinutes
+ * @category Minute Helpers
+ * @summary Rounds the given date to the nearest minute
+ *
+ * @description
+ * Rounds the given date to the nearest minute (or number of minutes).
+ * Rounds up when the given date is exactly between the nearest round minutes.
+ *
+ * ### 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} date - the date to round
+ * @param {Object} [options] - an object with options.
+ * @param {Number} [options.nearestTo=1] - nearest number of minutes to round to. E.g. `15` to round to quarter hours.
+ * @returns {Date} the new date rounded to the closest minute
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} `options.nearestTo` must be between 1 and 30
+ *
+ * @example
+ * // Round 10 July 2014 12:12:34 to nearest minute:
+ * var result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
+ * //=> Thu Jul 10 2014 12:13:00
+ *
+ * @example
+ * // Round 10 July 2014 12:07:30 to nearest quarter hour:
+ * var result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
+ * // rounds up because given date is exactly between 12:00:00 and 12:15:00
+ * //=> Thu Jul 10 2014 12:15:00
+ */
+export default function roundToNearestMinutes(dirtyDate, options) {
+ if (arguments.length < 1) {
+ throw new TypeError('1 argument required, but only none provided present')
+ }
+
+ var nearestTo =
+ options && 'nearestTo' in options ? toInteger(options.nearestTo) : 1
+
+ if (nearestTo < 1 || nearestTo > 30) {
+ throw new RangeError('`options.nearestTo` must be between 1 and 30')
+ }
+
+ var date = toDate(dirtyDate)
+ var seconds = date.getSeconds() // relevant if nearestTo is 1, which is the default case
+ var minutes = date.getMinutes() + seconds / 60
+ var roundedMinutes = Math.floor(minutes / nearestTo) * nearestTo
+ var remainderMinutes = minutes % nearestTo
+ var addedMinutes = Math.round(remainderMinutes / nearestTo) * nearestTo
+
+ return new Date(
+ date.getFullYear(),
+ date.getMonth(),
+ date.getDate(),
+ date.getHours(),
+ roundedMinutes + addedMinutes
+ )
+}
diff --git a/date-fns/src/roundToNearestMinutes/index.js.flow b/date-fns/src/roundToNearestMinutes/index.js.flow
new file mode 100644
index 0000000..8e6f0ec
--- /dev/null
+++ b/date-fns/src/roundToNearestMinutes/index.js.flow
@@ -0,0 +1,57 @@
+// @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: (
+ date: Date | number,
+ options?: {
+ nearestTo?: number,
+ }
+) => Date
diff --git a/date-fns/src/roundToNearestMinutes/test.js b/date-fns/src/roundToNearestMinutes/test.js
new file mode 100644
index 0000000..e4f7c04
--- /dev/null
+++ b/date-fns/src/roundToNearestMinutes/test.js
@@ -0,0 +1,70 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import roundToNearestMinutes from '.'
+
+describe('roundToNearestMinutes', function() {
+ it('rounds given date to the nearest minute by default', function() {
+ var result = roundToNearestMinutes(
+ new Date(2014, 6 /* Jul */, 10, 12, 16, 16)
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 16, 0))
+ })
+
+ it('accepts a timestamp', function() {
+ var result = roundToNearestMinutes(
+ new Date(2014, 6 /* Jul */, 10, 12, 13, 16).getTime()
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0))
+ })
+
+ it('rounds to the closest x minutes if nearestTo is provided', function() {
+ var result = roundToNearestMinutes(
+ new Date(2014, 6 /* Jul */, 10, 12, 10, 30),
+ { nearestTo: 4 }
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 12, 0))
+ })
+
+ it('rounds up >=30 seconds for nearestTo=1', function() {
+ var result = roundToNearestMinutes(
+ new Date(2014, 6 /* Jul */, 10, 12, 13, 30)
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 14, 0))
+ })
+
+ it('rounds down <30 seconds for nearestTo=1', function() {
+ var result = roundToNearestMinutes(
+ new Date(2014, 6 /* Jul */, 10, 12, 13, 29, 999)
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0))
+ })
+
+ it('does not mutate the original date', function() {
+ var date = new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99)
+ roundToNearestMinutes(date)
+ assert.deepEqual(date, new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ var result = roundToNearestMinutes(new Date(NaN))
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws `TypeError` exception if passed less than 1 argument', function() {
+ assert.throws(roundToNearestMinutes.bind(null), TypeError)
+ })
+
+ it('throws `RangeError` if nearestTo is not between 1 and 30', function() {
+ var date = new Date(2014, 6 /* Jul */, 10, 12, 10, 30)
+ assert.throws(
+ roundToNearestMinutes.bind(null, date, { nearestTo: 31 }),
+ RangeError
+ )
+ assert.throws(
+ roundToNearestMinutes.bind(null, date, { nearestTo: 0 }),
+ RangeError
+ )
+ })
+})