forked from date-fns/date-fns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecondParser.ts
35 lines (30 loc) · 1 KB
/
SecondParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { Match } from "../../../locale/types.js";
import { numericPatterns } from "../constants.js";
import { Parser } from "../Parser.js";
import type { ParseFlags, ParseResult } from "../types.js";
import { parseNumericPattern } from "../utils.js";
export class SecondParser extends Parser<number> {
priority = 50;
parse(dateString: string, token: string, match: Match): ParseResult<number> {
switch (token) {
case "s":
return parseNumericPattern(numericPatterns.second, dateString);
case "so":
return match.ordinalNumber(dateString, { unit: "second" });
default:
return parseNumericPattern(numericPatterns.exactTwoDigits, dateString);
}
}
validate<DateType extends Date>(_date: DateType, value: number): boolean {
return value >= 0 && value <= 59;
}
set<DateType extends Date>(
date: DateType,
_flags: ParseFlags,
value: number,
): DateType {
date.setSeconds(value, 0);
return date;
}
incompatibleTokens = ["t", "T"];
}