Skip to content

Commit

Permalink
fix: use utcFormat instead of timeFormat for non local scales
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Aug 18, 2022
1 parent a0297a2 commit 5af0eae
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/utils/buildAxis.linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
utcSecond,
} from 'd3-time'

import { timeFormat } from 'd3-time-format'
import { timeFormat, utcFormat } from 'd3-time-format'

import {
Axis,
Expand Down Expand Up @@ -206,35 +206,42 @@ function buildTimeAxis<TDatum>(
}
}

const resolvedTimeFormat = isLocal ? timeFormat : utcFormat
const trimFormat = (str: string) => str.trim().replace(/(,$|^,)/, '')

const contextFormat = (format: string, date: Date) => {
if (units.second(date) < date) {
// milliseconds - Do not remove any context
return timeFormat(format)(date)
return resolvedTimeFormat(format)(date)
}
if (units.minute(date) < date) {
// seconds - remove potential milliseconds
return timeFormat(trimFormat(format.replace(/\.%L.*?(\s|$)/, '')))(date)
return resolvedTimeFormat(
trimFormat(format.replace(/\.%L.*?(\s|$)/, ''))
)(date)
}
if (units.hour(date) < date) {
// minutes - remove potential seconds and milliseconds
return timeFormat(trimFormat(format.replace(/:%S.*?(\s|$)/, '')))(date)
return resolvedTimeFormat(trimFormat(format.replace(/:%S.*?(\s|$)/, '')))(
date
)
}
if (units.day(date) < date) {
// hours - remove potential minutes and seconds and milliseconds
return timeFormat(trimFormat(format.replace(/:%M.*?(\s|$)/, '')))(date)
return resolvedTimeFormat(trimFormat(format.replace(/:%M.*?(\s|$)/, '')))(
date
)
}
if (units.month(date) < date) {
// days - remove potential hours, minutes, seconds and milliseconds
return timeFormat(trimFormat(format.replace(/%-I.*/, '')))(date)
return resolvedTimeFormat(trimFormat(format.replace(/%-I.*/, '')))(date)
}
if (units.year(date) < date) {
// months - remove potential days, hours, minutes, seconds and milliseconds
return timeFormat(trimFormat(format.replace(/%-d.*/, '')))(date)
return resolvedTimeFormat(trimFormat(format.replace(/%-d.*/, '')))(date)
}
// years
return timeFormat('%Y')(date)
return resolvedTimeFormat('%Y')(date)
}

let shouldNice = options.shouldNice
Expand Down

0 comments on commit 5af0eae

Please sign in to comment.