pagantis_elixir_tools v0.19.1 ElixirTools.CardDate
Helper functions for dealing with card date transformations and parsing.
Dictionary of terms
Term | Description | Template | Example |
--------------|---------------------------------------|-------------|-------------|
card_date
| Date format used by CC issuers | MM/YY | 12/30 |
date
| Elixir Date type | %Date{} | ... |
iso_date
| ISO8601 string of the date | YYYY-MM-DD | 2030-12-15 |
short_year
| Year written in the shortened format | YY | 19 |
full_year
| Year written in the full format | YYYY | 2019 |
Link to this section Summary
Functions
Enforces that the provided card_date
is in the correct format (mm/yy).
If the format is correct, the same data is returned, and if it's incorrect
an error is raised.
Takes a Date-compatible input and generates a card_date
string.
Gets the integer value of the month
part of the card_date
.
Gets the integer value of the (full) year
part of the card_date
.
Tries to parse the card_date
into a NaiveDateTime. Raises a RuntimeError
if unsuccessful.
Transforms the card_date
format into an Elixir %Date{} type.
Since the card_date
format does not contain a day
value, it
must also be supplied, otherwise the default 1
is taken.
Returns the card_date
formatted as an ISO8601 string.
Since the card_date
format does not contain a day
value, it
must also be supplied, otherwise the default 1
is taken.
Link to this section Functions
enforce_card_date_format!(card_date)
Enforces that the provided card_date
is in the correct format (mm/yy).
If the format is correct, the same data is returned, and if it's incorrect
an error is raised.
Examples
iex> ElixirTools.CardDate.enforce_card_date_format!("12/19")
"12/19"
iex> ElixirTools.CardDate.enforce_card_date_format!("12.19")
** (RuntimeError) Invalid card_date format
iex> ElixirTools.CardDate.enforce_card_date_format!("2019-12-01")
** (RuntimeError) Invalid card_date format
iex> ElixirTools.CardDate.enforce_card_date_format!(42)
** (RuntimeError) Invalid card_date format
Takes a Date-compatible input and generates a card_date
string.
Examples
iex> ElixirTools.CardDate.from_date!(~D[2012-09-01])
"09/12"
iex> ElixirTools.CardDate.from_date!(~N[2022-12-03 12:34:56])
"12/22"
iex> ElixirTools.CardDate.from_date!("2001-09-11")
** (RuntimeError) Invalid Date provided
Gets the integer value of the month
part of the card_date
.
Examples
iex> ElixirTools.CardDate.get_month!("12/19")
12
get_year!(card_date)
get_year!(card_date()) :: non_neg_integer() | no_return()
Gets the integer value of the (full) year
part of the card_date
.
Examples
iex> ElixirTools.CardDate.get_year!("12/19")
2019
parse_card_date!(card_date)
parse_card_date!(term()) :: NaiveDateTime.t() | no_return()
Tries to parse the card_date
into a NaiveDateTime. Raises a RuntimeError
if unsuccessful.
Examples
iex> ElixirTools.CardDate.parse_card_date!("12/19")
~N[2019-12-01 00:00:00]
iex> ElixirTools.CardDate.parse_card_date!(42)
** (RuntimeError) Invalid card_date format
to_date!(card_date, opts \\ [])
to_date!(card_date(), to_date_opts()) :: date()
Transforms the card_date
format into an Elixir %Date{} type.
Since the card_date
format does not contain a day
value, it
must also be supplied, otherwise the default 1
is taken.
Examples
iex> ElixirTools.CardDate.to_date!("12/19")
~D[2019-12-01]
iex> ElixirTools.CardDate.to_date!("12/19", [day: 16])
~D[2019-12-16]
to_iso_string!(card_date, opts \\ [])
to_iso_string!(card_date(), to_iso_string_opts()) :: iso_date()
Returns the card_date
formatted as an ISO8601 string.
Since the card_date
format does not contain a day
value, it
must also be supplied, otherwise the default 1
is taken.
Examples
iex> ElixirTools.CardDate.to_iso_string!("12/19")
"2019-12-01"
iex> ElixirTools.CardDate.to_iso_string!("12/19", [day: 16])
"2019-12-16"