Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Respect abbreviate date style and be defensive around missing settings (#68661)

Romeo Van Snick997b8d898e48
diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
index 4fa2950e385..cb4d9958d0b 100644
--- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
@@ -1494,3 +1494,85 @@ describe("issue #67903", () => {
cy.findByTestId("table-header").should("not.be.visible");
});
});
+
+describe("issue 68574", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+
+ const questionDetails: NativeQuestionDetails = {
+ name: "Question 1",
+ native: {
+ query: "SELECT * FROM ORDERS WHERE CREATED_AT > {{ start }}",
+ "template-tags": {
+ start: {
+ type: "date",
+ name: "start",
+ "display-name": "Start",
+ id: "1",
+ },
+ },
+ },
+ parameters: [
+ createMockParameter({
+ id: "1",
+ slug: "start",
+ required: true,
+ name: "Start",
+ type: "date/single",
+ target: ["variable", ["template-tag", "start"]],
+ }),
+ ],
+ };
+
+ H.createNativeQuestion(questionDetails, { wrapId: true });
+ });
+
+ it("should be possible to run a query for a empty required parameter without a default value (metabase#68574)", () => {
+ updateFormattingSettings({
+ date_style: "D MMMM, YYYY",
+ date_abbreviate: false,
+ });
+ visitQuestion("2024-01-01");
+ assertParameterFormat("1 January, 2024");
+
+ cy.log("change the date format");
+ updateFormattingSettings({
+ date_style: "dddd, MMMM D, YYYY",
+ date_abbreviate: false,
+ });
+ visitQuestion("2024-01-01");
+ assertParameterFormat("Monday, January 1, 2024");
+
+ cy.log("enable date abbreviation");
+ updateFormattingSettings({
+ date_style: "dddd, MMMM D, YYYY",
+ date_abbreviate: true,
+ });
+ visitQuestion("2024-01-01");
+ assertParameterFormat("Mon, Jan 1, 2024");
+
+ cy.log("even when the setting is unset, it should render a valid format");
+ updateFormattingSettings(undefined);
+ visitQuestion("2024-01-01");
+ assertParameterFormat("January 1, 2024");
+ });
+
+ function updateFormattingSettings(settings: any) {
+ H.updateSetting("custom-formatting", {
+ "type/Temporal": settings,
+ });
+ }
+
+ function visitQuestion(value: string) {
+ cy.get("@questionId").then((id) => {
+ cy.visit(`/question/${id}?start=${value}`);
+ });
+ }
+
+ function assertParameterFormat(value: string) {
+ cy.findByTestId("parameter-value-widget-target")
+ .should("be.visible")
+ .should("contain.text", value);
+ }
+});
diff --git a/frontend/src/metabase/parameters/utils/formatting.unit.spec.ts b/frontend/src/metabase/parameters/utils/formatting.unit.spec.ts
index d9f71b33dda..b12a428a8a6 100644
--- a/frontend/src/metabase/parameters/utils/formatting.unit.spec.ts
+++ b/frontend/src/metabase/parameters/utils/formatting.unit.spec.ts
@@ -286,4 +286,116 @@ describe("metabase/parameters/utils/formatting", () => {
},
);
});
+
+ describe("formatParameterValue with settings and abbreviated dates", () => {
+ const cases = [
+ {
+ type: "date/single",
+ value: "2018-01-01",
+ expected: "1 Jan, 2018",
+ },
+ {
+ type: "date/single",
+ value: "2018-01-01T12:30:00",
+ expected: "1 Jan, 2018 12:30 pm",
+ },
+ {
+ type: "date/range",
+ value: "1995-01-01~1995-01-10",
+ expected: "1 Jan, 1995 - 10 Jan, 1995",
+ },
+ {
+ type: "date/range",
+ value: "2018-01-01T12:30:00~2018-01-10",
+ expected: "1 Jan, 2018 12:30 pm - 10 Jan, 2018 12:00 am",
+ },
+ {
+ type: "date/range",
+ value: "2018-01-01~2018-01-10T08:15:00",
+ expected: "1 Jan, 2018 12:00 am - 10 Jan, 2018 08:15 am",
+ },
+ {
+ type: "date/range",
+ value: "2018-01-01T12:30:00~2018-01-10T08:15:00",
+ expected: "1 Jan, 2018 12:30 pm - 10 Jan, 2018 08:15 am",
+ },
+ {
+ type: "date/all-options",
+ value: "2018-01-01",
+ expected: "On 1 Jan, 2018",
+ },
+ {
+ type: "date/month-year",
+ value: "2018-01",
+ expected: "Jan 2018",
+ },
+ {
+ type: "date/quarter-year",
+ value: "Q1-2018",
+ expected: "Q1 2018",
+ },
+ {
+ type: "date/relative",
+ value: "past30days",
+ expected: "Previous 30 days",
+ },
+ {
+ type: "date/month-year",
+ value: "thisday",
+ expected: "Today",
+ },
+ {
+ type: "date/month-year",
+ value: "thisweek",
+ expected: "This week",
+ },
+ {
+ type: "date/month-year",
+ value: "past1days",
+ expected: "Yesterday",
+ },
+ {
+ type: "date/month-year",
+ value: "past1weeks",
+ expected: "Previous week",
+ },
+ {
+ type: "date/month-year",
+ value: "2023-10-02~2023-10-24",
+ expected: "2 Oct, 2023 - 24 Oct, 2023",
+ },
+ ];
+
+ const formattingSettings = {
+ "type/Temporal": {
+ date_style: "D MMMM, YYYY",
+ time_style: "hh:mm a",
+ date_abbreviate: true,
+ },
+ };
+
+ test.each(cases)(
+ "should format $type parameter",
+ ({ value, expected, ...parameterProps }) => {
+ const parameter = createMockUiParameter(parameterProps);
+ expect(
+ formatParameterValue(value, parameter, formattingSettings),
+ ).toEqual(expected);
+ },
+ );
+ });
+
+ describe("formatParameterValue with unset settings", () => {
+ it("should render a sensical value even if the date style is unset", () => {
+ const parameter = createMockUiParameter({ type: "date/month-year" });
+ const value = "2023-10-02~2023-10-24";
+ const expected = "October 2, 2023 - October 24, 2023";
+
+ expect(formatParameterValue(value, parameter)).toEqual(expected);
+ expect(formatParameterValue(value, parameter, undefined)).toEqual(
+ expected,
+ );
+ expect(formatParameterValue(value, parameter, {})).toEqual(expected);
+ });
+ });
});
diff --git a/frontend/src/metabase/querying/filters/utils/dates.ts b/frontend/src/metabase/querying/filters/utils/dates.ts
index 6a73fbd815e..225c4f4ddc8 100644
--- a/frontend/src/metabase/querying/filters/utils/dates.ts
+++ b/frontend/src/metabase/querying/filters/utils/dates.ts
@@ -316,7 +316,7 @@ export function getDateFilterDisplayName(
return t`Not empty`;
})
.with({ type: "month" }, ({ month, year }) => {
- return formatMonth(month, year);
+ return formatMonth(month, year, formattingSettings);
})
.with({ type: "quarter" }, ({ quarter, year }) => {
return formatQuarter(quarter, year);
@@ -327,21 +327,42 @@ export function getDateFilterDisplayName(
export function formatDate(
date: Date,
hasTime: boolean,
- formattingSettings: DateFormattingSettings = {
- date_style: "LL", // fall back to local date format
- time_style: DEFAULT_TIME_STYLE,
- },
+ formattingSettings: DateFormattingSettings = {},
) {
- const { date_style, time_style } = formattingSettings;
- const format = hasTime ? `${date_style} ${time_style}` : date_style;
+ const format = formattingSettingsToFormatString(formattingSettings, hasTime);
return dayjs(date).format(format);
}
-function formatMonth(month: number, year: number) {
+function formattingSettingsToFormatString(
+ formattingSettings: DateFormattingSettings = {},
+ hasTime: boolean = false,
+) {
+ const { date_style = "LL", time_style = DEFAULT_TIME_STYLE } =
+ formattingSettings;
+
+ const format = hasTime ? `${date_style} ${time_style}` : date_style;
+ return abbreviateFormat(format, formattingSettings);
+}
+
+function abbreviateFormat(
+ format: string,
+ formattingSettings: DateFormattingSettings = {},
+) {
+ if (!formattingSettings.date_abbreviate) {
+ return format;
+ }
+ return format.replace(/MMMM/, "MMM").replace(/dddd/, "ddd");
+}
+
+function formatMonth(
+ month: number,
+ year: number,
+ formattingSettings: DateFormattingSettings = {},
+) {
return dayjs()
.year(year)
.month(month - 1)
- .format("MMMM YYYY");
+ .format(abbreviateFormat("MMMM YYYY", formattingSettings));
}
function formatQuarter(quarter: number, year: number) {
diff --git a/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts b/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
index 30aab95ea61..a4c77e8e84b 100644
--- a/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
+++ b/frontend/src/metabase/querying/filters/utils/dates.unit.spec.ts
@@ -2,6 +2,7 @@ import { setLocalization } from "metabase/lib/i18n";
import type { DateFilterValue } from "metabase/querying/filters/types";
import * as Lib from "metabase-lib";
import { columnFinder, createQuery } from "metabase-lib/test-helpers";
+import type { DateFormattingSettings } from "metabase-types/api";
import {
formatDate,
@@ -148,6 +149,7 @@ type DateFilterDisplayNameCase = {
value: DateFilterValue;
displayName: string;
withPrefix?: boolean;
+ formattingSettings?: DateFormattingSettings;
};
describe("getDateFilterDisplayName", () => {
@@ -317,12 +319,50 @@ describe("getDateFilterDisplayName", () => {
},
displayName: "Not empty",
},
+ {
+ value: {
+ type: "specific",
+ operator: "=",
+ values: [new Date(2024, 2, 5)],
+ hasTime: false,
+ },
+ formattingSettings: {
+ date_style: "dddd MMMM D, YYYY",
+ },
+ displayName: "Tuesday March 5, 2024",
+ },
+ {
+ value: {
+ type: "specific",
+ operator: "=",
+ values: [new Date(2024, 2, 5)],
+ hasTime: false,
+ },
+ formattingSettings: {
+ date_style: "dddd MMMM D, YYYY",
+ date_abbreviate: true,
+ },
+ displayName: "Tue Mar 5, 2024",
+ },
+ {
+ value: {
+ type: "specific",
+ operator: "=",
+ values: [new Date(2024, 2, 5)],
+ hasTime: true,
+ },
+ formattingSettings: {
+ date_style: "dddd MMMM D, YYYY",
+ date_abbreviate: true,
+ },
+ displayName: "Tue Mar 5, 2024 12:00 AM",
+ },
])(
"should format a relative date filter",
- ({ value, displayName, withPrefix }) => {
- expect(getDateFilterDisplayName(value, { withPrefix })).toEqual(
- displayName,
- );
+ ({ value, displayName, withPrefix, formattingSettings }) => {
+ expect(
+ getDateFilterDisplayName(value, { withPrefix, formattingSettings }),
+ ).toEqual(displayName);
},
);
});