From 578ba59f1dc9cb2d672b8d587176446cb86da0f8 Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Tue, 6 Jun 2023 22:44:25 +0100 Subject: [PATCH] Use US Locale for Date and Time parsing and formatting (#6967) Sorts out parsing and printing long form names of months and weekdays. --- .../main/java/org/enso/base/Time_Utils.java | 21 ++++++++++--------- .../src/In_Memory/Column_Format_Spec.enso | 4 ++-- test/Tests/src/Data/Time/Date_Spec.enso | 17 +++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/std-bits/base/src/main/java/org/enso/base/Time_Utils.java b/std-bits/base/src/main/java/org/enso/base/Time_Utils.java index 478cc3fea63..02f8841ceb8 100644 --- a/std-bits/base/src/main/java/org/enso/base/Time_Utils.java +++ b/std-bits/base/src/main/java/org/enso/base/Time_Utils.java @@ -37,6 +37,7 @@ public class Time_Utils { * @return DateTimeFormatter */ public static DateTimeFormatter make_formatter(String format, Locale locale) { + var usedLocale = locale == Locale.ROOT ? Locale.US : locale; return switch (format) { case "ENSO_ZONED_DATE_TIME" -> Time_Utils.default_zoned_date_time_formatter(); case "ISO_ZONED_DATE_TIME" -> DateTimeFormatter.ISO_ZONED_DATE_TIME; @@ -44,7 +45,7 @@ public class Time_Utils { case "ISO_LOCAL_DATE_TIME" -> DateTimeFormatter.ISO_LOCAL_DATE_TIME; case "ISO_LOCAL_DATE" -> DateTimeFormatter.ISO_LOCAL_DATE; case "ISO_LOCAL_TIME" -> DateTimeFormatter.ISO_LOCAL_TIME; - default -> DateTimeFormatter.ofPattern(format, locale); + default -> DateTimeFormatter.ofPattern(format, usedLocale); }; } @@ -103,27 +104,27 @@ public class Time_Utils { } public static String local_date_format(LocalDate date, Object format) { - return DateTimeFormatter.ofPattern(format.toString()).format(date); + return make_output_formatter(format.toString(), Locale.US).format(date); } public static String local_date_format_with_locale(LocalDate date, Object format, Locale locale) { - return DateTimeFormatter.ofPattern(format.toString()).withLocale(locale).format(date); + return make_output_formatter(format.toString(), locale).format(date); } public static String date_time_format(ZonedDateTime dateTime, Object format) { - return DateTimeFormatter.ofPattern(format.toString()).format(dateTime); + return make_output_formatter(format.toString(), Locale.US).format(dateTime); } public static String date_time_format_with_locale(ZonedDateTime dateTime, Object format, Locale locale) { - return DateTimeFormatter.ofPattern(format.toString()).withLocale(locale).format(dateTime); - } - - public static String time_of_day_format_with_locale(LocalTime localTime, Object format, Locale locale) { - return DateTimeFormatter.ofPattern(format.toString()).withLocale(locale).format(localTime); + return make_output_formatter(format.toString(), locale).format(dateTime); } public static String time_of_day_format(LocalTime localTime, Object format) { - return DateTimeFormatter.ofPattern(format.toString()).format(localTime); + return make_output_formatter(format.toString(), Locale.US).format(localTime); + } + + public static String time_of_day_format_with_locale(LocalTime localTime, Object format, Locale locale) { + return make_output_formatter(format.toString(), locale).format(localTime); } public static LocalDate date_adjust(LocalDate date, AdjustOp op, Period period) { diff --git a/test/Table_Tests/src/In_Memory/Column_Format_Spec.enso b/test/Table_Tests/src/In_Memory/Column_Format_Spec.enso index 7c7cf9c7b78..30abd594bb0 100644 --- a/test/Table_Tests/src/In_Memory/Column_Format_Spec.enso +++ b/test/Table_Tests/src/In_Memory/Column_Format_Spec.enso @@ -22,7 +22,7 @@ spec = Test.specify "Date with locale" <| input = Column.from_vector "values" [Date.new 2020 6 21, Date.new 2023 4 25] - expected_default = Column.from_vector "values" ["21. Jun 2020", "25. Apr 2023"] + expected_default = Column.from_vector "values" ["21. June 2020", "25. April 2023"] expected_gb = Column.from_vector "values" ["21. Jun 2020", "25. Apr 2023"] expected_fr = Column.from_vector "values" ["21. juin 2020", "25. avril 2023"] input.format "d. MMMM yyyy" . should_equal expected_default @@ -86,7 +86,7 @@ spec = Test.specify "Date_Time with locale" <| input = Column.from_vector "values" [Date_Time.new 2020 6 21 8 10 20, Date_Time.new 2023 4 25 14 25 2] - expected_default = Column.from_vector "values" ["21. Jun 2020 08.10.20", "25. Apr 2023 14.25.02"] + expected_default = Column.from_vector "values" ["21. June 2020 08.10.20", "25. April 2023 14.25.02"] expected_gb = Column.from_vector "values" ["21. Jun 2020 08.10.20", "25. Apr 2023 14.25.02"] expected_fr = Column.from_vector "values" ["21. juin 2020 08.10.20", "25. avril 2023 14.25.02"] input.format "d. MMMM yyyy HH.mm.ss" . should_equal expected_default diff --git a/test/Tests/src/Data/Time/Date_Spec.enso b/test/Tests/src/Data/Time/Date_Spec.enso index c3cefa634e1..83f535ade2c 100644 --- a/test/Tests/src/Data/Time/Date_Spec.enso +++ b/test/Tests/src/Data/Time/Date_Spec.enso @@ -37,6 +37,11 @@ spec_with name create_new_date parse_date = text = create_new_date 2020 12 21 . format "yyyyMMdd" text . should_equal "20201221" + Test.specify "should format local date using provided pattern and US locale" <| + d = create_new_date 2020 6 21 + d.format "d. MMM yyyy" . should_equal "21. Jun 2020" + d.format "d. MMMM yyyy" . should_equal "21. June 2020" + Test.specify "should format local date using provided pattern and locale" <| d = create_new_date 2020 6 21 d.format "d. MMMM yyyy" (Locale.new "gb") . should_equal "21. Jun 2020" @@ -72,6 +77,18 @@ spec_with name create_new_date parse_date = date . month . should_equal 1 date . day . should_equal 1 + Test.specify "should parse text month formats" <| + date = parse_date "1999 Jan 1" "yyyy MMM d" + date . year . should_equal 1999 + date . month . should_equal 1 + date . day . should_equal 1 + + Test.specify "should parse text long month formats" <| + date = parse_date "1999 January 1" "yyyy MMMM d" + date . year . should_equal 1999 + date . month . should_equal 1 + date . day . should_equal 1 + Test.specify "should throw error when parsing custom format" <| date = parse_date "1999-01-01" "yyyy M d" case date.catch of