/
index.js
146 lines (126 loc) · 4.64 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
import tzIntlTimeZoneName from '../../_lib/tzIntlTimeZoneName/index.js'
import tzParseTimezone from '../../_lib/tzParseTimezone/index.js'
var MILLISECONDS_IN_MINUTE = 60 * 1000
var formatters = {
// Timezone (ISO-8601. If offset is 0, output is always `'Z'`)
X: function (date, token, localize, options) {
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)
if (timezoneOffset === 0) {
return 'Z'
}
switch (token) {
// Hours and optional minutes
case 'X':
return formatTimezoneWithOptionalMinutes(timezoneOffset)
// Hours, minutes and optional seconds without `:` delimeter
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
// so this token always has the same output as `XX`
case 'XXXX':
case 'XX': // Hours and minutes without `:` delimeter
return formatTimezone(timezoneOffset)
// Hours, minutes and optional seconds with `:` delimeter
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
// so this token always has the same output as `XXX`
case 'XXXXX':
case 'XXX': // Hours and minutes with `:` delimeter
default:
return formatTimezone(timezoneOffset, ':')
}
},
// Timezone (ISO-8601. If offset is 0, output is `'+00:00'` or equivalent)
x: function (date, token, localize, options) {
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)
switch (token) {
// Hours and optional minutes
case 'x':
return formatTimezoneWithOptionalMinutes(timezoneOffset)
// Hours, minutes and optional seconds without `:` delimeter
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
// so this token always has the same output as `xx`
case 'xxxx':
case 'xx': // Hours and minutes without `:` delimeter
return formatTimezone(timezoneOffset)
// Hours, minutes and optional seconds with `:` delimeter
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
// so this token always has the same output as `xxx`
case 'xxxxx':
case 'xxx': // Hours and minutes with `:` delimeter
default:
return formatTimezone(timezoneOffset, ':')
}
},
// Timezone (GMT)
O: function (date, token, localize, options) {
var timezoneOffset = getTimeZoneOffset(options.timeZone, options._originalDate || date)
switch (token) {
// Short
case 'O':
case 'OO':
case 'OOO':
return 'GMT' + formatTimezoneShort(timezoneOffset, ':')
// Long
case 'OOOO':
default:
return 'GMT' + formatTimezone(timezoneOffset, ':')
}
},
// Timezone (specific non-location)
z: function (date, token, localize, options) {
var originalDate = options._originalDate || date
switch (token) {
// Short
case 'z':
case 'zz':
case 'zzz':
return tzIntlTimeZoneName('short', originalDate, options)
// Long
case 'zzzz':
default:
return tzIntlTimeZoneName('long', originalDate, options)
}
},
}
function getTimeZoneOffset(timeZone, originalDate) {
var timeZoneOffset = timeZone
? tzParseTimezone(timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()
if (Number.isNaN(timeZoneOffset)) {
throw new RangeError('Invalid time zone specified: ' + timeZone)
}
return timeZoneOffset
}
function addLeadingZeros(number, targetLength) {
var sign = number < 0 ? '-' : ''
var output = Math.abs(number).toString()
while (output.length < targetLength) {
output = '0' + output
}
return sign + output
}
function formatTimezone(offset, dirtyDelimeter) {
var delimeter = dirtyDelimeter || ''
var sign = offset > 0 ? '-' : '+'
var absOffset = Math.abs(offset)
var hours = addLeadingZeros(Math.floor(absOffset / 60), 2)
var minutes = addLeadingZeros(Math.floor(absOffset % 60), 2)
return sign + hours + delimeter + minutes
}
function formatTimezoneWithOptionalMinutes(offset, dirtyDelimeter) {
if (offset % 60 === 0) {
var sign = offset > 0 ? '-' : '+'
return sign + addLeadingZeros(Math.abs(offset) / 60, 2)
}
return formatTimezone(offset, dirtyDelimeter)
}
function formatTimezoneShort(offset, dirtyDelimeter) {
var sign = offset > 0 ? '-' : '+'
var absOffset = Math.abs(offset)
var hours = Math.floor(absOffset / 60)
var minutes = absOffset % 60
if (minutes === 0) {
return sign + String(hours)
}
var delimeter = dirtyDelimeter || ''
return sign + String(hours) + delimeter + addLeadingZeros(minutes, 2)
}
export default formatters