-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseDate.ts
393 lines (355 loc) · 11.1 KB
/
parseDate.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { tzLocal } from './LocalTimezone'
import {
I18nSettings,
LocaleInfo,
getLocaleInfo,
NameStyle
} from './LocaleInfo'
import { Timezone } from './Timezone'
import { tzUtc } from './UTCTimezone'
import { daysInMonth } from './daysInMonth'
interface DateInfo {
year: number
monthIndex: number
day: number
hour: number
minute: number
second: number
millisecond: number
isAfternoon: number
timezoneOffset: number | null
}
interface ParseInfo {
field: keyof DateInfo | null
pattern: string
parse?: (value: string, localeInfo: I18nSettings) => number | null
requiredField?: keyof DateInfo
}
const literalRegex = /\[([^]*?)\]/gm
const tokenRegex =
/d{1,4}|m{1,4}|yy(?:yy)?|S{1,3}|F{1,3}|D{2,4}|ZZ|Z|([HhMS])\1?|t|"[^"]*"|'[^']*'/g
const fourDigitsPattern = '\\d{4}'
const threeDigitsPattern = '\\d{3}'
const twoDigitsPattern = '\\d{2}'
const oneOrTwoDigitsPattern = '\\d{1,2}'
const wordPattern = '[^\\s]+'
const escapeRegexTokens = (text: string): string =>
text.replace(/[|\\{()[^$+*?.-]/g, '\\$&')
function parseMonthName(
name: string,
style: NameStyle,
localeInfo: I18nSettings
): number | null {
const index = localeInfo.month[style].findIndex(
x => x.localeCompare(name, localeInfo.locale, { sensitivity: 'base' }) == 0
)
return index === -1 ? null : index
}
function parseDecade(value: string): number {
const decade = +value
const currentYear = new Date().getFullYear()
const currentCentury = currentYear - (currentYear % 100)
const year = currentCentury + decade
const yearsBetween = year - currentYear
if (Math.abs(yearsBetween) <= 50) {
return year
} else if (yearsBetween > 0) {
return year - 100
} else {
return year + 100
}
}
const parseMonthIndex = (value: string): number => +value - 1
function parseDayPeriod(
value: string,
localeInfo: LocaleInfo,
style: NameStyle
): number {
const dayPeriods = localeInfo.dayPeriod[style]
const [morning, afternoon] = dayPeriods
if (
value.localeCompare(morning, localeInfo.locale, {
sensitivity: 'base'
}) === 0
) {
return -1
} else if (
value.localeCompare(afternoon, localeInfo.locale, {
sensitivity: 'base'
}) === 0
) {
return 1
} else {
return 0
}
}
const emptyWordParseInfo: ParseInfo = { field: null, pattern: wordPattern }
const timezoneOffsetParseInfo: ParseInfo = {
field: 'timezoneOffset',
pattern: '[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?',
parse: (value: string): number | null => {
const parts = (value + '').match(/([+-]|\d\d)/gi)
if (parts) {
const minutes = +parts[1] * 60 + parseInt(parts[2], 10)
return parts[0] === '+' ? minutes : -minutes
}
return 0
}
}
const parseInfoMap: Record<string, ParseInfo> = {
d: { field: 'day', pattern: oneOrTwoDigitsPattern },
dd: { field: 'day', pattern: twoDigitsPattern },
ddd: emptyWordParseInfo,
dddd: emptyWordParseInfo,
DD: {
field: 'day',
pattern: oneOrTwoDigitsPattern + wordPattern,
parse: value => parseInt(value, 10)
},
DDD: emptyWordParseInfo,
DDDD: emptyWordParseInfo,
m: {
field: 'monthIndex',
pattern: oneOrTwoDigitsPattern,
parse: parseMonthIndex
},
mm: {
field: 'monthIndex',
pattern: twoDigitsPattern,
parse: parseMonthIndex
},
mmm: {
field: 'monthIndex',
pattern: wordPattern,
parse: (value, localeInfo) => parseMonthName(value, 'short', localeInfo)
},
mmmm: {
field: 'monthIndex',
pattern: wordPattern,
parse: (value, localeInfo) => parseMonthName(value, 'long', localeInfo)
},
yy: {
field: 'year',
pattern: twoDigitsPattern,
parse: parseDecade
},
yyyy: { field: 'year', pattern: fourDigitsPattern },
h: {
field: 'hour',
pattern: oneOrTwoDigitsPattern,
requiredField: 'isAfternoon'
},
hh: {
field: 'hour',
pattern: twoDigitsPattern,
requiredField: 'isAfternoon'
},
H: { field: 'hour', pattern: oneOrTwoDigitsPattern },
HH: { field: 'hour', pattern: twoDigitsPattern },
M: { field: 'minute', pattern: oneOrTwoDigitsPattern },
MM: { field: 'minute', pattern: twoDigitsPattern },
S: { field: 'second', pattern: oneOrTwoDigitsPattern },
SS: { field: 'second', pattern: twoDigitsPattern },
F: {
field: 'millisecond',
pattern: '\\d',
parse: v => +v * 100
},
FF: { field: 'millisecond', pattern: twoDigitsPattern, parse: v => +v * 10 },
FFF: { field: 'millisecond', pattern: threeDigitsPattern },
t: {
field: 'isAfternoon',
pattern: wordPattern,
parse: (value, localeInfo) => parseDayPeriod(value, localeInfo, 'narrow')
},
ZZ: timezoneOffsetParseInfo,
Z: timezoneOffsetParseInfo
}
function isDateInfoValid(dateInfo: DateInfo): boolean {
return (
dateInfo.monthIndex >= 0 &&
dateInfo.monthIndex < 12 &&
dateInfo.day >= 1 &&
dateInfo.day <= daysInMonth(dateInfo.year, dateInfo.monthIndex) &&
dateInfo.hour >= 0 &&
dateInfo.hour < 24 &&
dateInfo.minute >= 0 &&
dateInfo.minute < 60 &&
dateInfo.second >= 0 &&
dateInfo.second < 60 &&
dateInfo.millisecond >= 0 &&
dateInfo.millisecond < 1000
)
}
function applyPattern(
text: string,
formatPattern: string,
formatParseInfos: ParseInfo[],
localeInfo: I18nSettings
): DateInfo | null {
// Check if the date string matches the format. If it doesn't return null
const matches = text.match(new RegExp(formatPattern, 'i'))
if (!matches || matches[0] !== matches.input) {
return null
}
// Default to the beginning of this year.
const dateInfo: DateInfo = {
year: new Date().getFullYear(),
monthIndex: 0,
day: 1,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
isAfternoon: 0,
timezoneOffset: null
}
// For each match, call the parser function for that date part
for (let i = 1; i < matches.length; ++i) {
const { field, parse: parser } = formatParseInfos[i - 1]
const value = parser ? parser(matches[i], localeInfo) : +matches[i]
// If the parser can't make sense of the value, return null
if (value == null) {
return null
}
if (field != null) {
dateInfo[field] = value
}
}
if (dateInfo.isAfternoon > 0 && dateInfo.hour !== 12) {
dateInfo.hour = +dateInfo.hour + 12
} else if (dateInfo.isAfternoon < 0 && dateInfo.hour === 12) {
dateInfo.hour = 0
}
return dateInfo
}
function createDateParser(
format: string
): (value: string, localeInfo: I18nSettings) => DateInfo | null {
// Replace all the literals with @@@. Hopefully a string that won't exist in the format
const literals: string[] = []
let formatWithoutLiterals = format.replace(literalRegex, (_match, p1) => {
literals.push(escapeRegexTokens(p1))
return '@@@'
})
// Change every token that we find into the correct regex
const formatParseInfos: ParseInfo[] = []
const specifiedFields: { [field: string]: boolean } = {}
const requiredFields: { [field: string]: boolean } = {}
const formatPatternWithoutLiterals = escapeRegexTokens(
formatWithoutLiterals
).replace(tokenRegex, match => {
const parseInfo = parseInfoMap[match]
// Check if the person has specified the same field twice. This will lead to confusing results.
if (parseInfo.field != null && parseInfo.field in specifiedFields) {
throw new Error(`Invalid format. Duplicate field "${parseInfo.field}"`)
}
if (parseInfo.field != null) {
specifiedFields[parseInfo.field] = true
}
// Check if there are any required fields. For instance, 12 hour time requires AM/PM specified
if (parseInfo.requiredField) {
requiredFields[parseInfo.requiredField] = true
}
formatParseInfos.push(parseInfo)
return '(' + parseInfo.pattern + ')'
})
for (const field in requiredFields) {
if (!(field in specifiedFields)) {
throw new Error(`Invalid format. Missing required field "${field}"`)
}
}
// Add back the literals.
const formatPattern = formatPatternWithoutLiterals.replace(
/@@@/g,
() => literals.shift() as string
)
return (value: string, localeInfo: I18nSettings) =>
applyPattern(value, formatPattern, formatParseInfos, localeInfo)
}
/**
* Parse a date string into a Javascript Date object.
*
* If the timezone offset is absent the dates will be according to the local
* timezone.
*
* ```js
* import { parseDate } from '@jetblack/date'
*
* const d = parseDate('12 March, 1998', 'dd mmm, yyyy')
* ```
*
* ### Pattern items
*
* | Value | Description |
* | ----- | ----------- |
* | d | Day of the month as digits; no leading zero for single-digit days. |
* | dd | Day of the month as digits; leading zero for single-digit days. |
* | ddd | Day of the week as a three-letter abbreviation. |
* | dddd | Day of the week as its full name. |
* | DD | Day of the month with the plural suffix. |
* | m | Month as digits; no leading zero for single-digit months. |
* | mm | Month as digits; leading zero for single-digit months. |
* | mmm | Month as a three-letter abbreviation. |
* | mmmm | Month as its full name. |
* | yy | Year as last two digits; leading zero for years less than 10. |
* | yyyy | Year represented by four digits. |
* | h | Hours; no leading zero for single-digit hours (12-hour clock). |
* | hh | Hours; leading zero for single-digit hours (12-hour clock). |
* | H | Hours; no leading zero for single-digit hours (24-hour clock). |
* | HH | Hours; leading zero for single-digit hours (24-hour clock). |
* | M | Minutes; no leading zero for single-digit minutes. |
* | MM | Minutes; leading zero for single-digit minutes. |
* | S | Seconds; no leading zero for single-digit seconds. |
* | SS | Seconds; leading zero for single-digit seconds. |
* | F | Hundreds of milliseconds. |
* | FF | Tens of milliseconds with a leading zero for single-digit values. |
* | FFF | Milliseconds: zero padded. |
* | t | Narrow dayPeriod from locale. |
* | tt | Short dayPeriod from locale. |
* | ttt | Long dayPeriod from locale. |
* | Z | Timezone offset. |
* | [dhM] | Literal characters. |
*
* @category Formatting
*
* @param text Date string
* @param format Date parse format
* @param tz An optional timezone. Defaults to the local timezone.
* @param locale An optional locale. Defaults to the current locale.
* @returns The date, or null if parsing failed.
*/
export function parseDate(
text: string,
format: string,
tz: Timezone = tzLocal,
locale: I18nSettings | string | undefined = undefined
): Date | null {
const localeInfo = getLocaleInfo(locale)
const dateParser = createDateParser(format)
const dateInfo = dateParser(text, localeInfo)
if (dateInfo == null || !isDateInfoValid(dateInfo)) {
return null
}
if (dateInfo.timezoneOffset == null) {
return tz.makeDate(
dateInfo.year,
dateInfo.monthIndex,
dateInfo.day,
dateInfo.hour,
dateInfo.minute,
dateInfo.second,
dateInfo.millisecond
)
} else {
return tzUtc.makeDate(
dateInfo.year,
dateInfo.monthIndex,
dateInfo.day,
dateInfo.hour,
dateInfo.minute - dateInfo.timezoneOffset,
dateInfo.second,
dateInfo.millisecond
)
}
}