Use US Locale for Date and Time parsing and formatting (#6967)

Sorts out parsing and printing long form names of months and weekdays.
This commit is contained in:
James Dunkerley 2023-06-06 22:44:25 +01:00 committed by GitHub
parent 1931e9e51f
commit 578ba59f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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