summaryrefslogtreecommitdiff
path: root/date-fns/src/parseJSON
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/parseJSON')
-rw-r--r--date-fns/src/parseJSON/benchmark.js16
-rw-r--r--date-fns/src/parseJSON/index.d.ts4
-rw-r--r--date-fns/src/parseJSON/index.js.flow52
-rw-r--r--date-fns/src/parseJSON/index.ts62
-rw-r--r--date-fns/src/parseJSON/test.ts132
5 files changed, 266 insertions, 0 deletions
diff --git a/date-fns/src/parseJSON/benchmark.js b/date-fns/src/parseJSON/benchmark.js
new file mode 100644
index 0000000..f6b1c0b
--- /dev/null
+++ b/date-fns/src/parseJSON/benchmark.js
@@ -0,0 +1,16 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import parseJSON from '.'
+import moment from 'moment'
+
+suite('toDate', function() {
+ benchmark('date-fns', function() {
+ return parseJSON('2014-10-25T13:46:20+00:00')
+ })
+
+ benchmark('Moment.js', function() {
+ return moment('2014-10-25T13:46:20+00:00')
+ })
+})
diff --git a/date-fns/src/parseJSON/index.d.ts b/date-fns/src/parseJSON/index.d.ts
new file mode 100644
index 0000000..b6b186d
--- /dev/null
+++ b/date-fns/src/parseJSON/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { parseJSON } from 'date-fns'
+export default parseJSON
diff --git a/date-fns/src/parseJSON/index.js.flow b/date-fns/src/parseJSON/index.js.flow
new file mode 100644
index 0000000..c4801bf
--- /dev/null
+++ b/date-fns/src/parseJSON/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: (argument: string | number | Date) => Date
diff --git a/date-fns/src/parseJSON/index.ts b/date-fns/src/parseJSON/index.ts
new file mode 100644
index 0000000..6af1eda
--- /dev/null
+++ b/date-fns/src/parseJSON/index.ts
@@ -0,0 +1,62 @@
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name parseJSON
+ * @category Common Helpers
+ * @summary Parse a JSON date string
+ *
+ * @description
+ * Converts a complete ISO date string in UTC time, the typical format for transmitting
+ * a date in JSON, to a JavaScript `Date` instance.
+ *
+ * This is a minimal implementation for converting dates retrieved from a JSON API to
+ * a `Date` instance which can be used with other functions in the `date-fns` library.
+ * The following formats are supported:
+ *
+ * - `2000-03-15T05:20:10.123Z`: The output of `.toISOString()` and `JSON.stringify(new Date())`
+ * - `2000-03-15T05:20:10Z`: Without milliseconds
+ * - `2000-03-15T05:20:10+00:00`: With a zero offset, the default JSON encoded format in some other languages
+ * - `2000-03-15T05:20:10+05:45`: With a positive or negative offset, the default JSON encoded format in some other languages
+ * - `2000-03-15T05:20:10+0000`: With a zero offset without a colon
+ * - `2000-03-15T05:20:10`: Without a trailing 'Z' symbol
+ * - `2000-03-15T05:20:10.1234567`: Up to 7 digits in milliseconds field. Only first 3 are taken into account since JS does not allow fractional milliseconds
+ * - `2000-03-15 05:20:10`: With a space instead of a 'T' separator for APIs returning a SQL date without reformatting
+ *
+ * For convenience and ease of use these other input types are also supported
+ * via [toDate]{@link https://date-fns.org/docs/toDate}:
+ *
+ * - A `Date` instance will be cloned
+ * - A `number` will be treated as a timestamp
+ *
+ * Any other input type or invalid date strings will return an `Invalid Date`.
+ *
+ * @param {String|Number|Date} argument A fully formed ISO8601 date string to convert
+ * @returns {Date} the parsed date in the local time zone
+ * @throws {TypeError} 1 argument required
+ */
+export default function parseJSON(argument: string): Date {
+ requiredArgs(1, arguments)
+
+ if (typeof argument === 'string') {
+ const parts = argument.match(
+ /(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{0,7}))?(?:Z|(.)(\d{2}):?(\d{2})?)?/
+ )
+ if (parts) {
+ // Group 8 matches the sign
+ return new Date(
+ Date.UTC(
+ +parts[1],
+ +parts[2] - 1,
+ +parts[3],
+ +parts[4] - (+parts[9] || 0) * (parts[8] == '-' ? -1 : 1),
+ +parts[5] - (+parts[10] || 0) * (parts[8] == '-' ? -1 : 1),
+ +parts[6],
+ +((parts[7] || '0') + '00').substring(0, 3)
+ )
+ )
+ }
+ return new Date(NaN)
+ }
+ return toDate(argument)
+}
diff --git a/date-fns/src/parseJSON/test.ts b/date-fns/src/parseJSON/test.ts
new file mode 100644
index 0000000..ffb348f
--- /dev/null
+++ b/date-fns/src/parseJSON/test.ts
@@ -0,0 +1,132 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import parseJSON from '.'
+import format from '../format/index'
+
+describe('parseJSON', function () {
+ it('parses a formatted new Date() back to UTC - issue 2149', () => {
+ const date = new Date()
+ const jsonFormat = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
+ const parsedDate = parseJSON(jsonFormat)
+ assert.equal(parsedDate.toISOString(), date.toISOString())
+ })
+
+ it('parses a formatted date with an hour of offset back to UTC - issue 2149', () => {
+ const date = '2021-01-09T13:18:10.873+01:00'
+ const expectedDate = new Date('2021-01-09T12:18:10.873Z')
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate.toISOString())
+ })
+
+ it('parses a formatted date with 2 hours of offset back to UTC - issue 2149', () => {
+ const date = '2021-01-09T13:18:10.873+02:00'
+ const expectedDate = new Date('2021-01-09T11:18:10.873Z')
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate.toISOString())
+ })
+
+ it('parses a formatted date with -2 hours of offset back to UTC - issue 2149', () => {
+ const date = '2021-01-09T13:18:10.873-02:00'
+ const expectedDate = new Date('2021-01-09T15:18:10.873Z')
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate.toISOString())
+ })
+
+ it('parses a formatted Indian Standart Time in Asia/Kolkata with +5:30 hours of offset back to UTC - issue 2149', () => {
+ const date = '2021-02-15T02:56:04.678+05:30'
+ const expectedDate = new Date('2021-02-14T21:26:04.678Z')
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate.toISOString())
+ })
+
+ it('parses a formatted time in Asia/Kathmandu with +5:45 hours of offset back to UTC - issue 2149', () => {
+ const date = '2021-02-15T17:45:00.900+05:45'
+ const expectedDate = new Date('2021-02-15T12:00:00.900Z')
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate.toISOString())
+ })
+
+ it('parses a fully formed ISO date with Z', () => {
+ const date = '2000-03-15T05:20:10.123Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), date)
+ })
+
+ it('parses a fully formed ISO date with Z without ms', () => {
+ const date = '2000-03-15T05:20:10Z'
+ const expectedDate = '2000-03-15T05:20:10.000Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO date with zero offset', () => {
+ const zeroOffset = '2000-03-15T05:20:10+00:00'
+ const expectedDate = '2000-03-15T05:20:10.000Z'
+ const parsedDate = parseJSON(zeroOffset)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO date with zero offset without colon', () => {
+ const zeroOffset = '2000-03-15T05:20:10+0000'
+ const expectedDate = '2000-03-15T05:20:10.000Z'
+ const parsedDate = parseJSON(zeroOffset)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO date without Z', () => {
+ const date = '2000-03-15T05:20:10.123'
+ const expectedDate = '2000-03-15T05:20:10.123Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO date without Z and with 6-digit millisecond part', () => {
+ const date = '2000-03-15T05:20:10.123456'
+ const expectedDate = '2000-03-15T05:20:10.123Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO with 1-digit millisecond part', () => {
+ const date = '2000-03-15T05:20:10.1Z'
+ const expectedDate = '2000-03-15T05:20:10.100Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses a fully formed ISO with 2-digit millisecond part', () => {
+ const date = '2000-03-15T05:20:10.12Z'
+ const expectedDate = '2000-03-15T05:20:10.120Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses supported formats with a space time separator instead of a T', () => {
+ const date = '2000-03-15 05:20:10.123Z'
+ const expectedDate = '2000-03-15T05:20:10.123Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses the SQL datetime format without milliseconds', () => {
+ const date = '2000-03-15 05:20:10'
+ const expectedDate = '2000-03-15T05:20:10.000Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('parses the SQL datetime format with up to 7 millisecond digits', () => {
+ const date = '2000-03-15 05:20:10.1234567'
+ const expectedDate = '2000-03-15T05:20:10.123Z'
+ const parsedDate = parseJSON(date)
+ assert.equal(parsedDate.toISOString(), expectedDate)
+ })
+
+ it('returns an invalid date for anything else', () => {
+ assert.equal(parseJSON('').toString(), 'Invalid Date')
+ assert.equal(parseJSON('invalid').toString(), 'Invalid Date')
+ assert.equal(parseJSON('2020-10-10').toString(), 'Invalid Date')
+ })
+})