/
index.js
540 lines (451 loc) · 12.6 KB
/
index.js
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import toInteger from '../_lib/toInteger/index.js'
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index.js'
var MILLISECONDS_IN_HOUR = 3600000
var MILLISECONDS_IN_MINUTE = 60000
var DEFAULT_ADDITIONAL_DIGITS = 2
var patterns = {
dateTimeDelimeter: /[T ]/,
plainTime: /:/,
timeZoneDelimeter: /[Z ]/i,
// year tokens
YY: /^(\d{2})$/,
YYY: [
/^([+-]\d{2})$/, // 0 additional digits
/^([+-]\d{3})$/, // 1 additional digit
/^([+-]\d{4})$/ // 2 additional digits
],
YYYY: /^(\d{4})/,
YYYYY: [
/^([+-]\d{4})/, // 0 additional digits
/^([+-]\d{5})/, // 1 additional digit
/^([+-]\d{6})/ // 2 additional digits
],
// date tokens
MM: /^-(\d{2})$/,
DDD: /^-?(\d{3})$/,
MMDD: /^-?(\d{2})-?(\d{2})$/,
Www: /^-?W(\d{2})$/,
WwwD: /^-?W(\d{2})-?(\d{1})$/,
HH: /^(\d{2}([.,]\d*)?)$/,
HHMM: /^(\d{2}):?(\d{2}([.,]\d*)?)$/,
HHMMSS: /^(\d{2}):?(\d{2}):?(\d{2}([.,]\d*)?)$/,
// timezone tokens
timezone: /([Z+-].*)$/,
timezoneZ: /^(Z)$/,
timezoneHH: /^([+-])(\d{2})$/,
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/
}
/**
* @name toDate
* @category Common Helpers
* @summary Convert the given argument to an instance of Date.
*
* @description
* Convert the given argument to an instance of Date.
*
* If the argument is an instance of Date, the function returns its clone.
*
* If the argument is a number, it is treated as a timestamp.
*
* If an argument is a string, the function tries to parse it.
* Function accepts complete ISO 8601 formats as well as partial implementations.
* ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
* If the function cannot parse the string or the values are invalid, it returns Invalid Date.
*
* If the argument is none of the above, the function returns Invalid Date.
*
* **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
* All *date-fns* functions will throw `RangeError` if `options.additionalDigits` is not 0, 1, 2 or undefined.
*
*
* ### v2.0.0 breaking changes:
*
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
*
* - The previous `parse` implementation was renamed to `toDate`.
*
* ```javascript
* // Before v2.0.0
* parse('2016-01-01')
*
* // v2.0.0 onward
* toDate('2016-01-01')
* ```
*
* - `toDate` now validates separate date and time values in ISO-8601 strings
* and returns `Invalid Date` if the date is invalid.
*
* ```javascript
* toDate('2018-13-32')
* //=> Invalid Date
* ```
*
* - `toDate` now doesn't fall back to `new Date` constructor
* if it fails to parse a string argument. Instead, it returns `Invalid Date`.
*
* @param {Date|String|Number} argument - the value to convert
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - the additional number of digits in the extended year format
* @returns {Date} the parsed date in the local time zone
* @throws {TypeError} 1 argument required
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
*
* @example
* // Convert string '2014-02-11T11:30:30' to date:
* var result = toDate('2014-02-11T11:30:30')
* //=> Tue Feb 11 2014 11:30:30
*
* @example
* // Convert string '+02014101' to date,
* // if the additional number of digits in the extended year format is 1:
* var result = toDate('+02014101', { additionalDigits: 1 })
* //=> Fri Apr 11 2014 00:00:00
*/
export default function toDate(argument, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
if (argument === null) {
return new Date(NaN)
}
var options = dirtyOptions || {}
var additionalDigits =
options.additionalDigits == null
? DEFAULT_ADDITIONAL_DIGITS
: toInteger(options.additionalDigits)
if (
additionalDigits !== 2 &&
additionalDigits !== 1 &&
additionalDigits !== 0
) {
throw new RangeError('additionalDigits must be 0, 1 or 2')
}
// Clone the date
if (
argument instanceof Date ||
(typeof argument === 'object' &&
Object.prototype.toString.call(argument) === '[object Date]')
) {
// Prevent the date to lose the milliseconds when passed to new Date() in IE10
return new Date(argument.getTime())
} else if (
typeof argument === 'number' ||
Object.prototype.toString.call(argument) === '[object Number]'
) {
return new Date(argument)
} else if (
!(
typeof argument === 'string' ||
Object.prototype.toString.call(argument) === '[object String]'
)
) {
return new Date(NaN)
}
var dateStrings = splitDateString(argument)
var parseYearResult = parseYear(dateStrings.date, additionalDigits)
var year = parseYearResult.year
var restDateString = parseYearResult.restDateString
var date = parseDate(restDateString, year)
if (isNaN(date)) {
return new Date(NaN)
}
if (date) {
var timestamp = date.getTime()
var time = 0
var offset
if (dateStrings.time) {
time = parseTime(dateStrings.time)
if (isNaN(time)) {
return new Date(NaN)
}
}
if (dateStrings.timezone) {
offset = parseTimezone(dateStrings.timezone)
if (isNaN(offset)) {
return new Date(NaN)
}
} else {
// get offset accurate to hour in timezones that change offset
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time))
}
return new Date(timestamp + time + offset)
} else {
return new Date(NaN)
}
}
function splitDateString(dateString) {
var dateStrings = {}
var array = dateString.split(patterns.dateTimeDelimeter)
var timeString
if (patterns.plainTime.test(array[0])) {
dateStrings.date = null
timeString = array[0]
} else {
dateStrings.date = array[0]
timeString = array[1]
if (patterns.timeZoneDelimeter.test(dateStrings.date)) {
dateStrings.date = dateString.split(patterns.timeZoneDelimeter)[0]
timeString = dateString.substr(dateStrings.date.length, dateString.length)
}
}
if (timeString) {
var token = patterns.timezone.exec(timeString)
if (token) {
dateStrings.time = timeString.replace(token[1], '')
dateStrings.timezone = token[1]
} else {
dateStrings.time = timeString
}
}
return dateStrings
}
function parseYear(dateString, additionalDigits) {
var patternYYY = patterns.YYY[additionalDigits]
var patternYYYYY = patterns.YYYYY[additionalDigits]
var token
// YYYY or ±YYYYY
token = patterns.YYYY.exec(dateString) || patternYYYYY.exec(dateString)
if (token) {
var yearString = token[1]
return {
year: parseInt(yearString, 10),
restDateString: dateString.slice(yearString.length)
}
}
// YY or ±YYY
token = patterns.YY.exec(dateString) || patternYYY.exec(dateString)
if (token) {
var centuryString = token[1]
return {
year: parseInt(centuryString, 10) * 100,
restDateString: dateString.slice(centuryString.length)
}
}
// Invalid ISO-formatted year
return {
year: null
}
}
function parseDate(dateString, year) {
// Invalid ISO-formatted year
if (year === null) {
return null
}
var token
var date
var month
var week
// YYYY
if (dateString.length === 0) {
date = new Date(0)
date.setUTCFullYear(year)
return date
}
// YYYY-MM
token = patterns.MM.exec(dateString)
if (token) {
date = new Date(0)
month = parseInt(token[1], 10) - 1
if (!validateDate(year, month)) {
return new Date(NaN)
}
date.setUTCFullYear(year, month)
return date
}
// YYYY-DDD or YYYYDDD
token = patterns.DDD.exec(dateString)
if (token) {
date = new Date(0)
var dayOfYear = parseInt(token[1], 10)
if (!validateDayOfYearDate(year, dayOfYear)) {
return new Date(NaN)
}
date.setUTCFullYear(year, 0, dayOfYear)
return date
}
// YYYY-MM-DD or YYYYMMDD
token = patterns.MMDD.exec(dateString)
if (token) {
date = new Date(0)
month = parseInt(token[1], 10) - 1
var day = parseInt(token[2], 10)
if (!validateDate(year, month, day)) {
return new Date(NaN)
}
date.setUTCFullYear(year, month, day)
return date
}
// YYYY-Www or YYYYWww
token = patterns.Www.exec(dateString)
if (token) {
week = parseInt(token[1], 10) - 1
if (!validateWeekDate(year, week)) {
return new Date(NaN)
}
return dayOfISOWeekYear(year, week)
}
// YYYY-Www-D or YYYYWwwD
token = patterns.WwwD.exec(dateString)
if (token) {
week = parseInt(token[1], 10) - 1
var dayOfWeek = parseInt(token[2], 10) - 1
if (!validateWeekDate(year, week, dayOfWeek)) {
return new Date(NaN)
}
return dayOfISOWeekYear(year, week, dayOfWeek)
}
// Invalid ISO-formatted date
return null
}
function parseTime(timeString) {
var token
var hours
var minutes
// hh
token = patterns.HH.exec(timeString)
if (token) {
hours = parseFloat(token[1].replace(',', '.'))
if (!validateTime(hours)) {
return NaN
}
return (hours % 24) * MILLISECONDS_IN_HOUR
}
// hh:mm or hhmm
token = patterns.HHMM.exec(timeString)
if (token) {
hours = parseInt(token[1], 10)
minutes = parseFloat(token[2].replace(',', '.'))
if (!validateTime(hours, minutes)) {
return NaN
}
return (
(hours % 24) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE
)
}
// hh:mm:ss or hhmmss
token = patterns.HHMMSS.exec(timeString)
if (token) {
hours = parseInt(token[1], 10)
minutes = parseInt(token[2], 10)
var seconds = parseFloat(token[3].replace(',', '.'))
if (!validateTime(hours, minutes, seconds)) {
return NaN
}
return (
(hours % 24) * MILLISECONDS_IN_HOUR +
minutes * MILLISECONDS_IN_MINUTE +
seconds * 1000
)
}
// Invalid ISO-formatted time
return null
}
function parseTimezone(timezoneString) {
var token
var absoluteOffset
// Z
token = patterns.timezoneZ.exec(timezoneString)
if (token) {
return 0
}
var hours
// ±hh
token = patterns.timezoneHH.exec(timezoneString)
if (token) {
hours = parseInt(token[2], 10)
if (!validateTimezone(hours)) {
return NaN
}
absoluteOffset = hours * MILLISECONDS_IN_HOUR
return token[1] === '+' ? -absoluteOffset : absoluteOffset
}
// ±hh:mm or ±hhmm
token = patterns.timezoneHHMM.exec(timezoneString)
if (token) {
hours = parseInt(token[2], 10)
var minutes = parseInt(token[3], 10)
if (!validateTimezone(hours, minutes)) {
return NaN
}
absoluteOffset =
hours * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE
return token[1] === '+' ? -absoluteOffset : absoluteOffset
}
return 0
}
function dayOfISOWeekYear(isoWeekYear, week, day) {
week = week || 0
day = day || 0
var date = new Date(0)
date.setUTCFullYear(isoWeekYear, 0, 4)
var fourthOfJanuaryDay = date.getUTCDay() || 7
var diff = week * 7 + day + 1 - fourthOfJanuaryDay
date.setUTCDate(date.getUTCDate() + diff)
return date
}
// Validation functions
var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var DAYS_IN_MONTH_LEAP_YEAR = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
function isLeapYearIndex(year) {
return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
}
function validateDate(year, month, date) {
if (month < 0 || month > 11) {
return false
}
if (date != null) {
if (date < 1) {
return false
}
var isLeapYear = isLeapYearIndex(year)
if (isLeapYear && date > DAYS_IN_MONTH_LEAP_YEAR[month]) {
return false
}
if (!isLeapYear && date > DAYS_IN_MONTH[month]) {
return false
}
}
return true
}
function validateDayOfYearDate(year, dayOfYear) {
if (dayOfYear < 1) {
return false
}
var isLeapYear = isLeapYearIndex(year)
if (isLeapYear && dayOfYear > 366) {
return false
}
if (!isLeapYear && dayOfYear > 365) {
return false
}
return true
}
function validateWeekDate(year, week, day) {
if (week < 0 || week > 52) {
return false
}
if (day != null && (day < 0 || day > 6)) {
return false
}
return true
}
function validateTime(hours, minutes, seconds) {
if (hours != null && (hours < 0 || hours >= 25)) {
return false
}
if (minutes != null && (minutes < 0 || minutes >= 60)) {
return false
}
if (seconds != null && (seconds < 0 || seconds >= 60)) {
return false
}
return true
}
function validateTimezone(hours, minutes) {
if (minutes != null && (minutes < 0 || minutes > 59)) {
return false
}
return true
}