Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: smarter date formatter #25404

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions superset-frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

// timezone for unit tests
process.env.TZ = 'America/New_York';

module.exports = {
testRegex:
'\\/superset-frontend\\/(spec|src|plugins|packages|tools)\\/.*(_spec|\\.test)\\.[jt]sx?$',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0,
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import finestTemporalGrain from './finestTemporalGrain';

test('finestTemporalGrain', () => {
const monthFormatter = finestTemporalGrain([
new Date('2003-01-01 00:00:00Z').getTime(),
new Date('2003-02-01 00:00:00Z').getTime(),
]);
expect(monthFormatter(new Date('2003-01-01 00:00:00Z').getTime())).toBe(
'2003-01-01',
);
expect(monthFormatter(new Date('2003-02-01 00:00:00Z').getTime())).toBe(
'2003-02-01',
);

const yearFormatter = finestTemporalGrain([
new Date('2003-01-01 00:00:00Z').getTime(),
new Date('2004-01-01 00:00:00Z').getTime(),
]);
expect(yearFormatter(new Date('2003-01-01 00:00:00Z').getTime())).toBe(
'2003',
);
expect(yearFormatter(new Date('2004-01-01 00:00:00Z').getTime())).toBe(
'2004',
);

const milliSecondFormatter = finestTemporalGrain([
new Date('2003-01-01 00:00:00Z').getTime(),
new Date('2003-04-05 06:07:08.123Z').getTime(),
]);
expect(milliSecondFormatter(new Date('2003-01-01 00:00:00Z').getTime())).toBe(
'2003-01-01 00:00:00.000',
);

const localTimeFormatter = finestTemporalGrain(
[
new Date('2003-01-01 00:00:00Z').getTime(),
new Date('2003-02-01 00:00:00Z').getTime(),
],
true,
);
expect(localTimeFormatter(new Date('2003-01-01 00:00:00Z').getTime())).toBe(
'2002-12-31 19:00',
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { utcFormat, timeFormat } from 'd3-time-format';
import { utcUtils, localTimeUtils } from '../utils/d3Time';
import TimeFormatter from '../TimeFormatter';

/*
* A formatter that examines all the values, and uses the finest temporal grain.
*/
export default function finestTemporalGrain(
values: any[],
useLocalTime = false,
) {
const format = useLocalTime ? timeFormat : utcFormat;

const formatMillisecond = format('%Y-%m-%d %H:%M:%S.%L');
const formatSecond = format('%Y-%m-%d %H:%M:%S');
const formatMinute = format('%Y-%m-%d %H:%M');
const formatHour = format('%Y-%m-%d %H:%M');
const formatDay = format('%Y-%m-%d');
const formatMonth = format('%Y-%m-%d');
const formatYear = format('%Y');

const {
hasMillisecond,
hasSecond,
hasMinute,
hasHour,
isNotFirstDayOfMonth,
isNotFirstMonth,
} = useLocalTime ? localTimeUtils : utcUtils;

let formatFunc = formatYear;
values.forEach((value: any) => {
if (formatFunc === formatYear && isNotFirstMonth(value)) {
formatFunc = formatMonth;
}
if (formatFunc === formatMonth && isNotFirstDayOfMonth(value)) {
formatFunc = formatDay;
}
if (formatFunc === formatDay && hasHour(value)) {
formatFunc = formatHour;
}
if (formatFunc === formatHour && hasMinute(value)) {
formatFunc = formatMinute;
}
if (formatFunc === formatMinute && hasSecond(value)) {
formatFunc = formatSecond;
}
if (formatFunc === formatSecond && hasMillisecond(value)) {
formatFunc = formatMillisecond;
}
});

return new TimeFormatter({
description:
'Use the finest grain in an array of dates to format all dates in the array',
formatFunc,
id: 'finest_temporal_grain',
label: 'Format temporal columns with the finest grain',
useLocalTime,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export { default as createMultiFormatter } from './factories/createMultiFormatte
export { default as smartDateFormatter } from './formatters/smartDate';
export { default as smartDateDetailedFormatter } from './formatters/smartDateDetailed';
export { default as smartDateVerboseFormatter } from './formatters/smartDateVerbose';
export { default as finestTemporalGrainFormatter } from './formatters/finestTemporalGrain';

export { default as normalizeTimestamp } from './utils/normalizeTimestamp';
export { default as denormalizeTimestamp } from './utils/denormalizeTimestamp';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
GenericDataType,
getColumnLabel,
JsonObject,
smartDateDetailedFormatter,
finestTemporalGrainFormatter,
t,
tn,
} from '@superset-ui/core';
Expand Down Expand Up @@ -117,9 +117,9 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
const labelFormatter = useMemo(
() =>
getDataRecordFormatter({
timeFormatter: smartDateDetailedFormatter,
timeFormatter: finestTemporalGrainFormatter(data.map(el => el.col)),
}),
[],
[data],
);

const updateDataMask = useCallback(
Expand Down
Loading