-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3.time.js
368 lines (322 loc) · 10.2 KB
/
d3.time.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
(function(){d3.time = {};
var d3_time = Date;
d3.time.format = function(template) {
var n = template.length;
function format(date) {
var string = [],
i = -1,
j = 0,
c,
f;
while (++i < n) {
if (template.charCodeAt(i) == 37) {
string.push(
template.substring(j, i),
(f = d3_time_formats[c = template.charAt(++i)])
? f(date) : c);
j = i + 1;
}
}
string.push(template.substring(j, i));
return string.join("");
}
format.parse = function(string) {
var date = new d3_time(1900, 0, 1),
i = d3_time_parse(date, template, string, 0);
if (i != string.length) return null;
if (date.hour12) {
var hours = date.getHours() % 12;
date.setHours(date.hour12pm ? hours + 12 : hours);
}
delete date.hour12;
delete date.hour12pm;
return date;
};
format.toString = function() {
return template;
};
return format;
};
function d3_time_parse(date, template, string, j) {
var c,
p,
i = 0,
n = template.length,
m = string.length;
while (i < n) {
if (j >= m) return -1;
c = template.charCodeAt(i++);
if (c == 37) {
p = d3_time_parsers[template.charAt(i++)];
if (!p || ((j = p(date, string, j)) < 0)) return -1;
} else if (c != string.charCodeAt(j++)) {
return -1;
}
}
return j;
}
var d3_time_zfill2 = d3.format("02d"),
d3_time_zfill3 = d3.format("03d"),
d3_time_zfill4 = d3.format("04d"),
d3_time_sfill2 = d3.format("2d");
var d3_time_formats = {
a: function(d) { return d3_time_weekdays[d.getDay()].substring(0, 3); },
A: function(d) { return d3_time_weekdays[d.getDay()]; },
b: function(d) { return d3_time_months[d.getMonth()].substring(0, 3); },
B: function(d) { return d3_time_months[d.getMonth()]; },
c: d3.time.format("%a %b %e %H:%M:%S %Y"),
d: function(d) { return d3_time_zfill2(d.getDate()); },
e: function(d) { return d3_time_sfill2(d.getDate()); },
H: function(d) { return d3_time_zfill2(d.getHours()); },
I: function(d) { return d3_time_zfill2(d.getHours() % 12 || 12); },
j: d3_time_dayOfYear,
m: function(d) { return d3_time_zfill2(d.getMonth() + 1); },
M: function(d) { return d3_time_zfill2(d.getMinutes()); },
p: function(d) { return d.getHours() >= 12 ? "PM" : "AM"; },
S: function(d) { return d3_time_zfill2(d.getSeconds()); },
U: d3_time_weekNumberSunday,
w: function(d) { return d.getDay(); },
W: d3_time_weekNumberMonday,
x: d3.time.format("%m/%d/%y"),
X: d3.time.format("%H:%M:%S"),
y: function(d) { return d3_time_zfill2(d.getFullYear() % 100); },
Y: function(d) { return d3_time_zfill4(d.getFullYear() % 10000); },
Z: d3_time_zone,
"%": function(d) { return "%"; }
};
var d3_time_parsers = {
a: d3_time_parseWeekdayAbbrev,
A: d3_time_parseWeekday,
b: d3_time_parseMonthAbbrev,
B: d3_time_parseMonth,
c: d3_time_parseLocaleFull,
d: d3_time_parseDay,
e: d3_time_parseDay,
H: d3_time_parseHour24,
I: d3_time_parseHour12,
// j: function(d, s, i) { /*TODO day of year [001,366] */ return i; },
m: d3_time_parseMonthNumber,
M: d3_time_parseMinutes,
p: d3_time_parseAmPm,
S: d3_time_parseSeconds,
// U: function(d, s, i) { /*TODO week number (sunday) [00,53] */ return i; },
// w: function(d, s, i) { /*TODO weekday [0,6] */ return i; },
// W: function(d, s, i) { /*TODO week number (monday) [00,53] */ return i; },
x: d3_time_parseLocaleDate,
X: d3_time_parseLocaleTime,
y: d3_time_parseYear,
Y: d3_time_parseFullYear
// ,
// Z: function(d, s, i) { /*TODO time zone */ return i; },
// "%": function(d, s, i) { /*TODO literal % */ return i; }
};
// Note: weekday is validated, but does not set the date.
function d3_time_parseWeekdayAbbrev(date, string, i) {
return string.substring(i, i += 3).toLowerCase() in d3_time_weekdayAbbrevLookup ? i : -1;
}
var d3_time_weekdayAbbrevLookup = {
sun: 3,
mon: 3,
tue: 3,
wed: 3,
thu: 3,
fri: 3,
sat: 3
};
// Note: weekday is validated, but does not set the date.
function d3_time_parseWeekday(date, string, i) {
d3_time_weekdayRe.lastIndex = 0;
var n = d3_time_weekdayRe.exec(string.substring(i, i + 10));
return n ? i += n[0].length : -1;
}
var d3_time_weekdayRe = /^(?:Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)/ig;
var d3_time_weekdays = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
function d3_time_parseMonthAbbrev(date, string, i) {
var n = d3_time_monthAbbrevLookup[string.substring(i, i += 3).toLowerCase()];
return n == null ? -1 : (date.setMonth(n), i);
}
var d3_time_monthAbbrevLookup = {
jan: 0,
feb: 1,
mar: 2,
apr: 3,
may: 4,
jun: 5,
jul: 6,
aug: 7,
sep: 8,
oct: 9,
nov: 10,
dec: 11
};
function d3_time_parseMonth(date, string, i) {
d3_time_monthRe.lastIndex = 0;
var n = d3_time_monthRe.exec(string.substring(i, i + 12));
return n ? (date.setMonth(d3_time_monthLookup[n[0].toLowerCase()]), i += n[0].length) : -1;
}
var d3_time_monthRe = /^(?:January|February|March|April|May|June|July|August|September|October|November|December)/ig;
var d3_time_monthLookup = {
january: 0,
february: 1,
march: 2,
april: 3,
may: 4,
june: 5,
july: 6,
august: 7,
september: 8,
october: 9,
november: 10,
december: 11
};
var d3_time_months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
function d3_time_parseLocaleFull(date, string, i) {
return d3_time_parse(date, d3_time_formats.c.toString(), string, i);
}
function d3_time_parseLocaleDate(date, string, i) {
return d3_time_parse(date, d3_time_formats.x.toString(), string, i);
}
function d3_time_parseLocaleTime(date, string, i) {
return d3_time_parse(date, d3_time_formats.X.toString(), string, i);
}
function d3_time_parseFullYear(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 4));
return n ? (date.setFullYear(n[0]), i += n[0].length) : -1;
}
function d3_time_parseYear(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setFullYear(d3_time_century() + +n[0]), i += n[0].length) : -1;
}
function d3_time_century() {
return ~~(new Date().getFullYear() / 1000) * 1000;
}
function d3_time_parseMonthNumber(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMonth(n[0] - 1), i += n[0].length) : -1;
}
function d3_time_parseDay(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setDate(+n[0]), i += n[0].length) : -1;
}
// Note: we don't validate that the hour is in the range [0,23].
function d3_time_parseHour24(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setHours(+n[0]), i += n[0].length) : -1;
}
// Note: we don't validate that the hour is in the range [1,12].
function d3_time_parseHour12(date, string, i) {
date.hour12 = true;
return d3_time_parseHour24(date, string, i);
}
function d3_time_parseMinutes(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMinutes(+n[0]), i += n[0].length) : -1;
}
function d3_time_parseSeconds(date, string, i) {
d3_time_numberRe.lastIndex = 0;
var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setSeconds(+n[0]), i += n[0].length) : -1;
}
// Note: we don't look at the next directive.
var d3_time_numberRe = /\s*\d+/;
function d3_time_parseAmPm(date, string, i) {
var n = d3_time_amPmLookup[string.substring(i, i += 2).toLowerCase()];
return n == null ? -1 : (date.hour12pm = n, i);
}
var d3_time_amPmLookup = {
am: 0,
pm: 1
};
function d3_time_year(d) {
return new d3_time(d.getFullYear(), 0, 1);
}
function d3_time_dayOfYear(d) {
return d3_time_zfill3(1 + ~~((d - d3_time_year(d)) / 864e5));
}
function d3_time_weekNumberSunday(d) {
var d0 = d3_time_year(d);
return d3_time_zfill2(~~(((d - d0) / 864e5 + d0.getDay()) / 7));
}
function d3_time_weekNumberMonday(d) {
var d0 = d3_time_year(d);
return d3_time_zfill2(~~(((d - d0) / 864e5 + (d0.getDay() + 6) % 7) / 7));
}
// TODO table of time zone offset names?
function d3_time_zone(d) {
var z = d.getTimezoneOffset(),
zs = z > 0 ? "-" : "+",
zh = ~~(Math.abs(z) / 60),
zm = Math.abs(z) % 60;
return zs + d3_time_zfill2(zh) + d3_time_zfill2(zm);
}
d3.time.format.utc = function(template) {
var local = d3.time.format(template);
function format(date) {
var utc = new d3_time_format_utc();
utc._ = date;
return local(utc);
}
format.parse = function(string) {
try {
d3_time = d3_time_format_utc;
var date = local.parse(string);
return date && date._;
} finally {
d3_time = Date;
}
};
format.toString = local.toString;
return format;
};
function d3_time_format_utc() {
this._ = new Date(Date.UTC.apply(this, arguments));
}
d3_time_format_utc.prototype = {
getDate: function() { return this._.getUTCDate(); },
getDay: function() { return this._.getUTCDay(); },
getFullYear: function() { return this._.getUTCFullYear(); },
getHours: function() { return this._.getUTCHours(); },
getMilliseconds: function() { return this._.getUTCMilliseconds(); },
getMinutes: function() { return this._.getUTCMinutes(); },
getMonth: function() { return this._.getUTCMonth(); },
getSeconds: function() { return this._.getUTCSeconds(); },
getTimezoneOffset: function() { return 0; },
valueOf: function() { return this._.getTime(); },
setDate: function(x) { this._.setUTCDate(x); },
setDay: function(x) { this._.setUTCDay(x); },
setFullYear: function(x) { this._.setUTCFullYear(x); },
setHours: function(x) { this._.setUTCHours(x); },
setMilliseconds: function(x) { this._.setUTCMilliseconds(x); },
setMinutes: function(x) { this._.setUTCMinutes(x); },
setMonth: function(x) { this._.setUTCMonth(x); },
setSeconds: function(x) { this._.setUTCSeconds(x); }
};
d3.time.format.iso = d3.time.format.utc("%Y-%m-%dT%H:%M:%SZ");
})();