# `Localize.DateTime.Strptime`
[🔗](https://github.com/elixir-localize/localize/blob/v1.7.0/lib/localize/datetime/strptime.ex#L1)

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](strptime.html) lists every directive, what each reads and
where the reading differs from C's `strptime(3)`.

# `parse_options`

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

* `options` is the keyword list given to `strptime/3`.

* `pattern` is the CLDR pattern `pattern/2` returned.

### Returns

* `{:ok, options}` with the keyword list of options for `parse/2`, or

* `{:error, exception}`, a `t:Localize.InvalidValueError.t/0` if
  `options` is not a keyword list or `pattern` is 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]}

# `pattern`

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

* `format` is a `Calendar.strftime/3` format string.

* `options` is a keyword list of options.

### Options

* `:preferred_datetime`, `:preferred_date` and `:preferred_time` are
  the strftime formats `%c`, `%x` and `%X` stand for, as
  `Calendar.strftime/3` takes 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`, `%x` or `%X`.

### Returns

* `{:ok, pattern}` with the CLDR pattern, or

* `{:error, exception}`, a `t:Localize.DateTimeFormatError.t/0` with
  the reason `:unsupported_directive` and the directive in `:detail`
  for a directive Localize does not read (`%s`, `%u`, `%w`, `%U`,
  `%W`, `%V`, `%G`, `%C`, the `%E` and `%O` modifiers, or a `%` that
  ends the format), or a `t:Localize.InvalidValueError.t/0` if
  `format` is 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"}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
