-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.js
56 lines (49 loc) · 1.49 KB
/
utils.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
'use strict'
const leftPad = require('left-pad')
/**
* Convert a JavaScript date into an `RFC3339Nano` formatted
* string.
*
* @param {Date} time
* @returns {string}
*/
module.exports.toRFC3339 = (time) => {
const year = time.getUTCFullYear()
const month = leftPad(time.getUTCMonth() + 1, 2, '0')
const day = leftPad(time.getUTCDate(), 2, '0')
const hour = leftPad(time.getUTCHours(), 2, '0')
const minute = leftPad(time.getUTCMinutes(), 2, '0')
const seconds = leftPad(time.getUTCSeconds(), 2, '0')
const milliseconds = time.getUTCMilliseconds()
const nanoseconds = milliseconds * 1000 * 1000
return `${year}-${month}-${day}T${hour}:${minute}:${seconds}.${nanoseconds}Z`
}
/**
* Parses a date string formatted as `RFC3339Nano` into a
* JavaScript Date object.
*
* @param {string} time
* @returns {Date}
*/
module.exports.parseRFC3339 = (time) => {
const rfc3339Matcher = new RegExp(
// 2006-01-02T
'(\\d{4})-(\\d{2})-(\\d{2})T' +
// 15:04:05
'(\\d{2}):(\\d{2}):(\\d{2})' +
// .999999999Z
'\\.(\\d+)Z'
)
const m = String(time).trim().match(rfc3339Matcher)
if (!m) {
throw new Error('Invalid format')
}
const year = parseInt(m[1], 10)
const month = parseInt(m[2], 10) - 1
const date = parseInt(m[3], 10)
const hour = parseInt(m[4], 10)
const minute = parseInt(m[5], 10)
const second = parseInt(m[6], 10)
const millisecond = parseInt(m[7].slice(0, -6), 10)
return new Date(Date.UTC(year, month, date, hour, minute, second, millisecond))
}