1 | import { DebugConsume, DebugHandler } from "./debugging";
|
2 |
|
3 | export interface ParsingOption {
|
4 | /**
|
5 | * To parse only forward dates (the results should be after the reference date).
|
6 | * This effects date/time implication (e.g. weekday or time mentioning)
|
7 | */
|
8 | forwardDate?: boolean;
|
9 |
|
10 | /**
|
11 | * Additional timezone keywords for the parsers to recognize.
|
12 | * Any value provided will override the default handling of that value.
|
13 | */
|
14 | timezones?: TimezoneAbbrMap;
|
15 |
|
16 | /**
|
17 | * Internal debug event handler.
|
18 | * @internal
|
19 | */
|
20 | debug?: DebugHandler | DebugConsume;
|
21 | }
|
22 |
|
23 | /**
|
24 | * Some timezone abbreviations are ambiguous in that they refer to different offsets
|
25 | * depending on the time of year — daylight savings time (DST), or non-DST. This interface
|
26 | * allows defining such timezones
|
27 | */
|
28 | export interface AmbiguousTimezoneMap {
|
29 | timezoneOffsetDuringDst: number;
|
30 | timezoneOffsetNonDst: number;
|
31 | /**
|
32 | * Return the start date of DST for the given year.
|
33 | * timezone.ts contains helper methods for common such rules.
|
34 | */
|
35 | dstStart: (year: number) => Date;
|
36 | /**
|
37 | * Return the end date of DST for the given year.
|
38 | * timezone.ts contains helper methods for common such rules.
|
39 | */
|
40 | dstEnd: (year: number) => Date;
|
41 | }
|
42 |
|
43 | /**
|
44 | * A map describing how timezone abbreviations should map to time offsets.
|
45 | * Supports both unambigous mappings abbreviation => offset,
|
46 | * and ambiguous mappings, where the offset will depend on whether the
|
47 | * time in question is during daylight savings time or not.
|
48 | */
|
49 | export type TimezoneAbbrMap = { [key: string]: number | AmbiguousTimezoneMap };
|
50 |
|
51 | export interface ParsingReference {
|
52 | /**
|
53 | * Reference date. The instant (JavaScript Date object) when the input is written or mention.
|
54 | * This effect date/time implication (e.g. weekday or time mentioning).
|
55 | * (default = now)
|
56 | */
|
57 | instant?: Date;
|
58 |
|
59 | /**
|
60 | * Reference timezone. The timezone where the input is written or mention.
|
61 | * Date/time implication will account the difference between input timezone and the current system timezone.
|
62 | * (default = current timezone)
|
63 | */
|
64 | timezone?: string | number;
|
65 | }
|
66 |
|
67 | /**
|
68 | * Parsed result or final output.
|
69 | * Each result object represents a date/time (or date/time-range) mentioning in the input.
|
70 | */
|
71 | export interface ParsedResult {
|
72 | readonly refDate: Date;
|
73 | readonly index: number;
|
74 | readonly text: string;
|
75 |
|
76 | readonly start: ParsedComponents;
|
77 | readonly end?: ParsedComponents;
|
78 |
|
79 | /**
|
80 | * @return a javascript date object created from the `result.start`.
|
81 | */
|
82 | date(): Date;
|
83 |
|
84 | /**
|
85 | * @return debugging tags combined of the `result.start` and `result.end`.
|
86 | */
|
87 | tags(): Set<string>;
|
88 | }
|
89 |
|
90 | /**
|
91 | * A collection of parsed date/time components (e.g. day, hour, minute, ..., etc).
|
92 | *
|
93 | * Each parsed component has three different levels of certainty.
|
94 | * - *Certain* (or *Known*): The component is directly mentioned and parsed.
|
95 | * - *Implied*: The component is not directly mentioned, but implied by other parsed information.
|
96 | * - *Unknown*: Completely no mention of the component.
|
97 | */
|
98 | export interface ParsedComponents {
|
99 | /**
|
100 | * Check the component certainly if the component is *Certain* (or *Known*)
|
101 | */
|
102 | isCertain(component: Component): boolean;
|
103 |
|
104 | /**
|
105 | * Get the component value for either *Certain* or *Implied* value.
|
106 | */
|
107 | get(component: Component): number | null;
|
108 |
|
109 | /**
|
110 | * @return a javascript date object.
|
111 | */
|
112 | date(): Date;
|
113 |
|
114 | /**
|
115 | * @return debugging tags of the parsed component.
|
116 | */
|
117 | tags(): Set<string>;
|
118 | }
|
119 |
|
120 | export type Component =
|
121 | | "year"
|
122 | | "month"
|
123 | | "day"
|
124 | | "weekday"
|
125 | | "hour"
|
126 | | "minute"
|
127 | | "second"
|
128 | | "millisecond"
|
129 | | "meridiem"
|
130 | | "timezoneOffset";
|
131 |
|
132 | export enum Meridiem {
|
133 | AM = 0,
|
134 | PM = 1,
|
135 | }
|
136 |
|
137 | export enum Weekday {
|
138 | SUNDAY = 0,
|
139 | MONDAY = 1,
|
140 | TUESDAY = 2,
|
141 | WEDNESDAY = 3,
|
142 | THURSDAY = 4,
|
143 | FRIDAY = 5,
|
144 | SATURDAY = 6,
|
145 | }
|
146 |
|
147 | export enum Month {
|
148 | JANUARY = 1,
|
149 | FEBRUARY = 2,
|
150 | MARCH = 3,
|
151 | APRIL = 4,
|
152 | MAY = 5,
|
153 | JUNE = 6,
|
154 | JULY = 7,
|
155 | AUGUST = 8,
|
156 | SEPTEMBER = 9,
|
157 | OCTOBER = 10,
|
158 | NOVEMBER = 11,
|
159 | DECEMBER = 12,
|
160 | }
|