Translates a Calendar.strftime/3 format into the CLDR pattern that
Localize.Date.parse/2, Localize.Time.parse/2 and
Localize.DateTime.parse/2 read with, so text written with a strftime
format, by Calendar.strftime/3, by C's strftime(3) or by Python or
Ruby, is read back by the same parsers in any locale.
Localize.Date.strptime/3, Localize.Time.strptime/3 and
Localize.DateTime.strptime/3 call pattern/2 and then parse/2.
This module is the translation alone. The strptime
guide lists every directive, what each reads and
where the reading differs from C's strptime(3).
Summary
Functions
The options parse/2 takes from the options given to a strptime/3
function: the options less :preferred_datetime, :preferred_date
and :preferred_time, with :format set to the translated pattern.
Translates a Calendar.strftime/3 format into a CLDR date and time
pattern.
Functions
@spec parse_options(Keyword.t(), String.t()) :: {:ok, Keyword.t()} | {:error, Exception.t()}
The options parse/2 takes from the options given to a strptime/3
function: the options less :preferred_datetime, :preferred_date
and :preferred_time, with :format set to the translated pattern.
Arguments
optionsis the keyword list given tostrptime/3.patternis the CLDR patternpattern/2returned.
Returns
{:ok, options}with the keyword list of options forparse/2, or{:error, exception}, aLocalize.InvalidValueError.t/0ifoptionsis not a keyword list orpatternis not a string.
Examples
iex> Localize.DateTime.Strptime.parse_options([locale: :de, preferred_date: "%d.%m.%Y"], "dd.MM.y")
{:ok, [format: "dd.MM.y", locale: :de]}
@spec pattern(String.t(), Keyword.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Translates a Calendar.strftime/3 format into a CLDR date and time
pattern.
Each directive becomes the pattern field that reads what the directive
writes; any other letter in the format is quoted, so it is read as the
literal text it is; %% is a literal percent sign; and padding and
width modifiers (%-d, %_H, %05Y) are accepted and ignored, since
the parsers read a field at any width.
Arguments
formatis aCalendar.strftime/3format string.optionsis a keyword list of options.
Options
:preferred_datetime,:preferred_dateand:preferred_timeare the strftime formats%c,%xand%Xstand for, asCalendar.strftime/3takes them. The defaults are"%Y-%m-%d %H:%M:%S","%Y-%m-%d"and"%H:%M:%S". A preferred format may not itself contain%c,%xor%X.
Returns
{:ok, pattern}with the CLDR pattern, or{:error, exception}, aLocalize.DateTimeFormatError.t/0with the reason:unsupported_directiveand the directive in:detailfor a directive Localize does not read (%s,%u,%w,%U,%W,%V,%G,%C, the%Eand%Omodifiers, or a%that ends the format), or aLocalize.InvalidValueError.t/0ifformatis not a string or an option is malformed.
Examples
iex> Localize.DateTime.Strptime.pattern("%Y-%m-%dT%H:%M:%S")
{:ok, "y-MM-dd'T'HH:mm:ss"}
iex> Localize.DateTime.Strptime.pattern("%d %B %Y at %I:%M %p")
{:ok, "dd MMMM y 'at' hh:mm a"}
iex> Localize.DateTime.Strptime.pattern("%c")
{:ok, "y-MM-dd HH:mm:ss"}
iex> Localize.DateTime.Strptime.pattern("%x %X", preferred_date: "%d/%m/%Y")
{:ok, "dd/MM/y HH:mm:ss"}
iex> Localize.DateTime.Strptime.pattern("%Y week %V")
{:error,
%Localize.DateTimeFormatError{format: "%Y week %V", reason: :unsupported_directive, detail: "%V"}}