defmodule Cldr.DateTime do @moduledoc """ Provides localization and formatting of a datetime. 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) CLDR provides standard format strings for `t:DateTime.t/0` which are represented by the names `:short`, `:medium`, `:long` and `:full`. This allows for locale-independent formatting since each locale and calendar will define the underlying format string as appropriate. """ alias Cldr.DateTime.Format alias Cldr.LanguageTag alias Cldr.Locale @typep options :: Keyword.t() | map() @format_types [:short, :medium, :long, :full] @default_format_type :medium @default_style :default @default_prefer :unicode @doc """ Indicates if a given map fulfills the requirements for a naive date time. """ defguard is_naive_date_time(datetime) when is_map_key(datetime, :year) and is_map_key(datetime, :month) and is_map_key(datetime, :day) and is_map_key(datetime, :hour) and is_map_key(datetime, :minute) and is_map_key(datetime, :second) @doc """ Indicates if a given map fulfills the requirements for a date time. """ defguard is_date_time(datetime) when is_naive_date_time(datetime) and is_map_key(datetime, :time_zone) and is_map_key(datetime, :zone_abbr) @doc """ Indicates if a given map fulfills the requirements for a naive date time or date time. """ defguard is_any_date_time(datetime) when is_date_time(datetime) or is_naive_date_time(datetime) @doc """ Guards whether the given datetime has components of a date. """ defguard has_date(datetime) when is_map_key(datetime, :year) or is_map_key(datetime, :month) or is_map_key(datetime, :day) @doc """ Guards whether the given datetime has components of a time. """ defguard has_time(datetime) when is_map_key(datetime, :hour) or is_map_key(datetime, :minute) or is_map_key(datetime, :second) @doc """ Guard whether the given datetime has components of both a date and a time. """ defguard has_date_and_time(datetime) when has_date(datetime) and has_time(datetime) defmodule Formats do @moduledoc false defstruct Module.get_attribute(Cldr.DateTime, :format_types) end @doc """ Formats a DateTime according to a format string as defined in CLDR and described in [TR35](http://unicode.org/reports/tr35/tr35-dates.html). ### Arguments * `datetime` 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 one of `:short`, `:medium`, `:long`, `:full`, or a format ID or a format string. The default is `:medium` for full datetimes (that is, dates having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and `:calendar` fields). The default for partial datetimes is to derive a candidate format ID from the date and find the best match from the formats returned by `Cldr.DateTime.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats) for more information about specifying formats. * `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined, this option is used to format the date part of the date time. This option is only acceptable if the `:format` option is not specified, or is specified as either `:short`, `:medium`, `:long`, `:full`. If `:date_format` is not specified then the date format is defined by the `:format` option. * `:time_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined, this option is used to format the time part of the date time. This option is only acceptable if the `:format` option is not specified, or is specified as either `:short`, `:medium`, `:long`, `:full`. If `:time_format` is not specified then the time format is defined by the `:format` option. * `:style` is either `:at` or `:default`. When set to `:at` the datetime may be formatted with a localised string representing ` at