-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathformatter.js
137 lines (122 loc) · 4.64 KB
/
formatter.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
// @flow
/**
* Copyright (c) 2017, Dirk-Jan Rutten
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
// Parses an RFC 3339 compliant time-string into a Date.
// It does this by combining the current date with the time-string
// to create a new Date instance.
//
// Example:
// Suppose the current date is 2016-01-01, then
// parseTime('11:00:12Z') parses to a Date corresponding to
// 2016-01-01T11:00:12Z.
export const parseTime = (time: string): Date => {
const currentDateString = new Date().toISOString()
return new Date(currentDateString.substr(0, currentDateString.indexOf('T') + 1) + time)
}
// Serializes a Date into an RFC 3339 compliant time-string in the
// format hh:mm:ss.sssZ.
export const serializeTime = (date: Date): string => {
const dateTimeString = date.toISOString()
return dateTimeString.substr(dateTimeString.indexOf('T') + 1)
}
// Serializes an RFC 3339 compliant time-string by shifting
// it to UTC.
export const serializeTimeString = (time: string): string => {
// If already formatted to UTC then return the time string
if (time.indexOf('Z') !== -1) {
return time
} else {
// These are time-strings with timezone information,
// these need to be shifted to UTC.
// Convert to UTC time string in
// format hh:mm:ss.sssZ.
const date = parseTime(time)
let timeUTC = serializeTime(date)
// Regex to look for fractional second part in time string
// such as 00:00:00.345+01:00
const regexFracSec = /\.\d{1,}/
// Retrieve the fractional second part of the time
// string if it exists.
const fractionalPart = time.match(regexFracSec)
if (fractionalPart == null) {
// These are time-strings without the fractional
// seconds. So we remove them from the UTC time-string.
timeUTC = timeUTC.replace(regexFracSec, '')
return timeUTC
} else {
// These are time-string with fractional seconds.
// Make sure that we inject the fractional
// second part back in. The `timeUTC` variable
// has millisecond precision, we may want more or less
// depending on the string that was passed.
timeUTC = timeUTC.replace(regexFracSec, fractionalPart[0])
return timeUTC
}
}
}
// Parses an RFC 3339 compliant date-string into a Date.
//
// Example:
// parseDate('2016-01-01') parses to a Date corresponding to
// 2016-01-01T00:00:00.000Z.
export const parseDate = (date: string): Date => {
return new Date(date)
}
// Serializes a Date into a RFC 3339 compliant date-string
// in the format YYYY-MM-DD.
export const serializeDate = (date: Date): string => {
return date.toISOString().split('T')[0]
}
// Parses an RFC 3339 compliant date-time-string into a Date.
export const parseDateTime = (dateTime: string): Date => {
return new Date(dateTime)
}
// Serializes a Date into an RFC 3339 compliant date-time-string
// in the format YYYY-MM-DDThh:mm:ss.sssZ.
export const serializeDateTime = (dateTime: Date): string => {
return dateTime.toISOString()
}
// Serializes an RFC 3339 compliant date-time-string by shifting
// it to UTC.
export const serializeDateTimeString = (dateTime: string): string => {
// If already formatted to UTC then return the time string
if (dateTime.indexOf('Z') !== -1) {
return dateTime
} else {
// These are time-strings with timezone information,
// these need to be shifted to UTC.
// Convert to UTC time string in
// format YYYY-MM-DDThh:mm:ss.sssZ.
let dateTimeUTC = (new Date(dateTime)).toISOString()
// Regex to look for fractional second part in date-time string
const regexFracSec = /\.\d{1,}/
// Retrieve the fractional second part of the time
// string if it exists.
const fractionalPart = dateTime.match(regexFracSec)
if (fractionalPart == null) {
// The date-time-string has no fractional part,
// so we remove it from the dateTimeUTC variable.
dateTimeUTC = dateTimeUTC.replace(regexFracSec, '')
return dateTimeUTC
} else {
// These are datetime-string with fractional seconds.
// Make sure that we inject the fractional
// second part back in. The `dateTimeUTC` variable
// has millisecond precision, we may want more or less
// depending on the string that was passed.
dateTimeUTC = dateTimeUTC.replace(regexFracSec, fractionalPart[0])
return dateTimeUTC
}
}
}
// Serializes a Unix timestamp to an RFC 3339 compliant date-time-string
// in the format YYYY-MM-DDThh:mm:ss.sssZ
export const serializeUnixTimestamp = (timestamp: number): string => {
return new Date(timestamp * 1000).toISOString()
}