Skip to content

Commit 8fb4ca4

Browse files
committed
Checkpoint d3.time.format.
1 parent 197ff6b commit 8fb4ca4

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/time/format.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
d3.time.format = function(template) {
2+
var format = {};
3+
4+
format.format = function(date) {
5+
var string = [],
6+
i = -1,
7+
j = 0,
8+
n = template.length,
9+
c,
10+
f;
11+
while (++i < n) {
12+
if (template.charCodeAt(i) == 37) {
13+
string.push(
14+
template.substring(j, i),
15+
f = d3_time_formats(c = template.charAt(i + 1))
16+
? f(date) : c);
17+
j = i + 1;
18+
}
19+
}
20+
return string.join("");
21+
};
22+
23+
// TODO format.parse = function(string) {};
24+
25+
return format;
26+
};
27+
28+
var d3_time_zfill2 = d3.format("02d"),
29+
d3_time_zfill3 = d3.format("03d"),
30+
d3_time_zfill4 = d3.format("04d"),
31+
d3_time_sfill2 = d3.format(" 2d");
32+
33+
var d3_time_formats = {
34+
"a": function(d) { return d3_time_weekdays[d.getDay()].substring(0, 3); },
35+
"A": function(d) { return d3_time_weekdays[d.getDay()]; },
36+
"b": function(d) { return d3_time_months[d.getMonth()].substring(0, 3); },
37+
"B": function(d) { return d3_time_months[d.getMonth()]; },
38+
"c": d3_time_localeFull,
39+
"d": function(d) { return d3_time_zfill2(d.getDate()); },
40+
"e": function(d) { return d3_time_sfill2(d.getDate()); },
41+
"H": function(d) { return d3_time_zfill2(d.getHours()); },
42+
"I": function(d) { return d3_time_zfill2(d.getHours() % 12 || 12); },
43+
"j": d3_time_dayOfYear,
44+
"m": function(d) { return d3_time_zfill2(d.getMonth() + 1); },
45+
"M": function(d) { return d3_time_zfill2(d.getMinutes()); },
46+
"p": function(d) { return d.getHours() >= 12 ? "PM" : "AM"; },
47+
"S": function(d) { return d3_time_zfill2(d.getSeconds()); },
48+
"U": d3_time_weekNumberSunday,
49+
"w": function(d) { return d.getDay(); },
50+
"W": d3_time_weekNumberMonday,
51+
"x": d3_time_localeDate,
52+
"X": d3_time_localeTime,
53+
"y": function(d) { return d3_time_zfill2(d.getYear() % 100); },
54+
"Y": function(d) { return d3_time_zfill4(d.getFullYear() % 10000); },
55+
"Z": d3_time_zone,
56+
"%": function(d) { return "%"; }
57+
};
58+
59+
var d3_time_localeDate = d3.time.format("%m/%d/%y"),
60+
d3_time_localeTime = d3.time.format("%H:%M:%S"),
61+
d3_time_localeFull = d3.time.format("%a %b %e %H:%M:%S %Y");
62+
63+
var d3_time_weekdays = [
64+
"Sunday",
65+
"Monday",
66+
"Tuesday",
67+
"Wednesday",
68+
"Thursday",
69+
"Friday",
70+
"Saturday",
71+
"Sunday"
72+
];
73+
74+
var d3_time_months = [
75+
"January",
76+
"February",
77+
"March",
78+
"April",
79+
"May",
80+
"June",
81+
"July",
82+
"August",
83+
"September",
84+
"October",
85+
"November",
86+
"December"
87+
];
88+
89+
function d3_time_dayOfYear(d) {
90+
return d3_time_zfill3(1 + ~~((d - new Date(d.getFullYear(), 0, 1)) / 864e5));
91+
}
92+
93+
function d3_time_weekNumberSunday(d) {
94+
var d0 = new Date(d.getFullYear(), 0, 1);
95+
return d3_time_zfill2(~~(((d - d0) / 864e5 + d0.getDay()) / 7));
96+
}
97+
98+
function d3_time_weekNumberMonday(d) {
99+
var d0 = new Date(d.getFullYear(), 0, 1);
100+
return d3_time_zfill2(~~(((d - d0) / 864e5 + (d0.getDay() + 6) % 7) / 7));
101+
}
102+
103+
// TODO table of time zone offset names?
104+
function d3_time_zone(d) {
105+
var z = d.getTimezoneOffset(),
106+
zs = z < 0 ? "-" : "+",
107+
zh = ~~(Math.abs(z) / 60),
108+
zm = Math.abs(z) % 60;
109+
return zs + d3_time_zfill2(zh) + d3_time_zfill2(zm);
110+
}

src/time/time.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d3.time = {};

0 commit comments

Comments
 (0)