summaryrefslogtreecommitdiff
path: root/date-fns/src/formatRFC7231
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/formatRFC7231')
-rw-r--r--date-fns/src/formatRFC7231/index.d.ts4
-rw-r--r--date-fns/src/formatRFC7231/index.js64
-rw-r--r--date-fns/src/formatRFC7231/index.js.flow52
-rw-r--r--date-fns/src/formatRFC7231/test.js25
4 files changed, 145 insertions, 0 deletions
diff --git a/date-fns/src/formatRFC7231/index.d.ts b/date-fns/src/formatRFC7231/index.d.ts
new file mode 100644
index 0000000..2caa714
--- /dev/null
+++ b/date-fns/src/formatRFC7231/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { formatRFC7231 } from 'date-fns'
+export default formatRFC7231
diff --git a/date-fns/src/formatRFC7231/index.js b/date-fns/src/formatRFC7231/index.js
new file mode 100644
index 0000000..3554e35
--- /dev/null
+++ b/date-fns/src/formatRFC7231/index.js
@@ -0,0 +1,64 @@
+import toDate from '../toDate/index'
+import isValid from '../isValid/index'
+import addLeadingZeros from '../_lib/addLeadingZeros/index'
+
+const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
+const months = [
+ 'Jan',
+ 'Feb',
+ 'Mar',
+ 'Apr',
+ 'May',
+ 'Jun',
+ 'Jul',
+ 'Aug',
+ 'Sep',
+ 'Oct',
+ 'Nov',
+ 'Dec'
+]
+
+/**
+ * @name formatRFC7231
+ * @category Common Helpers
+ * @summary Format the date according to the RFC 7231 standard (https://tools.ietf.org/html/rfc7231#section-7.1.1.1).
+ *
+ * @description
+ * Return the formatted date string in RFC 7231 format.
+ * The result will always be in UTC timezone.
+ *
+ * @param {Date|Number} date - the original date
+ * @returns {String} the formatted date string
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} `date` must not be Invalid Date
+ *
+ * @example
+ * // Represent 18 September 2019 in RFC 7231 format:
+ * const result = formatRFC7231(new Date(2019, 8, 18, 19, 0, 52))
+ * //=> 'Wed, 18 Sep 2019 19:00:52 GMT'
+ */
+export default function formatRFC7231(dirtyDate) {
+ if (arguments.length < 1) {
+ throw new TypeError(
+ `1 arguments required, but only ${arguments.length} present`
+ )
+ }
+
+ const originalDate = toDate(dirtyDate)
+
+ if (!isValid(originalDate)) {
+ throw new RangeError('Invalid time value')
+ }
+
+ const dayName = days[originalDate.getUTCDay()]
+ const dayOfMonth = addLeadingZeros(originalDate.getUTCDate(), 2)
+ const monthName = months[originalDate.getUTCMonth()]
+ const year = originalDate.getUTCFullYear()
+
+ const hour = addLeadingZeros(originalDate.getUTCHours(), 2)
+ const minute = addLeadingZeros(originalDate.getUTCMinutes(), 2)
+ const second = addLeadingZeros(originalDate.getUTCSeconds(), 2)
+
+ // Result variables.
+ return `${dayName}, ${dayOfMonth} ${monthName} ${year} ${hour}:${minute}:${second} GMT`
+}
diff --git a/date-fns/src/formatRFC7231/index.js.flow b/date-fns/src/formatRFC7231/index.js.flow
new file mode 100644
index 0000000..8462b25
--- /dev/null
+++ b/date-fns/src/formatRFC7231/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: (date: Date | number) => string
diff --git a/date-fns/src/formatRFC7231/test.js b/date-fns/src/formatRFC7231/test.js
new file mode 100644
index 0000000..018bbc5
--- /dev/null
+++ b/date-fns/src/formatRFC7231/test.js
@@ -0,0 +1,25 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import formatRFC7231 from '.'
+
+describe('formatRFC7231', () => {
+ it('formats RFC-7231 date string', () => {
+ const date = new Date(Date.UTC(2019, 2, 3, 19, 0, 52))
+ assert(formatRFC7231(date) === 'Sun, 03 Mar 2019 19:00:52 GMT')
+ })
+
+ it('accepts a timestamp', () => {
+ const date = Date.UTC(2019, 9, 4, 12, 30, 13)
+ assert(formatRFC7231(date) === 'Fri, 04 Oct 2019 12:30:13 GMT')
+ })
+
+ it('throws RangeError if the time value is invalid', () => {
+ assert.throws(formatRFC7231.bind(null, new Date(NaN)), RangeError)
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function() {
+ assert.throws(formatRFC7231.bind(null), TypeError)
+ })
+})