@@ -123,23 +123,39 @@ for (const [name, interval] of utcIntervals) {
123
123
interval [ intervalType ] = "utc" ;
124
124
}
125
125
126
+ const utcFormatIntervals = [
127
+ [ "year" , utcYear , "utc" ] ,
128
+ [ "month" , utcMonth , "utc" ] ,
129
+ [ "day" , unixDay , "utc" , 6 * durationMonth ] ,
130
+ [ "hour" , utcHour , "utc" , 3 * durationDay ] ,
131
+ [ "minute" , utcMinute , "utc" , 6 * durationHour ] ,
132
+ [ "second" , utcSecond , "utc" , 30 * durationMinute ]
133
+ ] ;
134
+
135
+ const timeFormatIntervals = [
136
+ [ "year" , timeYear , "time" ] ,
137
+ [ "month" , timeMonth , "time" ] ,
138
+ [ "day" , timeDay , "time" , 6 * durationMonth ] ,
139
+ [ "hour" , timeHour , "time" , 3 * durationDay ] ,
140
+ [ "minute" , timeMinute , "time" , 6 * durationHour ] ,
141
+ [ "second" , timeSecond , "time" , 30 * durationMinute ]
142
+ ] ;
143
+
126
144
// An interleaved array of UTC and local time intervals, in descending order
127
145
// from largest to smallest, used to determine the most specific standard time
128
146
// format for a given array of dates. This is a subset of the tick intervals
129
147
// listed above; we only need the breakpoints where the format changes.
130
148
const formatIntervals = [
131
- [ "year" , utcYear , "utc" ] ,
132
- [ "year" , timeYear , "time" ] ,
133
- [ "month" , utcMonth , "utc" ] ,
134
- [ "month" , timeMonth , "time" ] ,
135
- [ "day" , unixDay , "utc" , 6 * durationMonth ] ,
136
- [ "day" , timeDay , "time" , 6 * durationMonth ] ,
149
+ utcFormatIntervals [ 0 ] ,
150
+ timeFormatIntervals [ 0 ] ,
151
+ utcFormatIntervals [ 1 ] ,
152
+ timeFormatIntervals [ 1 ] ,
153
+ utcFormatIntervals [ 2 ] ,
154
+ timeFormatIntervals [ 2 ] ,
137
155
// Below day, local time typically has an hourly offset from UTC and hence the
138
156
// two are aligned and indistinguishable; therefore, we only consider UTC, and
139
157
// we don’t consider these if the domain only has a single value.
140
- [ "hour" , utcHour , "utc" , 3 * durationDay ] ,
141
- [ "minute" , utcMinute , "utc" , 6 * durationHour ] ,
142
- [ "second" , utcSecond , "utc" , 30 * durationMinute ]
158
+ ...utcFormatIntervals . slice ( 3 )
143
159
] ;
144
160
145
161
function parseInterval ( input , intervals , type ) {
@@ -238,16 +254,20 @@ function getTimeTemplate(anchor) {
238
254
: ( f1 , f2 ) => `${ f1 } \n${ f2 } ` ;
239
255
}
240
256
257
+ function getFormatIntervals ( type ) {
258
+ return type === "time" ? timeFormatIntervals : type === "utc" ? utcFormatIntervals : formatIntervals ;
259
+ }
260
+
241
261
// Given an array of dates, returns the largest compatible standard time
242
262
// interval. If no standard interval is compatible (other than milliseconds,
243
263
// which is universally compatible), returns undefined.
244
- export function inferTimeFormat ( dates , anchor ) {
264
+ export function inferTimeFormat ( type , dates , anchor ) {
245
265
const step = max ( pairs ( dates , ( a , b ) => Math . abs ( b - a ) ) ) ; // maybe undefined!
246
266
if ( step < 1000 ) return formatTimeInterval ( "millisecond" , "utc" , anchor ) ;
247
- for ( const [ name , interval , type , maxStep ] of formatIntervals ) {
267
+ for ( const [ name , interval , intervalType , maxStep ] of getFormatIntervals ( type ) ) {
248
268
if ( step > maxStep ) break ; // e.g., 52 weeks
249
269
if ( name === "hour" && ! step ) break ; // e.g., domain with a single date
250
- if ( dates . every ( ( d ) => interval . floor ( d ) >= d ) ) return formatTimeInterval ( name , type , anchor ) ;
270
+ if ( dates . every ( ( d ) => interval . floor ( d ) >= d ) ) return formatTimeInterval ( name , intervalType , anchor ) ;
251
271
}
252
272
}
253
273
0 commit comments