forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-formatting.js
228 lines (182 loc) · 6.12 KB
/
date-formatting.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Single Date Formatting
// -------------------------------------------------------------------------------------------------
// call this if you want Moment's original format method to be used
function oldMomentFormat(mom, formatStr) {
return oldMomentProto.format.call(mom, formatStr); // oldMomentProto defined in moment-ext.js
}
// Formats `date` with a Moment formatting string, but allow our non-zero areas and
// additional token.
function formatDate(date, formatStr) {
return formatDateWithChunks(date, getFormatStringChunks(formatStr));
}
function formatDateWithChunks(date, chunks) {
var s = '';
var i;
for (i=0; i<chunks.length; i++) {
s += formatDateWithChunk(date, chunks[i]);
}
return s;
}
// addition formatting tokens we want recognized
var tokenOverrides = {
t: function(date) { // "a" or "p"
return oldMomentFormat(date, 'a').charAt(0);
},
T: function(date) { // "A" or "P"
return oldMomentFormat(date, 'A').charAt(0);
}
};
function formatDateWithChunk(date, chunk) {
var token;
var maybeStr;
if (typeof chunk === 'string') { // a literal string
return chunk;
}
else if ((token = chunk.token)) { // a token, like "YYYY"
if (tokenOverrides[token]) {
return tokenOverrides[token](date); // use our custom token
}
return oldMomentFormat(date, token);
}
else if (chunk.maybe) { // a grouping of other chunks that must be non-zero
maybeStr = formatDateWithChunks(date, chunk.maybe);
if (maybeStr.match(/[1-9]/)) {
return maybeStr;
}
}
return '';
}
// Date Range Formatting
// -------------------------------------------------------------------------------------------------
// TODO: make it work with timezone offset
// Using a formatting string meant for a single date, generate a range string, like
// "Sep 2 - 9 2013", that intelligently inserts a separator where the dates differ.
// If the dates are the same as far as the format string is concerned, just return a single
// rendering of one date, without any separator.
function formatRange(date1, date2, formatStr, separator, isRTL) {
var localeData;
date1 = fc.moment.parseZone(date1);
date2 = fc.moment.parseZone(date2);
localeData = (date1.localeData || date1.lang).call(date1); // works with moment-pre-2.8
// Expand localized format strings, like "LL" -> "MMMM D YYYY"
formatStr = localeData.longDateFormat(formatStr) || formatStr;
// BTW, this is not important for `formatDate` because it is impossible to put custom tokens
// or non-zero areas in Moment's localized format strings.
separator = separator || ' - ';
return formatRangeWithChunks(
date1,
date2,
getFormatStringChunks(formatStr),
separator,
isRTL
);
}
fc.formatRange = formatRange; // expose
function formatRangeWithChunks(date1, date2, chunks, separator, isRTL) {
var chunkStr; // the rendering of the chunk
var leftI;
var leftStr = '';
var rightI;
var rightStr = '';
var middleI;
var middleStr1 = '';
var middleStr2 = '';
var middleStr = '';
// Start at the leftmost side of the formatting string and continue until you hit a token
// that is not the same between dates.
for (leftI=0; leftI<chunks.length; leftI++) {
chunkStr = formatSimilarChunk(date1, date2, chunks[leftI]);
if (chunkStr === false) {
break;
}
leftStr += chunkStr;
}
// Similarly, start at the rightmost side of the formatting string and move left
for (rightI=chunks.length-1; rightI>leftI; rightI--) {
chunkStr = formatSimilarChunk(date1, date2, chunks[rightI]);
if (chunkStr === false) {
break;
}
rightStr = chunkStr + rightStr;
}
// The area in the middle is different for both of the dates.
// Collect them distinctly so we can jam them together later.
for (middleI=leftI; middleI<=rightI; middleI++) {
middleStr1 += formatDateWithChunk(date1, chunks[middleI]);
middleStr2 += formatDateWithChunk(date2, chunks[middleI]);
}
if (middleStr1 || middleStr2) {
if (isRTL) {
middleStr = middleStr2 + separator + middleStr1;
}
else {
middleStr = middleStr1 + separator + middleStr2;
}
}
return leftStr + middleStr + rightStr;
}
var similarUnitMap = {
Y: 'year',
M: 'month',
D: 'day', // day of month
d: 'day', // day of week
// prevents a separator between anything time-related...
A: 'second', // AM/PM
a: 'second', // am/pm
T: 'second', // A/P
t: 'second', // a/p
H: 'second', // hour (24)
h: 'second', // hour (12)
m: 'second', // minute
s: 'second' // second
};
// TODO: week maybe?
// Given a formatting chunk, and given that both dates are similar in the regard the
// formatting chunk is concerned, format date1 against `chunk`. Otherwise, return `false`.
function formatSimilarChunk(date1, date2, chunk) {
var token;
var unit;
if (typeof chunk === 'string') { // a literal string
return chunk;
}
else if ((token = chunk.token)) {
unit = similarUnitMap[token.charAt(0)];
// are the dates the same for this unit of measurement?
if (unit && date1.isSame(date2, unit)) {
return oldMomentFormat(date1, token); // would be the same if we used `date2`
// BTW, don't support custom tokens
}
}
return false; // the chunk is NOT the same for the two dates
// BTW, don't support splitting on non-zero areas
}
// Chunking Utils
// -------------------------------------------------------------------------------------------------
var formatStringChunkCache = {};
function getFormatStringChunks(formatStr) {
if (formatStr in formatStringChunkCache) {
return formatStringChunkCache[formatStr];
}
return (formatStringChunkCache[formatStr] = chunkFormatString(formatStr));
}
// Break the formatting string into an array of chunks
function chunkFormatString(formatStr) {
var chunks = [];
var chunker = /\[([^\]]*)\]|\(([^\)]*)\)|(LTS|LT|(\w)\4*o?)|([^\w\[\(]+)/g; // TODO: more descrimination
var match;
while ((match = chunker.exec(formatStr))) {
if (match[1]) { // a literal string inside [ ... ]
chunks.push(match[1]);
}
else if (match[2]) { // non-zero formatting inside ( ... )
chunks.push({ maybe: chunkFormatString(match[2]) });
}
else if (match[3]) { // a formatting token
chunks.push({ token: match[3] });
}
else if (match[5]) { // an unenclosed literal string
chunks.push(match[5]);
}
}
return chunks;
}