Skip to content

Commit 380688a

Browse files
committed
fix: more utc and formatting fixes
1 parent a10d6ce commit 380688a

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/utils/buildAxis.linear.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,61 +175,63 @@ function buildTimeAxis<TDatum>(
175175
if (minValue && maxValue) {
176176
if (
177177
units.year.count(minValue, maxValue) > 0 ||
178-
+units.year.floor(maxValue) < +units.year()
178+
units.year.floor(maxValue) < units.year()
179179
) {
180180
autoFormatStr = '%b %-d, %Y %-I:%M:%S.%L %p'
181181
} else if (
182182
units.month.count(minValue, maxValue) > 0 ||
183-
+units.month.floor(maxValue) < +units.month()
183+
units.month.floor(maxValue) < units.month()
184184
) {
185185
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
186186
} else if (
187187
units.day.count(minValue, maxValue) > 0 ||
188-
+units.day.floor(maxValue) < +units.day()
188+
units.day.floor(maxValue) < units.day()
189189
) {
190190
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
191191
} else if (
192192
units.hour.count(minValue, maxValue) > 0 ||
193-
+units.hour.floor(maxValue) < +units.hour()
193+
units.hour.floor(maxValue) < units.hour()
194194
) {
195195
autoFormatStr = '%-I:%M:%S.%L %p'
196196
} else if (
197197
units.minute.count(minValue, maxValue) > 0 ||
198-
+units.minute.floor(maxValue) < +units.minute()
198+
units.minute.floor(maxValue) < units.minute()
199199
) {
200200
autoFormatStr = '%-I:%M:%S.%L'
201201
} else if (
202202
units.second.count(minValue, maxValue) > 0 ||
203-
+units.second.floor(maxValue) < +units.second()
203+
units.second.floor(maxValue) < units.second()
204204
) {
205205
autoFormatStr = '%L'
206206
}
207207
}
208208

209+
const trimFormat = (str: string) => str.trim().replace(/(,$|^,)/, '')
210+
209211
const contextFormat = (format: string, date: Date) => {
210212
if (units.second(date) < date) {
211213
// milliseconds - Do not remove any context
212214
return timeFormat(format)(date)
213215
}
214216
if (units.minute(date) < date) {
215217
// seconds - remove potential milliseconds
216-
return timeFormat(format.replace(/\.%L.*?(\s|$)/, ' '))(date)
218+
return timeFormat(trimFormat(format.replace(/\.%L.*?(\s|$)/, '')))(date)
217219
}
218220
if (units.hour(date) < date) {
219221
// minutes - remove potential seconds and milliseconds
220-
return timeFormat(format.replace(/:%S.*?(\s|$)/, ' '))(date)
222+
return timeFormat(trimFormat(format.replace(/:%S.*?(\s|$)/, '')))(date)
221223
}
222224
if (units.day(date) < date) {
223225
// 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)
225227
}
226228
if (units.month(date) < date) {
227229
// days - remove potential hours, minutes, seconds and milliseconds
228-
return timeFormat(format.replace(/%I.*/, ''))(date)
230+
return timeFormat(trimFormat(format.replace(/%-I.*/, '')))(date)
229231
}
230232
if (units.year(date) < date) {
231233
// 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)
233235
}
234236
// years
235237
return timeFormat('%Y')(date)

0 commit comments

Comments
 (0)