31 lines
803 B
JavaScript
31 lines
803 B
JavaScript
import { numericPatterns } from "../constants.mjs";
|
|
import { Parser } from "../Parser.mjs";
|
|
import { parseNDigits, parseNumericPattern } from "../utils.mjs";
|
|
|
|
export class Hour1To24Parser extends Parser {
|
|
priority = 70;
|
|
|
|
parse(dateString, token, match) {
|
|
switch (token) {
|
|
case "k":
|
|
return parseNumericPattern(numericPatterns.hour24h, dateString);
|
|
case "ko":
|
|
return match.ordinalNumber(dateString, { unit: "hour" });
|
|
default:
|
|
return parseNDigits(token.length, dateString);
|
|
}
|
|
}
|
|
|
|
validate(_date, value) {
|
|
return value >= 1 && value <= 24;
|
|
}
|
|
|
|
set(date, _flags, value) {
|
|
const hours = value <= 24 ? value % 24 : value;
|
|
date.setHours(hours, 0, 0, 0);
|
|
return date;
|
|
}
|
|
|
|
incompatibleTokens = ["a", "b", "h", "H", "K", "t", "T"];
|
|
}
|