Skip to content

Commit c81904c

Browse files
authored
Merge pull request #4 from mxdvl/simplify-get-timezone-offset
Refactor `getTimezoneOffset` with explicit defaults
2 parents ee0bf81 + a8a2d5b commit c81904c

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

parse.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
// Times are parsed as UTC if no offset is specified
22
export class IsoDateParts {
3-
static DEFAULT_TIMEZONE_OFFSET = {
4-
hours: 0,
5-
minutes: 0,
6-
};
7-
83
static FULL_DATE_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)$/;
94
static DATETIME_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)[Tt ]([0-2]\d(?:[\.\,]\d+)?)(?::?([0-5]\d(?:[\.\,]\d+)?)(?::?([0-5]\d))?(?:[\.\,](\d{1,9}))?)?(Z|[+-][0-2]\d(?::?[0-5]\d)?)?$/;
105
static TIMEZONE_REGEX = /^([+-]\d{2})(?::?(\d{2}))?$/;
116
static IS_FRACTIONAL_REGEX = /^\d+[\.\,]\d+$/;
127

13-
static getTimezoneOffset(offset = "") {
14-
if(offset === "Z") {
15-
return this.DEFAULT_TIMEZONE_OFFSET;
16-
}
17-
let match = offset.match(this.TIMEZONE_REGEX);
18-
if(!match) {
19-
return this.DEFAULT_TIMEZONE_OFFSET;
20-
}
8+
static getTimezoneOffset(offset = "Z") {
9+
let [, hours = "0", minutes = "0"] = offset.match(this.TIMEZONE_REGEX) ?? [];
2110

22-
let [hours, minutes] = [match[1], match[2]];
23-
let sign = hours[0] === '-' ? -1 : 1;
11+
let sign = hours[0] === '-' ? -1 : 1;
2412
return {
25-
hours: parseInt(hours, 10) || 0,
26-
minutes: (parseInt(minutes, 10) || 0) * sign
13+
hours: parseInt(hours, 10),
14+
minutes: parseInt(minutes, 10) * sign
2715
};
2816
}
2917

0 commit comments

Comments
 (0)