60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
|
import { numericPatterns } from "../constants.mjs";
|
||
|
import { Parser } from "../Parser.mjs";
|
||
|
import {
|
||
|
isLeapYearIndex,
|
||
|
parseNDigits,
|
||
|
parseNumericPattern,
|
||
|
} from "../utils.mjs";
|
||
|
|
||
|
export class DayOfYearParser extends Parser {
|
||
|
priority = 90;
|
||
|
|
||
|
subpriority = 1;
|
||
|
|
||
|
parse(dateString, token, match) {
|
||
|
switch (token) {
|
||
|
case "D":
|
||
|
case "DD":
|
||
|
return parseNumericPattern(numericPatterns.dayOfYear, dateString);
|
||
|
case "Do":
|
||
|
return match.ordinalNumber(dateString, { unit: "date" });
|
||
|
default:
|
||
|
return parseNDigits(token.length, dateString);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
validate(date, value) {
|
||
|
const year = date.getFullYear();
|
||
|
const isLeapYear = isLeapYearIndex(year);
|
||
|
if (isLeapYear) {
|
||
|
return value >= 1 && value <= 366;
|
||
|
} else {
|
||
|
return value >= 1 && value <= 365;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
set(date, _flags, value) {
|
||
|
date.setMonth(0, value);
|
||
|
date.setHours(0, 0, 0, 0);
|
||
|
return date;
|
||
|
}
|
||
|
|
||
|
incompatibleTokens = [
|
||
|
"Y",
|
||
|
"R",
|
||
|
"q",
|
||
|
"Q",
|
||
|
"M",
|
||
|
"L",
|
||
|
"w",
|
||
|
"I",
|
||
|
"d",
|
||
|
"E",
|
||
|
"i",
|
||
|
"e",
|
||
|
"c",
|
||
|
"t",
|
||
|
"T",
|
||
|
];
|
||
|
}
|