Skip to content

Commit

Permalink
Fixed format not throwing RangeError for invalid time zones with offs…
Browse files Browse the repository at this point in the history
…et tokens, closes #152
  • Loading branch information
marnusw committed Feb 24, 2022
1 parent 82724f4 commit 2cad862
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [DOCS] Clarify when `format` throws a `RangeError` and fix the test for it
- [ENHANCEMENT] More extensive validation of numeric time zone offsets
- [BUGFIX] Fixed `zonedTimeToUtc` throwing `RangeError` instead of returning an Invalid Date (#151)
- [BUGFIX] Fixed `format` not throwing `RangeError` for invalid time zones with offset tokens (#152)

### v1.2.2 (21 December 2021)

Expand Down
25 changes: 13 additions & 12 deletions src/format/formatters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ var MILLISECONDS_IN_MINUTE = 60 * 1000
var formatters = {
// Timezone (ISO-8601. If offset is 0, output is always `'Z'`)
X: function (date, token, localize, options) {
var originalDate = options._originalDate || date
var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)

if (timezoneOffset === 0) {
return 'Z'
Expand Down Expand Up @@ -39,10 +36,7 @@ var formatters = {

// Timezone (ISO-8601. If offset is 0, output is `'+00:00'` or equivalent)
x: function (date, token, localize, options) {
var originalDate = options._originalDate || date
var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)

switch (token) {
// Hours and optional minutes
Expand All @@ -68,10 +62,7 @@ var formatters = {

// Timezone (GMT)
O: function (date, token, localize, options) {
var originalDate = options._originalDate || date
var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)

switch (token) {
// Short
Expand Down Expand Up @@ -104,6 +95,16 @@ var formatters = {
},
}

function getTimeZoneOffset(timeZone, originalDate) {
var timeZoneOffset = timeZone
? tzParseTimezone(timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()
if (Number.isNaN(timeZoneOffset)) {
throw new RangeError('Invalid time zone specified: ' + timeZone)
}
return timeZoneOffset
}

function addLeadingZeros(number, targetLength) {
var sign = number < 0 ? '-' : ''
var output = Math.abs(number).toString()
Expand Down
11 changes: 10 additions & 1 deletion src/format/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ 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', () => {
it('throws RangeError if the time zone is invalid and a name included in the output', () => {
var result = format(new Date(2021, 11, 20), 'MMMM d, yyyy', { timeZone: 'bad/timeZone' })
assert.equal(result, 'December 20, 2021')
assert.throws(
Expand All @@ -779,6 +779,15 @@ describe('format', function () {
)
})

it('throws RangeError if the time zone is invalid and an offset included in the output', () => {
var result = format(new Date(2021, 11, 20), 'xxxxx', { timeZone: 'Europe/London' })
assert.equal(result, '+00:00')
assert.throws(
() => format(new Date(2021, 11, 20), 'xxxxx', { timeZone: 'X/Y' }),
/RangeError: Invalid time zone specified: X\/Y$/
)
})

it('handles dates before 100 AD', function () {
var initialDate = new Date(0)
initialDate.setFullYear(7, 11 /* Dec */, 31)
Expand Down

0 comments on commit 2cad862

Please sign in to comment.