@@ -18,6 +18,12 @@ import {
18
18
timeDay ,
19
19
timeMonth ,
20
20
timeYear ,
21
+ utcYear ,
22
+ utcMonth ,
23
+ utcDay ,
24
+ utcHour ,
25
+ utcMinute ,
26
+ utcSecond ,
21
27
} from 'd3-time'
22
28
23
29
import { timeFormat } from 'd3-time-format'
@@ -118,6 +124,7 @@ function buildTimeAxis<TDatum>(
118
124
range : [ number , number ] ,
119
125
outerRange : [ number , number ]
120
126
) : AxisTime < TDatum > {
127
+ const isLocal = options . scaleType !== 'localTime'
121
128
const scaleFn = options . scaleType === 'localTime' ? scaleTime : scaleUtc
122
129
123
130
let isInvalid = false
@@ -147,62 +154,80 @@ function buildTimeAxis<TDatum>(
147
154
148
155
let autoFormatStr : string
149
156
157
+ const units = isLocal
158
+ ? {
159
+ year : timeYear ,
160
+ month : timeMonth ,
161
+ day : timeDay ,
162
+ hour : timeHour ,
163
+ minute : timeMinute ,
164
+ second : timeSecond ,
165
+ }
166
+ : {
167
+ year : utcYear ,
168
+ month : utcMonth ,
169
+ day : utcDay ,
170
+ hour : utcHour ,
171
+ minute : utcMinute ,
172
+ second : utcSecond ,
173
+ }
174
+
150
175
if ( minValue && maxValue ) {
151
176
if (
152
- timeYear . count ( minValue , maxValue ) > 0 ||
153
- + timeYear . floor ( maxValue ) < + timeYear ( )
177
+ units . year . count ( minValue , maxValue ) > 0 ||
178
+ + units . year . floor ( maxValue ) < + units . year ( )
154
179
) {
155
180
autoFormatStr = '%b %-d, %Y %-I:%M:%S.%L %p'
156
181
} else if (
157
- timeMonth . count ( minValue , maxValue ) > 0 ||
158
- + timeMonth . floor ( maxValue ) < + timeMonth ( )
182
+ units . month . count ( minValue , maxValue ) > 0 ||
183
+ + units . month . floor ( maxValue ) < + units . month ( )
159
184
) {
160
185
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
161
186
} else if (
162
- timeDay . count ( minValue , maxValue ) > 0 ||
163
- + timeDay . floor ( maxValue ) < + timeDay ( )
187
+ units . day . count ( minValue , maxValue ) > 0 ||
188
+ + units . day . floor ( maxValue ) < + units . day ( )
164
189
) {
165
190
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
166
191
} else if (
167
- timeHour . count ( minValue , maxValue ) > 0 ||
168
- + timeHour . floor ( maxValue ) < + timeHour ( )
192
+ units . hour . count ( minValue , maxValue ) > 0 ||
193
+ + units . hour . floor ( maxValue ) < + units . hour ( )
169
194
) {
170
195
autoFormatStr = '%-I:%M:%S.%L %p'
171
196
} else if (
172
- timeMinute . count ( minValue , maxValue ) > 0 ||
173
- + timeMinute . floor ( maxValue ) < + timeMinute ( )
197
+ units . minute . count ( minValue , maxValue ) > 0 ||
198
+ + units . minute . floor ( maxValue ) < + units . minute ( )
174
199
) {
175
200
autoFormatStr = '%-I:%M:%S.%L'
176
201
} else if (
177
- timeSecond . count ( minValue , maxValue ) > 0 ||
178
- + timeSecond . floor ( maxValue ) < + timeSecond ( )
202
+ units . second . count ( minValue , maxValue ) > 0 ||
203
+ + units . second . floor ( maxValue ) < + units . second ( )
179
204
) {
180
205
autoFormatStr = '%L'
181
206
}
182
207
}
183
208
184
209
const contextFormat = ( format : string , date : Date ) => {
185
- if ( timeSecond ( date ) < date ) {
210
+ if ( units . second ( date ) < date ) {
186
211
// milliseconds - Do not remove any context
187
212
return timeFormat ( format ) ( date )
188
213
}
189
- if ( timeMinute ( date ) < date ) {
214
+ if ( units . minute ( date ) < date ) {
190
215
// seconds - remove potential milliseconds
191
216
return timeFormat ( format . replace ( / \. % L .* ?( \s | $ ) / , ' ' ) ) ( date )
192
217
}
193
- if ( timeHour ( date ) < date ) {
218
+ if ( units . hour ( date ) < date ) {
194
219
// minutes - remove potential seconds and milliseconds
195
220
return timeFormat ( format . replace ( / : % S .* ?( \s | $ ) / , ' ' ) ) ( date )
196
221
}
197
- if ( timeDay ( date ) < date ) {
222
+ if ( units . day ( date ) < date ) {
198
223
// hours - remove potential minutes and seconds and milliseconds
199
224
return timeFormat ( format . replace ( / : % M .* ?( \s | $ ) / , ' ' ) ) ( date )
200
225
}
201
- if ( timeMonth ( date ) < date ) {
226
+ if ( units . month ( date ) < date ) {
202
227
// days - remove potential hours, minutes, seconds and milliseconds
203
228
return timeFormat ( format . replace ( / % I .* / , '' ) ) ( date )
204
229
}
205
- if ( timeYear ( date ) < date ) {
230
+ if ( units . year ( date ) < date ) {
206
231
// months - remove potential days, hours, minutes, seconds and milliseconds
207
232
return timeFormat ( format . replace ( / % e , .* ?( \s | $ ) / , '' ) ) ( date )
208
233
}
0 commit comments