Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing offset from date-time-string. #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/dateTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
validateUnixTimestamp,
validateJSDate,
serializeDateTime,
serializeDateTimeString,
serializeUnixTimestamp,
parseDateTime
} from '../utils'
Expand Down Expand Up @@ -46,7 +45,7 @@ const config: GraphQLScalarTypeConfig<Date, string> = {
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}.`
Expand Down
13 changes: 0 additions & 13 deletions src/dateTime/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
2 changes: 1 addition & 1 deletion src/dateTime/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/utils/__tests__/formatterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
37 changes: 0 additions & 37 deletions src/utils/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down