timepiece/node_modules/date-fns/parse/_lib/parsers/ISOTimezoneParser.mjs

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-05-14 14:54:12 +00:00
import { constructFrom } from "../../../constructFrom.mjs";
import { getTimezoneOffsetInMilliseconds } from "../../../_lib/getTimezoneOffsetInMilliseconds.mjs";
import { timezonePatterns } from "../constants.mjs";
import { Parser } from "../Parser.mjs";
import { parseTimezonePattern } from "../utils.mjs";
// Timezone (ISO-8601)
export class ISOTimezoneParser extends Parser {
priority = 10;
parse(dateString, token) {
switch (token) {
case "x":
return parseTimezonePattern(
timezonePatterns.basicOptionalMinutes,
dateString,
);
case "xx":
return parseTimezonePattern(timezonePatterns.basic, dateString);
case "xxxx":
return parseTimezonePattern(
timezonePatterns.basicOptionalSeconds,
dateString,
);
case "xxxxx":
return parseTimezonePattern(
timezonePatterns.extendedOptionalSeconds,
dateString,
);
case "xxx":
default:
return parseTimezonePattern(timezonePatterns.extended, dateString);
}
}
set(date, flags, value) {
if (flags.timestampIsSet) return date;
return constructFrom(
date,
date.getTime() - getTimezoneOffsetInMilliseconds(date) - value,
);
}
incompatibleTokens = ["t", "T", "X"];
}