-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-to-03.js
175 lines (160 loc) · 5.06 KB
/
02-to-03.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
const v02_elem = document.getElementById('v02-in');
const v03_elem = document.getElementById('v03-out');
class Literal {
constructor(value) {
this.value = value;
}
to_v03_string() {
return this.value.replace('[', '[[').replace('\\', '\\\\');
}
}
class Specifier {
constructor(letter, padding = null) {
this.letter = letter;
this.padding = padding;
}
to_v03_string() {
if (this.letter === 'a') {
return '[weekday repr:short]';
}
if (this.letter === 'A') {
return '[weekday]';
}
if (this.letter === 'b') {
return '[month repr:short]';
}
if (this.letter === 'B') {
return '[month repr:long]';
}
if (this.letter === 'c') {
return parse_v02('%a %b %-d %-H:%M:%S %-Y').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'd') {
return `[day${this.padding}]`;
}
if (this.letter === 'D') {
return parse_v02('%-m/%d/%y').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'F') {
return parse_v02('%-Y-%m-%d').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'g') {
return `[year repr:last_two base:iso_week${this.padding}]`;
}
if (this.letter === 'G') {
return `[year base:iso_week${this.padding}]`;
}
if (this.letter === 'H') {
return `[hour${this.padding}]`;
}
if (this.letter === 'I') {
return `[hour repr:12${this.padding}]`;
}
if (this.letter === 'j') {
return `[ordinal${this.padding}]`;
}
if (this.letter === 'm') {
return '[month]';
}
if (this.letter === 'M') {
return `[minute${this.padding}]`;
}
if (this.letter === 'N') {
return '[subsecond digits:9]';
}
if (this.letter === 'p') {
return '[period]';
}
if (this.letter === 'P') {
return '[period case:upper]';
}
if (this.letter === 'r') {
return parse_v02('%-I:%M:%S %p').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'R') {
return parse_v02('%-H:%M').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'S') {
return `[second${this.padding}]`;
}
if (this.letter === 'T') {
return parse_v02('%-H:%M:%S').map(elem => elem.to_v03_string()).join('');
}
if (this.letter === 'u') {
return `[weekday repr:monday one_indexed:true${this.padding}]`;
}
if (this.letter === 'U') {
return `[week_number repr:sunday${this.padding}]`;
}
if (this.letter === 'V') {
return `[week_number repr:iso${this.padding}]`;
}
if (this.letter === 'w') {
return `[weekday repr:sunday one_indexed:false${this.padding}]`;
}
if (this.letter === 'W') {
return `[week_number repr:monday${this.padding}]`;
}
if (this.letter === 'y') {
return `[year repr:last_two${this.padding}]`;
}
if (this.letter === 'Y') {
return `[year${this.padding}]`;
}
if (this.letter === 'z') {
return '[offset_hour sign:mandatory][offset_minute]';
}
return '';
}
}
const padding = Object.freeze({
default: '',
none: ' padding:none',
space: ' padding:space',
zero: '',
});
function parse_v02(s) {
let items = [];
while (true) {
if (s.startsWith('%')) {
s = s.substring(1);
if (s === '') {
break;
}
if (s.startsWith('%')) {
items.push(new Literal('%'));
s = s.substring(1);
} else if (s.startsWith('-')) {
items.push(new Specifier(s[1], padding.none));
s = s.substring(2);
} else if (s.startsWith('_')) {
items.push(new Specifier(s[1], padding.space));
s = s.substring(2);
} else if (s.startsWith('0')) {
items.push(new Specifier(s[1], padding.zero));
s = s.substring(2);
} else {
items.push(new Specifier(s[0], padding.default));
s = s.substring(1);
}
} else {
let index = 0;
while (s[index] !== '%' && s[index] !== undefined) {
index++;
}
if (s[index - 1] === undefined) {
break;
}
items.push(new Literal(s.substring(0, index)));
s = s.substring(index);
}
}
return items;
}
function convert() {
const v02_str = v02_elem.value;
const items = parse_v02(v02_str);
const v03_str = items.flatMap(item => item.to_v03_string()).join('');
v03_elem.value = v03_str;
}
v02_elem.addEventListener('keyup', convert);