The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Format datetimes with user locale (#61680)
John Swanson69b313f83ee1
diff --git a/src/metabase/formatter/datetime.clj b/src/metabase/formatter/datetime.cljindex 58aa9837906..8d5ba0a8db7 100644--- a/src/metabase/formatter/datetime.clj+++ b/src/metabase/formatter/datetime.clj@@ -5,9 +5,9 @@[java-time.api :as t][metabase.models.visualization-settings :as mb.viz][metabase.query-processor.streaming.common :as streaming.common]- [metabase.system.core :as system][metabase.util.date-2 :as u.date][metabase.util.formatting.constants :as constants]+ [metabase.util.i18n :as i18n][metabase.util.log :as log])(:import(com.ibm.icu.text RuleBasedNumberFormat)@@ -18,7 +18,7 @@(def ^:dynamic *formatting-locale*"Dynamic var to hold the current locale for datetime formatting.Defaults to the site locale from system settings."- (Locale. (system/site-locale)))+ (i18n/site-locale))(defn temporal-string?"Returns `true` if the string `s` is parseable as a datetime.@@ -47,7 +47,7 @@(defn- x-of-y"Format an integer as x-th of y, for example, 2nd week of year."[n]- (let [nf (RuleBasedNumberFormat. ^java.util.Locale *formatting-locale* RuleBasedNumberFormat/ORDINAL)]+ (let [nf (RuleBasedNumberFormat. ^Locale *formatting-locale* RuleBasedNumberFormat/ORDINAL)](.format nf n)))(defn- hour-of-day@@ -241,17 +241,14 @@date-format)temporal-str)))))-;;; TODO (Cam 6/19/25) -- this is broken, see #59803(defn make-temporal-str-formatter"Return a formatter which, given a temporal literal string, reformats it by combining time zone, column, and viz- setting information to create a final desired output format.-- HAS GLOBAL SIDE EFFECTS!"+ setting information to create a final desired output format."[timezone-id col viz-settings](let [merged-viz-settings (streaming.common/normalize-keys(streaming.common/viz-settings-for-col col viz-settings))](fn [temporal-str](if (str/blank? temporal-str)""- (binding [*formatting-locale* (Locale. (system/site-locale))]+ (binding [*formatting-locale* (i18n/user-locale)](format-timestring timezone-id temporal-str col merged-viz-settings))))))diff --git a/test/metabase/formatter/datetime_test.clj b/test/metabase/formatter/datetime_test.cljindex fccb4f6ed85..ffc768e6c30 100644--- a/test/metabase/formatter/datetime_test.clj+++ b/test/metabase/formatter/datetime_test.clj@@ -37,7 +37,8 @@;;; TODO (Cam 6/18/25) -- these fail for me locally with `Dec` instead of `December` -- see;;; https://metaboat.slack.com/archives/CKZEMT1MJ/p1750288689214359(deftest format-temporal-str-test- (mt/with-temporary-setting-values [custom-formatting nil]+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting nil](testing "Null values do not blow up"(is (= ""(format-temporal-str "UTC" nil :now))))@@ -107,6 +108,20 @@{:effective_type :type/Time:settings {:time_enabled nil}}))))))+(deftest format-temporal-str-respects-user-locale+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting nil]+ (testing "Uses the user locale if present"+ (mt/with-temp [:model/User {id :id} {:locale "nl"}]+ (mt/with-current-user id+ (is (= "mei 1, 2014, 8:30 a.m."+ (format-temporal-str "UTC" "2014-05-01T08:30:00"+ {:effective_type :type/DateTime}))))))+ (testing "Falls back to site locale otherwise"+ (is (= "May 1, 2014, 8:30 AM"+ (format-temporal-str "UTC" "2014-05-01T08:30:00"+ {:effective_type :type/DateTime}))))))+(defn- format-with-colname-key[column viz](format-temporal-str "UTC" now column {::mb.viz/column-settings@@ -132,7 +147,8 @@:fmt-fn format-with-field-id-and-colname-key}])(deftest format-temporal-str-column-viz-settings-written-date-test- (mt/with-temporary-setting-values [custom-formatting nil]+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting nil](let [column {:field_ref [:field 1 nil] :name "created_at" :id 1 :effective_type :type/Date}](doseq [{:keys [name fmt-fn]} formatters-with-viz](let [fmt-part (partial fmt-fn column)]@@ -150,7 +166,8 @@(when date-style {::mb.viz/date-style date-style}))))))))))))(deftest format-temporal-str-column-viz-settings-numerical-date-test- (mt/with-temporary-setting-values [custom-formatting nil]+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting nil](let [column {:field_ref [:field 1 nil] :name "created_at" :id 1 :effective_type :type/Date}](doseq [{:keys [name fmt-fn]} formatters-with-viz](let [fmt-part (partial fmt-fn column)]@@ -174,7 +191,8 @@(testing "Custom Formatting options are respected as defaults.";; NOTE - format-temporal-str gets global settings from the `::mb.viz/global-column-settings`;; key of the viz-settings argument. These are looked up based on the type of the column.- (mt/with-temporary-setting-values [custom-formatting {:type/Temporal {:date_style "MMMM D, YYYY"+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting {:type/Temporal {:date_style "MMMM D, YYYY":date_abbreviate true}}](let [global-settings (m/map-vals mb.viz/db->norm-column-settings-entries(appearance/custom-formatting))]@@ -195,7 +213,8 @@(deftest format-datetime-test(testing "Testing permutations of a datetime string with different type information and viz settings (#36559)"- (mt/with-temporary-setting-values [custom-formatting nil]+ (mt/with-temporary-setting-values [site-locale "en"+ custom-formatting nil](let [common-viz-settings {::mb.viz/column-settings;; Settings specific to a certain column{{::mb.viz/column-name "CUSTOM_DATETIME"}