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

37 lines
947 B
JavaScript
Raw Normal View History

2024-05-14 14:54:12 +00:00
import { numericPatterns } from "../constants.mjs";
import { Parser } from "../Parser.mjs";
import { parseNDigits, parseNumericPattern } from "../utils.mjs";
export class Hour1to12Parser extends Parser {
priority = 70;
parse(dateString, token, match) {
switch (token) {
case "h":
return parseNumericPattern(numericPatterns.hour12h, dateString);
case "ho":
return match.ordinalNumber(dateString, { unit: "hour" });
default:
return parseNDigits(token.length, dateString);
}
}
validate(_date, value) {
return value >= 1 && value <= 12;
}
set(date, _flags, value) {
const isPM = date.getHours() >= 12;
if (isPM && value < 12) {
date.setHours(value + 12, 0, 0, 0);
} else if (!isPM && value === 12) {
date.setHours(0, 0, 0, 0);
} else {
date.setHours(value, 0, 0, 0);
}
return date;
}
incompatibleTokens = ["H", "K", "k", "t", "T"];
}