summaryrefslogtreecommitdiff
path: root/date-fns/src/milliseconds
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/milliseconds')
-rw-r--r--date-fns/src/milliseconds/index.d.ts4
-rw-r--r--date-fns/src/milliseconds/index.js.flow52
-rw-r--r--date-fns/src/milliseconds/index.ts62
-rw-r--r--date-fns/src/milliseconds/test.ts65
4 files changed, 183 insertions, 0 deletions
diff --git a/date-fns/src/milliseconds/index.d.ts b/date-fns/src/milliseconds/index.d.ts
new file mode 100644
index 0000000..6927b52
--- /dev/null
+++ b/date-fns/src/milliseconds/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { milliseconds } from 'date-fns'
+export default milliseconds
diff --git a/date-fns/src/milliseconds/index.js.flow b/date-fns/src/milliseconds/index.js.flow
new file mode 100644
index 0000000..5ca1b36
--- /dev/null
+++ b/date-fns/src/milliseconds/index.js.flow
@@ -0,0 +1,52 @@
+// @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: (duration: Duration) => number
diff --git a/date-fns/src/milliseconds/index.ts b/date-fns/src/milliseconds/index.ts
new file mode 100644
index 0000000..6519229
--- /dev/null
+++ b/date-fns/src/milliseconds/index.ts
@@ -0,0 +1,62 @@
+import requiredArgs from '../_lib/requiredArgs/index'
+import { Duration } from '../types'
+
+// Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
+// 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
+const daysInYear = 365.2425
+
+/**
+ * @name milliseconds
+ * @category Millisecond Helpers
+ * @summary
+ * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
+ *
+ * @description
+ * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
+ *
+ * One years equals 365.2425 days according to the formula:
+ *
+ * > Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
+ * > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
+ *
+ * One month is a year divided by 12.
+ *
+ * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
+ * @returns {number} the milliseconds
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // 1 year in milliseconds
+ * milliseconds({ years: 1 })
+ * //=> 31556952000
+ *
+ * // 3 months in milliseconds
+ * milliseconds({ months: 3 })
+ * //=> 7889238000
+ */
+export default function milliseconds({
+ years,
+ months,
+ weeks,
+ days,
+ hours,
+ minutes,
+ seconds,
+}: Duration): number {
+ requiredArgs(1, arguments)
+
+ let totalDays = 0
+
+ if (years) totalDays += years * daysInYear
+ if (months) totalDays += months * (daysInYear / 12)
+ if (weeks) totalDays += weeks * 7
+ if (days) totalDays += days
+
+ let totalSeconds = totalDays * 24 * 60 * 60
+
+ if (hours) totalSeconds += hours * 60 * 60
+ if (minutes) totalSeconds += minutes * 60
+ if (seconds) totalSeconds += seconds
+
+ return Math.round(totalSeconds * 1000)
+}
diff --git a/date-fns/src/milliseconds/test.ts b/date-fns/src/milliseconds/test.ts
new file mode 100644
index 0000000..e576c23
--- /dev/null
+++ b/date-fns/src/milliseconds/test.ts
@@ -0,0 +1,65 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import milliseconds from '.'
+
+describe('milliseconds', () => {
+ it('converts years to milliseconds', () => {
+ const result = milliseconds({ years: 2 })
+ assert(result === 63113904000)
+ })
+
+ it('converts months to milliseconds', () => {
+ const result = milliseconds({ months: 3 })
+ assert(result === 7889238000)
+ })
+
+ it('converts weeks to milliseconds', () => {
+ const result = milliseconds({ weeks: 2 })
+ assert(result === 1209600000)
+ })
+
+ it('converts days to milliseconds', () => {
+ const result = milliseconds({ days: 5 })
+ assert(result === 432000000)
+ })
+
+ it('converts hours to milliseconds', () => {
+ const result = milliseconds({ hours: 2 })
+ assert(result === 7200000)
+ })
+
+ it('converts minutes to milliseconds', () => {
+ const result = milliseconds({ minutes: 5 })
+ assert(result === 300000)
+ })
+
+ it('converts seconds to milliseconds', () => {
+ const result = milliseconds({ seconds: 10 })
+ assert(result === 10000)
+ })
+
+ it('sums all the duration values', () => {
+ const result = milliseconds({
+ years: 2,
+ months: 3,
+ weeks: 2,
+ days: 5,
+ hours: 2,
+ minutes: 5,
+ seconds: 10
+ })
+ assert(result === 72652252000)
+ })
+
+ it('returns 0 for an empty duration', () => {
+ const result = milliseconds({})
+ assert(result === 0)
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function() {
+ // @ts-expect-error
+ assert.throws(milliseconds.bind(null), TypeError)
+ })
+})