-
Notifications
You must be signed in to change notification settings - Fork 2
/
dateString.js
77 lines (61 loc) · 1.76 KB
/
dateString.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
var hash = require('./hash')
var defaults = require('./utils/defaults')
var conj = require('./utils/conj')
var fit = require('./utils/fit')
var MIN_YEAR = 1980
var MAX_YEAR = 2019
function dateString(input, opts) {
var id = hash(input)
opts = opts || 0
var minDate, maxDate
// keep older dateString function for backwards compatibility
if (
!opts ||
(opts.minYear && opts.minYear) ||
(opts.minYear && !opts.max) ||
(!opts.min && opts.maxYear)
) {
return dateStringLegacy(input, opts)
}
if (opts.min) {
minDate = new Date(opts.min)
} else {
var minYear = defaults(opts.minYear, MIN_YEAR)
minDate = new Date(Date.UTC(minYear, 0, 1))
}
if (opts.max) {
maxDate = new Date(opts.max)
} else {
var maxYear = defaults(opts.maxYear, MAX_YEAR)
maxDate = new Date(Date.UTC(maxYear, 11, 31, 23, 59, 59, 999))
}
var min = minDate.getTime()
var max = maxDate.getTime()
var time = fit(id, min, max)
return new Date(time).toISOString()
}
function dateStringLegacy(input, opts) {
var id = hash(input)
opts = opts || 0
var minYear = defaults(opts.minYear, MIN_YEAR)
var maxYear = defaults(opts.maxYear, MAX_YEAR)
// for simplicity, use 28d as max regardless of month/year
var year = fit(id, minYear, maxYear)
var monthIndex = fit(id, 0, 11)
var day = fit(id, 1, 28)
var hour = fit(id, 0, 23)
var min = fit(id, 0, 59)
var millisec = fit(id, 0, 1000)
return new Date(
Date.UTC(year, monthIndex, day, hour, min, millisec)
).toISOString()
}
dateString.options = function dateStringOptions(opts) {
var base = this
dateStringFn.options = dateString.options
return dateStringFn
function dateStringFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}
module.exports = dateString