UNPKG

9.53 kBTypeScriptView Raw
1// Last module patch version validated against: 4.0.0
2
3/**
4 * Specification of time locale to use when creating a new TimeLocaleObject
5 */
6export interface TimeLocaleDefinition {
7 /**
8 * The date and time (%c) format specifier (e.g., "%a %b %e %X %Y").
9 */
10 dateTime: string;
11 /**
12 * The date (%x) format specifier (e.g., "%m/%d/%Y").
13 */
14 date: string;
15 /**
16 * The time (%X) format specifier (e.g., "%H:%M:%S").
17 */
18 time: string;
19 /**
20 * The A.M. and P.M. equivalents (e.g., ["AM", "PM"]).
21 */
22 periods: [string, string];
23 /**
24 * The full names of the weekdays, starting with Sunday.
25 */
26 days: [string, string, string, string, string, string, string];
27 /**
28 * The abbreviated names of the weekdays, starting with Sunday.
29 */
30 shortDays: [string, string, string, string, string, string, string];
31 /**
32 * The full names of the months (starting with January).
33 */
34 months: [string, string, string, string, string, string, string, string, string, string, string, string];
35 /**
36 * the abbreviated names of the months (starting with January).
37 */
38 shortMonths: [string, string, string, string, string, string, string, string, string, string, string, string];
39}
40
41/**
42 * Interface describing a time-locale-based object which exposes time-formatting/parsing
43 * methods for a specified locale definition.
44 */
45export interface TimeLocaleObject {
46 /**
47 * Returns a new formatter for the given string specifier. The specifier string may contain the following directives:
48 * - %a - abbreviated weekday name.*
49 * - %A - full weekday name.*
50 * - %b - abbreviated month name.*
51 * - %B - full month name.*
52 * - %c - the locale’s date and time, such as %x, %X.*
53 * - %d - zero-padded day of the month as a decimal number [01,31].
54 * - %e - space-padded day of the month as a decimal number [ 1,31]; equivalent to %_d.
55 * - %f - microseconds as a decimal number [000000, 999999].
56 * - %g - ISO 8601 week-based year without century as a decimal number [00,99].
57 * - %G - ISO 8601 week-based year with century as a decimal number.
58 * - %H - hour (24-hour clock) as a decimal number [00,23].
59 * - %I - hour (12-hour clock) as a decimal number [01,12].
60 * - %j - day of the year as a decimal number [001,366].
61 * - %m - month as a decimal number [01,12].
62 * - %M - minute as a decimal number [00,59].
63 * - %L - milliseconds as a decimal number [000, 999].
64 * - %p - either AM or PM.*
65 * - %q - quarter of the year as a decimal number [1,4].
66 * - %Q - milliseconds since UNIX epoch.
67 * - %s - seconds since UNIX epoch.
68 * - %S - second as a decimal number [00,61].
69 * - %u - Monday-based (ISO) weekday as a decimal number [1,7].
70 * - %U - Sunday-based week of the year as a decimal number [00,53].
71 * - %V - ISO 8601 week number of the year as a decimal number [01, 53].
72 * - %w - Sunday-based weekday as a decimal number [0,6].
73 * - %W - Monday-based week of the year as a decimal number [00,53].
74 * - %x - the locale’s date, such as %-m/%-d/%Y.*
75 * - %X - the locale’s time, such as %-I:%M:%S %p.*
76 * - %y - year without century as a decimal number [00,99].
77 * - %Y - year with century as a decimal number.
78 * - %Z - time zone offset, such as -0700, -07:00, -07, or Z.
79 * - %% - a literal percent sign (%).
80 *
81 * Directives marked with an asterisk (*) may be affected by the locale definition.
82 *
83 * For %U, all days in a new year preceding the first Sunday are considered to be in week 0.
84 * For %W, all days in a new year preceding the first Monday are considered to be in week 0.
85 * Week numbers are computed using interval.count. For example, 2015-52 and 2016-00 represent Monday, December 28, 2015, while 2015-53 and 2016-01 represent Monday, January 4, 2016.
86 * This differs from the ISO week date specification (%V), which uses a more complicated definition!
87 *
88 * For %V,%g and %G, per the strftime man page:
89 *
90 * In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week.
91 * Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday;
92 * or, the week that has 4 January in it). If the ISO week number belongs to the previous or next year, that year is used instead.
93 *
94 * The % sign indicating a directive may be immediately followed by a padding modifier:
95 *
96 * 1) 0 - zero-padding
97 * 2) _ - space-padding
98 * 3) - disable padding
99 *
100 * If no padding modifier is specified, the default is 0 for all directives except %e, which defaults to _.
101 * (In some implementations of strftime and strptime, a directive may include an optional field width or precision; this feature is not yet implemented.)
102 *
103 * The returned function formats a specified date, returning the corresponding string.
104 *
105 * @param specifier A specifier string for the date format.
106 */
107 format(specifier: string): (date: Date) => string;
108 /**
109 * Returns a new parser for the given string specifier. The specifier string may contain the same directives as locale.format (TimeLocaleObject.format).
110 * The %d and %e directives are considered equivalent for parsing.
111 *
112 * The returned function parses a specified string, returning the corresponding date or null if the string could not be parsed according to this format’s specifier.
113 * Parsing is strict: if the specified string does not exactly match the associated specifier, this method returns null.
114 *
115 * For example, if the associated specifier is %Y-%m-%dT%H:%M:%SZ, then the string "2011-07-01T19:15:28Z" will be parsed as expected,
116 * but "2011-07-01T19:15:28", "2011-07-01 19:15:28" and "2011-07-01" will return null. (Note that the literal Z here is different from the time zone offset directive %Z.)
117 * If a more flexible parser is desired, try multiple formats sequentially until one returns non-null.
118 *
119 * @param specifier A specifier string for the date format.
120 */
121 parse(specifier: string): (dateString: string) => Date | null;
122 /**
123 * Equivalent to locale.format (TimeLocaleObject.format), except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.
124 *
125 * @param specifier A specifier string for the date format.
126 */
127 utcFormat(specifier: string): (date: Date) => string;
128 /**
129 * Equivalent to locale.parse (TimeLocaleObject.parse), except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.
130 *
131 * @param specifier A specifier string for the date format.
132 */
133 utcParse(specifier: string): (dateString: string) => Date | null;
134}
135
136/**
137 * Create a new time-locale-based object which exposes time-formatting
138 * methods for the specified locale definition.
139 *
140 * @param definition A time locale definition.
141 */
142export function timeFormatLocale(definition: TimeLocaleDefinition): TimeLocaleObject;
143
144/**
145 * Create a new time-locale-based object which exposes time-formatting
146 * methods for the specified locale definition. The new time locale definition
147 * will be set as the new default time locale.
148 *
149 * @param definition A time locale definition.
150 */
151export function timeFormatDefaultLocale(definition: TimeLocaleDefinition): TimeLocaleObject;
152
153/**
154 * Returns a new formatter for the given string specifier. The returned function formats a specified date, returning the corresponding string.
155 *
156 * An alias for locale.format (TimeLocaleObject.format) on the default locale.
157 *
158 * @param specifier A specifier string for the date format.
159 */
160export function timeFormat(specifier: string): (date: Date) => string;
161
162/**
163 * Returns a new parser for the given string specifier.
164 *
165 * An alias for locale.parse (TimeLocaleObject.parse) on the default locale.
166 *
167 * @param specifier A specifier string for the date format.
168 */
169export function timeParse(specifier: string): (dateString: string) => Date | null;
170
171/**
172 * Equivalent to timeFormat, except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.
173 *
174 * An alias for locale.utcFormat (TimeLocaleObject.utcFormat) on the default locale.
175 *
176 * @param specifier A specifier string for the date format.
177 */
178export function utcFormat(specifier: string): (date: Date) => string;
179
180/**
181 * Equivalent to timeParse, except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.
182 *
183 * An alias for locale.utcParse (TimeLocaleObject.utcParse) on the default locale.
184 *
185 * @param specifier A specifier string for the date format.
186 */
187export function utcParse(specifier: string): (dateString: string) => Date | null;
188
189/**
190 * The full ISO 8601 UTC time formatter. Where available, this method will use Date.toISOString to format.
191 *
192 * @param date A date to format.
193 */
194export function isoFormat(date: Date): string;
195
196/**
197 * The full ISO 8601 UTC time parser. Where available, this method will use the Date constructor to parse strings.
198 * If you depend on strict validation of the input format according to ISO 8601, you should construct a UTC parser function using utcParse.
199 *
200 * @param dateString A string encoded date to parse.
201 */
202export function isoParse(dateString: string): Date | null;