From e5ed4c1ef2d836ddfb7a3b1aff5c7ed475850af4 Mon Sep 17 00:00:00 2001 From: Thomas Step Date: Thu, 19 Mar 2020 14:48:02 -0500 Subject: [PATCH] Allowing offset from date-time-string. --- src/dateTime/index.js | 3 +-- src/dateTime/index.test.js | 13 ---------- src/dateTime/integration.test.js | 2 +- src/utils/__tests__/formatterTests.js | 12 --------- src/utils/formatter.js | 37 --------------------------- 5 files changed, 2 insertions(+), 65 deletions(-) diff --git a/src/dateTime/index.js b/src/dateTime/index.js index 38872b3..5f9c5d9 100644 --- a/src/dateTime/index.js +++ b/src/dateTime/index.js @@ -15,7 +15,6 @@ import { validateUnixTimestamp, validateJSDate, serializeDateTime, - serializeDateTimeString, serializeUnixTimestamp, parseDateTime } from '../utils' @@ -46,7 +45,7 @@ const config: GraphQLScalarTypeConfig = { throw new TypeError('DateTime cannot represent an invalid Date instance') } else if (typeof value === 'string' || value instanceof String) { if (validateDateTime(value)) { - return serializeDateTimeString(value) + return value } throw new TypeError( `DateTime cannot represent an invalid date-time-string ${value}.` diff --git a/src/dateTime/index.test.js b/src/dateTime/index.test.js index 148eadb..1e2dc0a 100644 --- a/src/dateTime/index.test.js +++ b/src/dateTime/index.test.js @@ -79,19 +79,6 @@ describe('GraphQLDateTime', () => { ).toThrowErrorMatchingSnapshot() }); - [ - [ '2016-02-01T00:00:15Z', '2016-02-01T00:00:15Z' ], - [ '2016-02-01T00:00:00.23498Z', '2016-02-01T00:00:00.23498Z' ], - [ '2016-02-01T00:00:00-11:00', '2016-02-01T11:00:00Z' ], - [ '2017-01-07T00:00:00.1+01:20', '2017-01-06T22:40:00.1Z' ] - ].forEach(([input, output]) => { - it(`serializes date-time-string ${input} into UTC date-time-string ${output}`, () => { - expect( - GraphQLDateTime.serialize(input) - ).toEqual(output) - }) - }) - invalidDates.forEach(dateString => { it(`throws an error when serializing an invalid date-string ${stringify(dateString)}`, () => { expect(() => diff --git a/src/dateTime/integration.test.js b/src/dateTime/integration.test.js index 4760ea6..81a2578 100644 --- a/src/dateTime/integration.test.js +++ b/src/dateTime/integration.test.js @@ -80,7 +80,7 @@ it('executes a query that includes a DateTime', async () => { data: { validDate: '2016-05-02T10:31:42.200Z', validUTCDateString: '1991-12-24T00:00:00Z', - validDateString: '2016-02-01T11:00:00Z', + validDateString: '2016-02-01T00:00:00-11:00', input: '2017-10-01T00:00:00.000Z', validUnixTimestamp: '1997-01-27T00:41:18.000Z', inputNull: null diff --git a/src/utils/__tests__/formatterTests.js b/src/utils/__tests__/formatterTests.js index b2dee1b..335f980 100644 --- a/src/utils/__tests__/formatterTests.js +++ b/src/utils/__tests__/formatterTests.js @@ -140,16 +140,4 @@ describe('formatting', () => { expect(parseDateTime(dateTime)).toEqual(date) }) }); - - [ - [ '2016-02-01T00:00:00Z', '2016-02-01T00:00:00Z' ], - [ '2016-02-01T12:23:44Z', '2016-02-01T12:23:44Z' ], - [ '2016-02-01T14:38:12-01:00', '2016-02-01T15:38:12Z' ], - [ '2016-02-02T00:00:00.4567+01:30', '2016-02-01T22:30:00.4567Z' ], - [ '2016-02-01T14:38:12.1+01:00', '2016-02-01T13:38:12.1Z' ] - ].forEach(([input, output]) => { - it(`serializes date-time-string ${input} into UTC date-time-string ${output}`, () => { - expect(serializeDateTimeString(input)).toEqual(output) - }) - }) }) diff --git a/src/utils/formatter.js b/src/utils/formatter.js index a9cce5c..c8712ff 100644 --- a/src/utils/formatter.js +++ b/src/utils/formatter.js @@ -93,43 +93,6 @@ export const serializeDateTime = (dateTime: Date): string => { return dateTime.toISOString() } -// Serializes an RFC 3339 compliant date-time-string by shifting -// it to UTC. -export const serializeDateTimeString = (dateTime: string): string => { - // If already formatted to UTC then return the time string - if (dateTime.indexOf('Z') !== -1) { - return dateTime - } else { - // These are time-strings with timezone information, - // these need to be shifted to UTC. - - // Convert to UTC time string in - // format YYYY-MM-DDThh:mm:ss.sssZ. - let dateTimeUTC = (new Date(dateTime)).toISOString() - - // Regex to look for fractional second part in date-time string - const regexFracSec = /\.\d{1,}/ - - // Retrieve the fractional second part of the time - // string if it exists. - const fractionalPart = dateTime.match(regexFracSec) - if (fractionalPart == null) { - // The date-time-string has no fractional part, - // so we remove it from the dateTimeUTC variable. - dateTimeUTC = dateTimeUTC.replace(regexFracSec, '') - return dateTimeUTC - } else { - // These are datetime-string with fractional seconds. - // Make sure that we inject the fractional - // second part back in. The `dateTimeUTC` variable - // has millisecond precision, we may want more or less - // depending on the string that was passed. - dateTimeUTC = dateTimeUTC.replace(regexFracSec, fractionalPart[0]) - return dateTimeUTC - } - } -} - // Serializes a Unix timestamp to an RFC 3339 compliant date-time-string // in the format YYYY-MM-DDThh:mm:ss.sssZ export const serializeUnixTimestamp = (timestamp: number): string => {