Skip to content

Commit

Permalink
- removed regex pattern to check iana string
Browse files Browse the repository at this point in the history
- switched pattern-compare timezone to intl-compare timezone
  • Loading branch information
X7Becka committed Jul 25, 2021
1 parent dd383f7 commit 3a72f7e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/_lib/tzParseTimezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var patterns = {
timezone: /([Z+-].*)$/,
timezoneZ: /^(Z)$/,
timezoneHH: /^([+-])(\d{2})$/,
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/,
timezoneIANA: /(UTC|(?:[a-zA-Z]+\/[a-zA-Z_-]+(?:\/[a-zA-Z_]+)?))$/,
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/
}

// Parse various time zone offset formats to an offset in milliseconds
Expand Down Expand Up @@ -52,8 +51,7 @@ export default function tzParseTimezone(timezoneString, date, isUtcDate) {
}

// IANA time zone
token = patterns.timezoneIANA.exec(timezoneString)
if (token) {
if (isValidTimezoneIANAString(timezoneString)) {
date = new Date(date || Date.now())
var utcDate = isUtcDate ? date : toUtcDate(date)

Expand Down Expand Up @@ -126,3 +124,13 @@ function validateTimezone(hours, minutes) {

return true
}

function isValidTimezoneIANAString(timeZoneString) {
try {
Intl.DateTimeFormat(undefined, {timeZone: timeZoneString});
return true;
}
catch {
return false;
}
}

0 comments on commit 3a72f7e

Please sign in to comment.