-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathunixFmt.js
179 lines (164 loc) · 4.24 KB
/
unixFmt.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
import { formatTimezone, zeroPad as pad } from '../../fns.js'
//parse this insane unix-time-templating thing, from the 19th century
//http://unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
//time-symbols we support
const mapping = {
G: (s) => s.era(),
GG: (s) => s.era(),
GGG: (s) => s.era(),
GGGG: (s) => (s.era() === 'AD' ? 'Anno Domini' : 'Before Christ'),
//year
y: (s) => s.year(),
yy: (s) => {
//last two chars
return pad(Number(String(s.year()).substr(2, 4)))
},
yyy: (s) => s.year(),
yyyy: (s) => s.year(),
yyyyy: (s) => '0' + s.year(),
// u: (s) => {},//extended non-gregorian years
//quarter
Q: (s) => s.quarter(),
QQ: (s) => s.quarter(),
QQQ: (s) => s.quarter(),
QQQQ: (s) => s.quarter(),
//month
M: (s) => s.month() + 1,
MM: (s) => pad(s.month() + 1),
MMM: (s) => s.format('month-short'),
MMMM: (s) => s.format('month'),
//week
w: (s) => s.week(),
ww: (s) => pad(s.week()),
//week of month
// W: (s) => s.week(),
//date of month
d: (s) => s.date(),
dd: (s) => pad(s.date()),
//date of year
D: (s) => s.dayOfYear(),
DD: (s) => pad(s.dayOfYear()),
DDD: (s) => pad(s.dayOfYear(), 3),
// F: (s) => {},//date of week in month
// g: (s) => {},//modified julian day
//day
E: (s) => s.format('day-short'),
EE: (s) => s.format('day-short'),
EEE: (s) => s.format('day-short'),
EEEE: (s) => s.format('day'),
EEEEE: (s) => s.format('day')[0],
e: (s) => s.day(),
ee: (s) => s.day(),
eee: (s) => s.format('day-short'),
eeee: (s) => s.format('day'),
eeeee: (s) => s.format('day')[0],
//am/pm
a: (s) => s.ampm().toUpperCase(),
aa: (s) => s.ampm().toUpperCase(),
aaa: (s) => s.ampm().toUpperCase(),
aaaa: (s) => s.ampm().toUpperCase(),
//hour
h: (s) => s.h12(),
hh: (s) => pad(s.h12()),
H: (s) => s.hour(),
HH: (s) => pad(s.hour()),
// j: (s) => {},//weird hour format
m: (s) => s.minute(),
mm: (s) => pad(s.minute()),
s: (s) => s.second(),
ss: (s) => pad(s.second()),
//milliseconds
SSS: (s) => pad(s.millisecond(), 3),
//milliseconds in the day
A: (s) => s.epoch - s.startOf('day').epoch,
//timezone
z: (s) => s.timezone().name,
zz: (s) => s.timezone().name,
zzz: (s) => s.timezone().name,
zzzz: (s) => s.timezone().name,
Z: (s) => formatTimezone(s.timezone().current.offset),
ZZ: (s) => formatTimezone(s.timezone().current.offset),
ZZZ: (s) => formatTimezone(s.timezone().current.offset),
ZZZZ: (s) => formatTimezone(s.timezone().current.offset, ':')
}
const addAlias = (char, to, n) => {
let name = char
let toName = to
for (let i = 0; i < n; i += 1) {
mapping[name] = mapping[toName]
name += char
toName += to
}
}
addAlias('q', 'Q', 4)
addAlias('L', 'M', 4)
addAlias('Y', 'y', 4)
addAlias('c', 'e', 4)
addAlias('k', 'H', 2)
addAlias('K', 'h', 2)
addAlias('S', 's', 2)
addAlias('v', 'z', 4)
addAlias('V', 'Z', 4)
// support unix-style escaping with ' character
const escapeChars = function (arr) {
for (let i = 0; i < arr.length; i += 1) {
if (arr[i] === `'`) {
// greedy-search for next apostrophe
for (let o = i + 1; o < arr.length; o += 1) {
if (arr[o]) {
arr[i] += arr[o]
}
if (arr[o] === `'`) {
arr[o] = null
break
}
arr[o] = null
}
}
}
return arr.filter((ch) => ch)
}
//combine consecutive chars, like 'yyyy' as one.
const combineRepeated = function (arr) {
for (let i = 0; i < arr.length; i += 1) {
let c = arr[i]
// greedy-forward
for (let o = i + 1; o < arr.length; o += 1) {
if (arr[o] === c) {
arr[i] += arr[o]
arr[o] = null
} else {
break
}
}
}
// '' means one apostrophe
arr = arr.filter((ch) => ch)
arr = arr.map((str) => {
if (str === `''`) {
str = `'`
}
return str
})
return arr
}
const unixFmt = (s, str) => {
let arr = str.split('')
// support character escaping
arr = escapeChars(arr)
//combine 'yyyy' as string.
arr = combineRepeated(arr)
return arr.reduce((txt, c) => {
if (mapping[c] !== undefined) {
txt += mapping[c](s) || ''
} else {
// 'unescape'
if (/^'.+'$/.test(c)) {
c = c.replace(/'/g, '')
}
txt += c
}
return txt
}, '')
}
export default unixFmt