Skip to content

Commit

Permalink
add locale to from_java, add examples with locale on from Text
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Sep 18, 2023
1 parent 1553909 commit e421654
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,23 @@ type Date_Time_Formatter

See the Java documentation for explanation of the pattern format:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html#patterns
from_java (pattern:Text|DateTimeFormatter) = case pattern of

Arguments:
- pattern: The pattern string to parse using the Java pattern rules, or
an existing `DateTimeFormatter` instance.
- locale: A locale to use when constructing the formatter from a text
pattern. If not specified, defaults to `Locale.default`. If passing a
`DateTimeFormatter` instance and this argument is set, it will
overwrite the original locale of that formatter.
from_java (pattern:Text|DateTimeFormatter) (locale:Locale|Nothing=Nothing) = case pattern of
java_formatter : DateTimeFormatter ->
Date_Time_Formatter.Value (EnsoDateTimeFormatter.new java_formatter False Nothing FormatterKind.RAW_JAVA)
amended_formatter = case locale of
Nothing -> java_formatter
_ : Locale -> java_formatter.withLocale locale.java_locale
Date_Time_Formatter.Value (EnsoDateTimeFormatter.new amended_formatter False Nothing FormatterKind.RAW_JAVA)
_ : Text ->
# TODO setting Locale too!
java_formatter = DateTimeFormatter.ofPattern pattern
java_locale = (locale.if_nothing Locale.default).java_locale
java_formatter = DateTimeFormatter.ofPattern pattern java_locale
Date_Time_Formatter.Value (EnsoDateTimeFormatter.new java_formatter False pattern FormatterKind.RAW_JAVA)

# TODO Locale for constants??
Expand Down
11 changes: 9 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Widget_Helpers.enso
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import project.Data.Locale.Locale
import project.Data.Time.Date.Date
import project.Data.Time.Date_Time.Date_Time
import project.Data.Time.Time_Of_Day.Time_Of_Day
Expand Down Expand Up @@ -28,11 +29,14 @@ make_date_format_selector (date:Date=(Date.new 2012 3 14)) =
iso_format = ['ISO-Format (e.g. ' + date.to_text + ')', "Date_Time_Format_Constants.ISO_Date"]
formats = ['d/M/yyyy', 'dd/MM/yyyy', 'd-MMM-yy', 'd MMMM yyyy', 'M/d/yyyy', 'MM/dd/yyyy', 'MMMM d, yyyy', 'yyyy-MM'].map f->
[f + " (e.g. " + date.format f + ")", f.pretty]
custom_locale_format =
format = Date_Time_Formatter.from 'd MMMM yyyy' locale=Locale.france
['d MMMM yyyy with custom Locale (e.g. ' + date.format format + ')', "(Date_Time_Formatter.from 'd MMMM yyyy' locale=Locale.france)"]
week_date_formats = ['YYYY-ww-d', 'YYYY-ww', 'ddd, YYYY-ww'].map f->
format = Date_Time_Formatter.from_iso_week_date_pattern f
["ISO Week-based Date: " + f + " (e.g. " + date.format format + ")", "("+fqn+".from_iso_week_date_pattern " + f.pretty + ")"]

make_single_choice ([iso_format] + formats + week_date_formats)
make_single_choice ([iso_format] + formats + [custom_locale_format] + week_date_formats)

## PRIVATE
Creates a Single_Choice Widget for parsing date times.
Expand All @@ -50,11 +54,14 @@ make_date_time_format_selector (date_time:Date_Time=(Date_Time.new 2012 3 14 15
['ISO-Local (e.g. ' + (date_time.format format) + ')', "Date_Time_Format_Constants.ISO_Local_Date_Time"]
formats = ['yyyy-MM-dd HH:mm:ss.f', 'yyyy-MM-dd HH:mm:ss.f TT', 'd/M/yyyy h:mm a', 'dd/MM/yyyy HH:mm:ss', 'd-MMM-yy HH:mm:ss', 'd-MMM-yy h:mm:ss a', 'd MMMM yyyy h:mm a', 'M/d/yyyy h:mm:ss a', 'MM/dd/yyyy HH:mm:ss']
mapped_formats = formats.map f-> [f + " (e.g. " + date_time.format f + ")", f.pretty]
custom_locale_format =
format = Date_Time_Formatter.from 'd MMMM yyyy h:mm a' locale=Locale.france
['d MMMM yyyy h:mm a with custom Locale (e.g. ' + date_time.format format + ')', "(Date_Time_Formatter.from 'd MMMM yyyy h:mm a' locale=Locale.france)"]
week_date_formats = ['YYYY-ww-d HH:mm:ss.f'].map f->
format = Date_Time_Formatter.from_iso_week_date_pattern f
["ISO Week-based Date-Time: " + f + " (e.g. " + date_time.format format + ")", "("+fqn+".from_iso_week_date_pattern " + f.pretty + ")"]

make_single_choice ([enso_format, iso_format, iso_local] + mapped_formats + week_date_formats)
make_single_choice ([enso_format, iso_format, iso_local] + mapped_formats + [custom_locale_format] + week_date_formats)

## PRIVATE
Creates a Single_Choice Widget for parsing times.
Expand Down

0 comments on commit e421654

Please sign in to comment.