Skip to content

Commit

Permalink
Accept positive and negative offsets in parseJSON (#2200)
Browse files Browse the repository at this point in the history
Added support of positive and negative offsets in `parseJSON` (#2149).
  • Loading branch information
tan75 committed Feb 22, 2021
1 parent dae8a76 commit 8f93b18
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/parseJSON/index.js
Expand Up @@ -17,6 +17,7 @@ import requiredArgs from '../_lib/requiredArgs/index'
* - `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
Expand All @@ -36,19 +37,19 @@ import requiredArgs from '../_lib/requiredArgs/index'
*/
export default function parseJSON(argument) {
requiredArgs(1, arguments)

if (typeof argument === 'string') {
var parts = argument.match(
/(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{0,7}))?(?:Z|\+00:?00)?/
/(\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[5],
+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)
)
Expand Down
45 changes: 44 additions & 1 deletion src/parseJSON/test.js
Expand Up @@ -3,8 +3,51 @@

import assert from 'power-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())
})

describe('parseJSON', function() {
it('parses a fully formed ISO date with Z', () => {
const date = '2000-03-15T05:20:10.123Z'
const parsedDate = parseJSON(date)
Expand Down

0 comments on commit 8f93b18

Please sign in to comment.