Skip to content

Commit 02241ef

Browse files
committed
Add timeFormat.multi.
Given an array of format strings and corresponding predicate functions, returns a time format function that tests the input date against each predicate function, using the first format that returns true.
1 parent d158235 commit 02241ef

5 files changed

Lines changed: 74 additions & 66 deletions

File tree

d3.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,7 @@ d3 = function() {
24992499
format.toString = local.toString;
25002500
return format;
25012501
};
2502+
d3_time_format.multi = d3_time_format.utc.multi = d3_time_formatMulti;
25022503
var d3_time_periodLookup = d3.map(), d3_time_dayRe = d3_time_formatRe(locale_days), d3_time_dayLookup = d3_time_formatLookup(locale_days), d3_time_dayAbbrevRe = d3_time_formatRe(locale_shortDays), d3_time_dayAbbrevLookup = d3_time_formatLookup(locale_shortDays), d3_time_monthRe = d3_time_formatRe(locale_months), d3_time_monthLookup = d3_time_formatLookup(locale_months), d3_time_monthAbbrevRe = d3_time_formatRe(locale_shortMonths), d3_time_monthAbbrevLookup = d3_time_formatLookup(locale_shortMonths);
25032504
locale_periods.forEach(function(p, i) {
25042505
d3_time_periodLookup.set(p.toLowerCase(), i);
@@ -2723,6 +2724,15 @@ d3 = function() {
27232724
var n = d3_time_percentRe.exec(string.substring(i, i + 1));
27242725
return n ? i + n[0].length : -1;
27252726
}
2727+
function d3_time_formatMulti(formats) {
2728+
var n = formats.length, i = -1;
2729+
while (++i < n) formats[i][0] = this(formats[i][0]);
2730+
return function(date) {
2731+
var i = 0, f = formats[i];
2732+
while (!f[1](date)) f = formats[++i];
2733+
return f[0](date);
2734+
};
2735+
}
27262736
d3.locale = function(locale) {
27272737
return {
27282738
numberFormat: d3_locale_numberFormat(locale),
@@ -9229,21 +9239,21 @@ d3 = function() {
92299239
}
92309240
var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ];
92319241
var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ];
9232-
var d3_time_scaleLocalFormat = d3_time_scaleMultiFormat([ [ d3_time_format("%Y"), d3_true ], [ d3_time_format("%B"), function(d) {
9233-
return d.getMonth();
9234-
} ], [ d3_time_format("%b %d"), function(d) {
9235-
return d.getDate() != 1;
9236-
} ], [ d3_time_format("%a %d"), function(d) {
9237-
return d.getDay() && d.getDate() != 1;
9238-
} ], [ d3_time_format("%I %p"), function(d) {
9239-
return d.getHours();
9240-
} ], [ d3_time_format("%I:%M"), function(d) {
9241-
return d.getMinutes();
9242-
} ], [ d3_time_format(":%S"), function(d) {
9243-
return d.getSeconds();
9244-
} ], [ d3_time_format(".%L"), function(d) {
9242+
var d3_time_scaleLocalFormat = d3_time_format.multi([ [ ".%L", function(d) {
92459243
return d.getMilliseconds();
9246-
} ] ]);
9244+
} ], [ ":%S", function(d) {
9245+
return d.getSeconds();
9246+
} ], [ "%I:%M", function(d) {
9247+
return d.getMinutes();
9248+
} ], [ "%I %p", function(d) {
9249+
return d.getHours();
9250+
} ], [ "%a %d", function(d) {
9251+
return d.getDay() && d.getDate() != 1;
9252+
} ], [ "%b %d", function(d) {
9253+
return d.getDate() != 1;
9254+
} ], [ "%B", function(d) {
9255+
return d.getMonth();
9256+
} ], [ "%Y", d3_true ] ]);
92479257
var d3_time_scaleMilliseconds = {
92489258
range: function(start, stop, step) {
92499259
return d3.range(+start, +stop, step).map(d3_time_scaleDate);
@@ -9255,31 +9265,24 @@ d3 = function() {
92559265
d3_time.scale = function() {
92569266
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
92579267
};
9258-
function d3_time_scaleMultiFormat(formats) {
9259-
return function(date) {
9260-
var i = formats.length - 1, f = formats[i];
9261-
while (!f[1](date)) f = formats[--i];
9262-
return f[0](date);
9263-
};
9264-
}
92659268
var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) {
92669269
return [ m[0].utc, m[1] ];
92679270
});
9268-
var d3_time_scaleUtcFormat = d3_time_scaleMultiFormat([ [ d3_time_formatUtc("%Y"), d3_true ], [ d3_time_formatUtc("%B"), function(d) {
9269-
return d.getUTCMonth();
9270-
} ], [ d3_time_formatUtc("%b %d"), function(d) {
9271-
return d.getUTCDate() != 1;
9272-
} ], [ d3_time_formatUtc("%a %d"), function(d) {
9273-
return d.getUTCDay() && d.getUTCDate() != 1;
9274-
} ], [ d3_time_formatUtc("%I %p"), function(d) {
9275-
return d.getUTCHours();
9276-
} ], [ d3_time_formatUtc("%I:%M"), function(d) {
9277-
return d.getUTCMinutes();
9278-
} ], [ d3_time_formatUtc(":%S"), function(d) {
9279-
return d.getUTCSeconds();
9280-
} ], [ d3_time_formatUtc(".%L"), function(d) {
9271+
var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([ [ ".%L", function(d) {
92819272
return d.getUTCMilliseconds();
9282-
} ] ]);
9273+
} ], [ ":%S", function(d) {
9274+
return d.getUTCSeconds();
9275+
} ], [ "%I:%M", function(d) {
9276+
return d.getUTCMinutes();
9277+
} ], [ "%I %p", function(d) {
9278+
return d.getUTCHours();
9279+
} ], [ "%a %d", function(d) {
9280+
return d.getUTCDay() && d.getUTCDate() != 1;
9281+
} ], [ "%b %d", function(d) {
9282+
return d.getUTCDate() != 1;
9283+
} ], [ "%B", function(d) {
9284+
return d.getUTCMonth();
9285+
} ], [ "%Y", d3_true ] ]);
92839286
d3_time_scaleUtcMethods.year = d3_time.year.utc;
92849287
d3_time.scale.utc = function() {
92859288
return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat);

d3.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locale/time-format.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ function d3_locale_timeFormat(locale) {
124124
return format;
125125
};
126126

127+
d3_time_format.multi =
128+
d3_time_format.utc.multi = d3_time_formatMulti;
129+
127130
var d3_time_periodLookup = d3.map(),
128131
d3_time_dayRe = d3_time_formatRe(locale_days),
129132
d3_time_dayLookup = d3_time_formatLookup(locale_days),
@@ -354,3 +357,13 @@ function d3_time_parseLiteralPercent(date, string, i) {
354357
var n = d3_time_percentRe.exec(string.substring(i, i + 1));
355358
return n ? i + n[0].length : -1;
356359
}
360+
361+
function d3_time_formatMulti(formats) {
362+
var n = formats.length, i = -1;
363+
while (++i < n) formats[i][0] = this(formats[i][0]);
364+
return function(date) {
365+
var i = 0, f = formats[i];
366+
while (!f[1](date)) f = formats[++i];
367+
return f[0](date);
368+
};
369+
}

src/time/scale-utc.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) {
77
return [m[0].utc, m[1]];
88
});
99

10-
var d3_time_scaleUtcFormat = d3_time_scaleMultiFormat([
11-
[d3_time_formatUtc("%Y"), d3_true],
12-
[d3_time_formatUtc("%B"), function(d) { return d.getUTCMonth(); }],
13-
[d3_time_formatUtc("%b %d"), function(d) { return d.getUTCDate() != 1; }],
14-
[d3_time_formatUtc("%a %d"), function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
15-
[d3_time_formatUtc("%I %p"), function(d) { return d.getUTCHours(); }],
16-
[d3_time_formatUtc("%I:%M"), function(d) { return d.getUTCMinutes(); }],
17-
[d3_time_formatUtc(":%S"), function(d) { return d.getUTCSeconds(); }],
18-
[d3_time_formatUtc(".%L"), function(d) { return d.getUTCMilliseconds(); }]
10+
var d3_time_scaleUtcFormat = d3_time_formatUtc.multi([
11+
[".%L", function(d) { return d.getUTCMilliseconds(); }],
12+
[":%S", function(d) { return d.getUTCSeconds(); }],
13+
["%I:%M", function(d) { return d.getUTCMinutes(); }],
14+
["%I %p", function(d) { return d.getUTCHours(); }],
15+
["%a %d", function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
16+
["%b %d", function(d) { return d.getUTCDate() != 1; }],
17+
["%B", function(d) { return d.getUTCMonth(); }],
18+
["%Y", d3_true]
1919
]);
2020

2121
d3_time_scaleUtcMethods.year = d3_time.year.utc;

src/time/scale.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ var d3_time_scaleLocalMethods = [
132132
[d3_time.year, 1]
133133
];
134134

135-
var d3_time_scaleLocalFormat = d3_time_scaleMultiFormat([
136-
[d3_time_format("%Y"), d3_true],
137-
[d3_time_format("%B"), function(d) { return d.getMonth(); }],
138-
[d3_time_format("%b %d"), function(d) { return d.getDate() != 1; }],
139-
[d3_time_format("%a %d"), function(d) { return d.getDay() && d.getDate() != 1; }],
140-
[d3_time_format("%I %p"), function(d) { return d.getHours(); }],
141-
[d3_time_format("%I:%M"), function(d) { return d.getMinutes(); }],
142-
[d3_time_format(":%S"), function(d) { return d.getSeconds(); }],
143-
[d3_time_format(".%L"), function(d) { return d.getMilliseconds(); }]
135+
var d3_time_scaleLocalFormat = d3_time_format.multi([
136+
[".%L", function(d) { return d.getMilliseconds(); }],
137+
[":%S", function(d) { return d.getSeconds(); }],
138+
["%I:%M", function(d) { return d.getMinutes(); }],
139+
["%I %p", function(d) { return d.getHours(); }],
140+
["%a %d", function(d) { return d.getDay() && d.getDate() != 1; }],
141+
["%b %d", function(d) { return d.getDate() != 1; }],
142+
["%B", function(d) { return d.getMonth(); }],
143+
["%Y", d3_true]
144144
]);
145145

146146
var d3_time_scaleMilliseconds = {
@@ -156,11 +156,3 @@ d3_time_scaleLocalMethods.year = d3_time.year;
156156
d3_time.scale = function() {
157157
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
158158
};
159-
160-
function d3_time_scaleMultiFormat(formats) {
161-
return function(date) {
162-
var i = formats.length - 1, f = formats[i];
163-
while (!f[1](date)) f = formats[--i];
164-
return f[0](date);
165-
};
166-
}

0 commit comments

Comments
 (0)