-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
formatting.ts
243 lines (212 loc) · 6.17 KB
/
formatting.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { int, pad } from "../utils";
import { Locale } from "../types/locale";
import { ParsedOptions } from "../types/options";
export type token =
| "D"
| "F"
| "G"
| "H"
| "J"
| "K"
| "M"
| "S"
| "U"
| "W"
| "Y"
| "Z"
| "d"
| "h"
| "i"
| "j"
| "l"
| "m"
| "n"
| "s"
| "u"
| "w"
| "y";
const doNothing = (): undefined => undefined;
export const monthToStr = (
monthNumber: number,
shorthand: boolean,
locale: Locale
) => locale.months[shorthand ? "shorthand" : "longhand"][monthNumber];
export type RevFormatFn = (
date: Date,
data: string,
locale: Locale
) => Date | void | undefined;
export type RevFormat = Record<string, RevFormatFn>;
export const revFormat: RevFormat = {
D: doNothing,
F: function (dateObj: Date, monthName: string, locale: Locale) {
dateObj.setMonth(locale.months.longhand.indexOf(monthName));
},
G: (dateObj: Date, hour: string) => {
dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
},
H: (dateObj: Date, hour: string) => {
dateObj.setHours(parseFloat(hour));
},
J: (dateObj: Date, day: string) => {
dateObj.setDate(parseFloat(day));
},
K: (dateObj: Date, amPM: string, locale: Locale) => {
dateObj.setHours(
(dateObj.getHours() % 12) +
12 * int(new RegExp(locale.amPM[1], "i").test(amPM))
);
},
M: function (dateObj: Date, shortMonth: string, locale: Locale) {
dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth));
},
S: (dateObj: Date, seconds: string) => {
dateObj.setSeconds(parseFloat(seconds));
},
U: (_: Date, unixSeconds: string) => new Date(parseFloat(unixSeconds) * 1000),
W: function (dateObj: Date, weekNum: string, locale: Locale) {
const weekNumber = parseInt(weekNum);
const date = new Date(
dateObj.getFullYear(),
0,
2 + (weekNumber - 1) * 7,
0,
0,
0,
0
);
date.setDate(date.getDate() - date.getDay() + locale.firstDayOfWeek);
return date;
},
Y: (dateObj: Date, year: string) => {
dateObj.setFullYear(parseFloat(year));
},
Z: (_: Date, ISODate: string) => new Date(ISODate),
d: (dateObj: Date, day: string) => {
dateObj.setDate(parseFloat(day));
},
h: (dateObj: Date, hour: string) => {
dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
},
i: (dateObj: Date, minutes: string) => {
dateObj.setMinutes(parseFloat(minutes));
},
j: (dateObj: Date, day: string) => {
dateObj.setDate(parseFloat(day));
},
l: doNothing,
m: (dateObj: Date, month: string) => {
dateObj.setMonth(parseFloat(month) - 1);
},
n: (dateObj: Date, month: string) => {
dateObj.setMonth(parseFloat(month) - 1);
},
s: (dateObj: Date, seconds: string) => {
dateObj.setSeconds(parseFloat(seconds));
},
u: (_: Date, unixMillSeconds: string) =>
new Date(parseFloat(unixMillSeconds)),
w: doNothing,
y: (dateObj: Date, year: string) => {
dateObj.setFullYear(2000 + parseFloat(year));
},
};
export type TokenRegex = { [k in token]: string };
export const tokenRegex: TokenRegex = {
D: "", // locale-dependent, setup on runtime
F: "", // locale-dependent, setup on runtime
G: "(\\d\\d|\\d)",
H: "(\\d\\d|\\d)",
J: "(\\d\\d|\\d)\\w+",
K: "", // locale-dependent, setup on runtime
M: "", // locale-dependent, setup on runtime
S: "(\\d\\d|\\d)",
U: "(.+)",
W: "(\\d\\d|\\d)",
Y: "(\\d{4})",
Z: "(.+)",
d: "(\\d\\d|\\d)",
h: "(\\d\\d|\\d)",
i: "(\\d\\d|\\d)",
j: "(\\d\\d|\\d)",
l: "", // locale-dependent, setup on runtime
m: "(\\d\\d|\\d)",
n: "(\\d\\d|\\d)",
s: "(\\d\\d|\\d)",
u: "(.+)",
w: "(\\d\\d|\\d)",
y: "(\\d{2})",
};
export type Formats = Record<
token,
(date: Date, locale: Locale, options: ParsedOptions) => string | number
>;
export const formats: Formats = {
// get the date in UTC
Z: (date: Date) => date.toISOString(),
// weekday name, short, e.g. Thu
D: function (date: Date, locale: Locale, options: ParsedOptions) {
return locale.weekdays.shorthand[
formats.w(date, locale, options) as number
];
},
// full month name e.g. January
F: function (date: Date, locale: Locale, options: ParsedOptions) {
return monthToStr(
(formats.n(date, locale, options) as number) - 1,
false,
locale
);
},
// padded hour 1-12
G: function (date: Date, locale: Locale, options: ParsedOptions) {
return pad(formats.h(date, locale, options));
},
// hours with leading zero e.g. 03
H: (date: Date) => pad(date.getHours()),
// day (1-30) with ordinal suffix e.g. 1st, 2nd
J: function (date: Date, locale: Locale) {
return locale.ordinal !== undefined
? date.getDate() + locale.ordinal(date.getDate())
: date.getDate();
},
// AM/PM
K: (date: Date, locale: Locale) => locale.amPM[int(date.getHours() > 11)],
// shorthand month e.g. Jan, Sep, Oct, etc
M: function (date: Date, locale: Locale) {
return monthToStr(date.getMonth(), true, locale);
},
// seconds 00-59
S: (date: Date) => pad(date.getSeconds()),
// unix timestamp
U: (date: Date) => date.getTime() / 1000,
W: function (date: Date, _: Locale, options: ParsedOptions) {
return options.getWeek(date);
},
// full year e.g. 2016, padded (0001-9999)
Y: (date: Date) => pad(date.getFullYear(), 4),
// day in month, padded (01-30)
d: (date: Date) => pad(date.getDate()),
// hour from 1-12 (am/pm)
h: (date: Date) => (date.getHours() % 12 ? date.getHours() % 12 : 12),
// minutes, padded with leading zero e.g. 09
i: (date: Date) => pad(date.getMinutes()),
// day in month (1-30)
j: (date: Date) => date.getDate(),
// weekday name, full, e.g. Thursday
l: function (date: Date, locale: Locale) {
return locale.weekdays.longhand[date.getDay()];
},
// padded month number (01-12)
m: (date: Date) => pad(date.getMonth() + 1),
// the month number (1-12)
n: (date: Date) => date.getMonth() + 1,
// seconds 0-59
s: (date: Date) => date.getSeconds(),
// Unix Milliseconds
u: (date: Date) => date.getTime(),
// number of the day of the week
w: (date: Date) => date.getDay(),
// last two digits of year e.g. 16 for 2016
y: (date: Date) => String(date.getFullYear()).substring(2),
};