Skip to content

Commit

Permalink
Handle invalid input correctly
Browse files Browse the repository at this point in the history
Functions that return dates will return `Invalid Date` for bad date
or time zone inputs, and `format` functions throw a `RangeError`.
  • Loading branch information
marnusw committed Dec 20, 2021
1 parent c110933 commit e51e3b6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/_lib/tzIntlTimeZoneName/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ describe('tzIntlTimeZoneName', function () {
var result = tzIntlTimeZoneName('long', date, { timeZone, locale })
assert.equal(result, 'Central European Summer Time')
})

it('an invalid time zone throws a range error', function () {
var locale = { code: 'en-GB' }
var date = new Date('2014-10-25T13:46:20Z')
var timeZone = 'bad/timeZone'
assert.throws(tzIntlTimeZoneName.bind(null, 'long', date, { timeZone, locale }), RangeError)
})
})
10 changes: 10 additions & 0 deletions src/format/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,16 @@ describe('format', function () {
assert.throws(format.bind(null, new Date(NaN), 'MMMM d, yyyy'), RangeError)
})

it('throws RangeError if the time zone is invalid and included in the output', () => {
var result = format(new Date(), 'MMMM d, yyyy', { timeZone: 'bad/timeZone' })
assert.equal(result, 'December 20, 2021')
try {
format(new Date(), 'MMMM d, yyyy zzz', { timeZone: 'bad/timeZone' })
} catch (error) {
assert.deepEqual(error, new Error('Invalid time zone specified: bad/timeZone'))
}
})

it('handles dates before 100 AD', function () {
var initialDate = new Date(0)
initialDate.setFullYear(7, 11 /* Dec */, 31)
Expand Down
13 changes: 12 additions & 1 deletion src/formatAsZonedTime/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ describe('formatAsZonedTime', function () {
var date = '1986-04-04T10:32:55.123Z'
var timeZone = 'Europe/Paris'
var result = formatAsZonedTime(date, timeZone, "dd.MM.yyyy HH:mm 'UTC'xxx")

assert(result === '04.04.1986 12:32 UTC+02:00')
})

it('throws a RangeError on invalid time zones', function () {
var date = '1986-04-04T10:32:55.123Z'
assert.throws(
formatAsZonedTime.bind(null, date, '02:65', "dd.MM.yyyy HH:mm 'UTC'xxx"),
RangeError
)
assert.throws(
formatAsZonedTime.bind(null, date, 'bad/timezone', "dd.MM.yyyy HH:mm 'UTC'xxx"),
RangeError
)
})
})
6 changes: 6 additions & 0 deletions src/toDate/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ describe('toDate', function () {
})

describe('invalid argument', function () {
it('returns Invalid Date if an invalid time zone is provided', function () {
var result = toDate('2020-01-01T12:00:00.000', { timeZone: 'bad/timeZone' })
assert(result instanceof Date)
assert(isNaN(result))
})

it('returns Invalid Date if argument is non-date string', function () {
var result = toDate('abc')
assert(result instanceof Date)
Expand Down
6 changes: 2 additions & 4 deletions src/utcToZonedTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import toDate from '../toDate'
export default function utcToZonedTime(dirtyDate, timeZone, options) {
var date = toDate(dirtyDate, options)

var offsetMilliseconds = tzParseTimezone(timeZone, date, true) || 0
var offsetMilliseconds = tzParseTimezone(timeZone, date, true)

var d = new Date(date.getTime() - offsetMilliseconds)

var zonedTime = new Date(
return new Date(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
Expand All @@ -41,6 +41,4 @@ export default function utcToZonedTime(dirtyDate, timeZone, options) {
d.getUTCSeconds(),
d.getUTCMilliseconds()
)

return zonedTime
}
6 changes: 6 additions & 0 deletions src/utcToZonedTime/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ describe('utcToZonedTime', function () {
var result = utcToZonedTime('2020-03-08T19:00:00.000Z', timeZone)
assert.equal(format(result, 'yyyy-MM-dd hh:mm:ss.SSS'), '2020-03-08 12:00:00.000')
})

it('returns an invalid date when the time zone is invalid', function () {
var result = utcToZonedTime('2020-03-08T19:00:00.000Z', 'bad/timeZone')
assert(result instanceof Date)
assert(isNaN(result))
})
})
2 changes: 1 addition & 1 deletion src/zonedTimeToUtc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function zonedTimeToUtc(date, timeZone, options) {
d.getMilliseconds()
)

var offsetMilliseconds = tzParseTimezone(timeZone, new Date(utc)) || 0
var offsetMilliseconds = tzParseTimezone(timeZone, new Date(utc))

return new Date(utc + offsetMilliseconds)
}
8 changes: 8 additions & 0 deletions src/zonedTimeToUtc/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,12 @@ describe('zonedTimeToUtc', function () {
assert.deepEqual(result, new Date('2019-11-26T10:00:00Z'))
})
})

describe('invalid time zone handling', function () {
it('returns an invalid date when the time zone is invalid', function () {
var result = zonedTimeToUtc('2020-01-01T12:00:00.000Z', 'bad/timezone')
assert(result instanceof Date)
assert(isNaN(result))
})
})
})

0 comments on commit e51e3b6

Please sign in to comment.