@@ -175,61 +175,63 @@ function buildTimeAxis<TDatum>(
175
175
if ( minValue && maxValue ) {
176
176
if (
177
177
units . year . count ( minValue , maxValue ) > 0 ||
178
- + units . year . floor ( maxValue ) < + units . year ( )
178
+ units . year . floor ( maxValue ) < units . year ( )
179
179
) {
180
180
autoFormatStr = '%b %-d, %Y %-I:%M:%S.%L %p'
181
181
} else if (
182
182
units . month . count ( minValue , maxValue ) > 0 ||
183
- + units . month . floor ( maxValue ) < + units . month ( )
183
+ units . month . floor ( maxValue ) < units . month ( )
184
184
) {
185
185
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
186
186
} else if (
187
187
units . day . count ( minValue , maxValue ) > 0 ||
188
- + units . day . floor ( maxValue ) < + units . day ( )
188
+ units . day . floor ( maxValue ) < units . day ( )
189
189
) {
190
190
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
191
191
} else if (
192
192
units . hour . count ( minValue , maxValue ) > 0 ||
193
- + units . hour . floor ( maxValue ) < + units . hour ( )
193
+ units . hour . floor ( maxValue ) < units . hour ( )
194
194
) {
195
195
autoFormatStr = '%-I:%M:%S.%L %p'
196
196
} else if (
197
197
units . minute . count ( minValue , maxValue ) > 0 ||
198
- + units . minute . floor ( maxValue ) < + units . minute ( )
198
+ units . minute . floor ( maxValue ) < units . minute ( )
199
199
) {
200
200
autoFormatStr = '%-I:%M:%S.%L'
201
201
} else if (
202
202
units . second . count ( minValue , maxValue ) > 0 ||
203
- + units . second . floor ( maxValue ) < + units . second ( )
203
+ units . second . floor ( maxValue ) < units . second ( )
204
204
) {
205
205
autoFormatStr = '%L'
206
206
}
207
207
}
208
208
209
+ const trimFormat = ( str : string ) => str . trim ( ) . replace ( / ( , $ | ^ , ) / , '' )
210
+
209
211
const contextFormat = ( format : string , date : Date ) => {
210
212
if ( units . second ( date ) < date ) {
211
213
// milliseconds - Do not remove any context
212
214
return timeFormat ( format ) ( date )
213
215
}
214
216
if ( units . minute ( date ) < date ) {
215
217
// seconds - remove potential milliseconds
216
- return timeFormat ( format . replace ( / \. % L .* ?( \s | $ ) / , ' ' ) ) ( date )
218
+ return timeFormat ( trimFormat ( format . replace ( / \. % L .* ?( \s | $ ) / , '' ) ) ) ( date )
217
219
}
218
220
if ( units . hour ( date ) < date ) {
219
221
// minutes - remove potential seconds and milliseconds
220
- return timeFormat ( format . replace ( / : % S .* ?( \s | $ ) / , ' ' ) ) ( date )
222
+ return timeFormat ( trimFormat ( format . replace ( / : % S .* ?( \s | $ ) / , '' ) ) ) ( date )
221
223
}
222
224
if ( units . day ( date ) < date ) {
223
225
// hours - remove potential minutes and seconds and milliseconds
224
- return timeFormat ( format . replace ( / : % M .* ?( \s | $ ) / , ' ' ) ) ( date )
226
+ return timeFormat ( trimFormat ( format . replace ( / : % M .* ?( \s | $ ) / , '' ) ) ) ( date )
225
227
}
226
228
if ( units . month ( date ) < date ) {
227
229
// days - remove potential hours, minutes, seconds and milliseconds
228
- return timeFormat ( format . replace ( / % I .* / , '' ) ) ( date )
230
+ return timeFormat ( trimFormat ( format . replace ( / % - I .* / , '' ) ) ) ( date )
229
231
}
230
232
if ( units . year ( date ) < date ) {
231
233
// months - remove potential days, hours, minutes, seconds and milliseconds
232
- return timeFormat ( format . replace ( / % e , .* ?( \s | $ ) / , '' ) ) ( date )
234
+ return timeFormat ( trimFormat ( format . replace ( / % - d .* ?( \s | $ ) / , '' ) ) ) ( date )
233
235
}
234
236
// years
235
237
return timeFormat ( '%Y' ) ( date )
0 commit comments