defmodule Cldr.DateTime do @moduledoc """ Provides localized formatting of full or partial date_times. A date_time is any `t:DateTime.t/0` or `t:NaiveDateTime.t/0` struct or any map that contains one or more of the keys `:year`, `:month`, `:day`, `:hour`, `:minute` and `:second` or `:microsecond` with optional `:time_zone`, `:zone_abbr`, `:utc_offset`, `:std_offset` and `:calendar` fields. `Cldr.DateTime` provides support for the built-in calendar `Calendar.ISO` or any calendars defined with [ex_cldr_calendars](https://hex.pm/packages/ex_cldr_calendars) For information about specifying formats, see `Cldr.DateTime.Format`. """ alias Cldr.DateTime.Format.Compiler alias Cldr.DateTime.Format.Match alias Cldr.DateTime.Format alias Cldr.Locale @typep options :: Keyword.t() | map() @standard_formats Format.standard_formats() @default_standard_format :medium @default_style :default @default_prefer :unicode @default_separators :standard @doc """ Indicates if a given map fulfills the requirements for a naive date time. """ defguard is_naive_date_time(date_time) when is_map_key(date_time, :year) and is_map_key(date_time, :month) and is_map_key(date_time, :day) and is_map_key(date_time, :hour) and is_map_key(date_time, :minute) and is_map_key(date_time, :second) @doc """ Indicates if a given map fulfills the requirements for a date time. """ defguard is_date_time(date_time) when is_naive_date_time(date_time) and is_map_key(date_time, :time_zone) and is_map_key(date_time, :zone_abbr) @doc """ Indicates if a given map fulfills the requirements for a naive date time or date time. """ defguard is_any_date_time(date_time) when is_date_time(date_time) or is_naive_date_time(date_time) @doc """ Guards whether the given date_time has components of a date. """ defguard has_date(date_time) when is_map_key(date_time, :year) or is_map_key(date_time, :month) or is_map_key(date_time, :day) @doc """ Guards whether the given date_time has components of a time. """ defguard has_time(date_time) when is_map_key(date_time, :hour) or is_map_key(date_time, :minute) or is_map_key(date_time, :second) @doc """ Guard whether the given date_time has components of both a date and a time. """ defguard has_date_and_time(date_time) when has_date(date_time) and has_time(date_time) @doc """ Guard whether a format is a format skeleton """ defguard is_skeleton(format) when is_atom(format) and not is_nil(format) defmodule Formats do @moduledoc false defstruct Module.get_attribute(Cldr.DateTime, :standard_formats) end @doc """ Formats a `t:DateTime.t/0` or `t:NaiveDateTime.t/0` according to the formats defined in CLDR and described in [TR35](http://unicode.org/reports/tr35/tr35-dates.html). ### Arguments * `date_time` is a `t:DateTime.t/0` or `t:NaiveDateTime.t/0` struct or any map that contains one or more of the keys `:year`, `:month`, `:day`, `:hour`, `:minute` and `:second` or `:microsecond` with optional `:time_zone`, `:zone_abbr`, `:utc_offset`, `:std_offset` and `:calendar` fields. * `backend` is any module that includes `use Cldr` and therefore is a `Cldr` backend module. The default is `Cldr.default_backend!/0`. * `options` is a keyword list of options for formatting. ## Options * `:format` is either a [standard format](Cldr.DateTime.Format.html#module-standard-formats) (one of `:short`, `:medium`, `:long`, `:full`), a [format skeleton](Cldr.DateTime.Format.html#module-format-skeletons) or a [format pattern](Cldr.DateTime.Format.html#module-format-patterns). * The default is `:medium` for full *date_times* (that is, *dates_times* having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and `:calendar` fields). * The default for partial *date_times* is to derive a format skeleton from the *date_time* and find the best match from the formats returned by `Cldr.DateTime.available_formats/3`. * See `Cldr.DateTime.Format` for more information about specifying formats. * `:date_format` is used to format the *date* part of a *date_time* and is either a standard format or a format skeleton. * If `:date_format` is not specified then the *date* format is defined by the `:format` option. * If `:date_format` is a format skeleton it may only include format fields appropriate for a *date*. * :date_format` may only be specified if `:format` is a standard format. * `:time_format` is used to format the *time* part of a *date_time* and is either a standard format or a format skeleton. * If `:time_format` is not specified then the *time* format is defined by the `:format` option. * If `:time_format` is a format skeleton it may only include format fields appropriate for a *time*. * :time_format` may only be specified if `:format` is a standard format. * `:style` is either `:at` or `:default`. When set to `:at` the *date_time* may be formatted with a localised string representing ` at