Machete.ISO8601DateTimeMatcher (Machete v0.3.11)

View Source

Defines a matcher that matches ISO8601 formatted strings

Summary

Types

Describes the arguments that can be passed to this matcher

t()

Describes an instance of this matcher

Functions

Matches against ISO8601 formatted strings

Types

opts()

@type opts() :: [
  precision: 0..6,
  time_zone: Calendar.time_zone() | :utc,
  offset_required: boolean(),
  exactly: DateTime.t(),
  roughly: DateTime.t() | :now,
  epsilon: integer() | {integer(), integer()},
  before: DateTime.t() | :now,
  after: DateTime.t() | :now
]

Describes the arguments that can be passed to this matcher

t()

@opaque t()

Describes an instance of this matcher

Functions

iso8601_datetime(opts \\ [])

@spec iso8601_datetime(opts()) :: t()

Matches against ISO8601 formatted strings

Takes the following arguments:

  • precision: Requires the matched ISO8601 string to have the specified microsecond precision
  • time_zone: Requires the matched ISO8601 string to have the specified time zone. The atom :utc can be used to specify the "Etc/UTC" time zone
  • offset_required: Requires the matched ISO8601 string to have a timezone. Defaults to true
  • exactly: Requires the matched ISO8601 string to be exactly equal to the specified DateTime
  • roughly: Requires the matched ISO8601 string to be within epsilon seconds of the specified DateTime. This values must be specified as a DateTime. The atom :now can be used to use the current time as the specified DateTime
  • epsilon: The bound(s) to use when determining how close (in microseconds) the matched ISO8601 string needs to be to roughly. Can be specified as a single integer that is used for both lower and upper bounds, or a tuple consisting of distinct lower and upper bounds. If not specified, defaults to 10_000_000 microseconds (10 seconds)
  • before: Requires the matched ISO8601 string to be before or equal to the specified DateTime. This values must be specified as a DateTime. The atom :now can be used to use the current time as the specified DateTime
  • after: Requires the matched ISO8601 string to be after or equal to the specified DateTime. This values must be specified as a DateTime. The atom :now can be used to use the current time as the specified DateTime

Examples:

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime()
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(precision: 6)
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(time_zone: :utc)
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(time_zone: "Etc/UTC")
true

iex> assert "2020-01-01 00:00:00.000000Z" ~> iso8601_datetime(exactly: ~U[2020-01-01 00:00:00.000000Z])
true

iex> assert DateTime.utc_now() |> DateTime.to_iso8601() ~> iso8601_datetime(roughly: :now)
true

iex> assert NaiveDateTime.utc_now() |> NaiveDateTime.to_iso8601() ~> iso8601_datetime(roughly: :now, offset_required: false)
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(roughly: ~U[2020-01-01 00:00:05.000000Z])
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(roughly: ~U[2020-01-01 00:00:10.000000Z], epsilon: 10000000)
true

iex> assert "2020-01-01 00:00:00.000000Z" ~> iso8601_datetime(roughly: ~U[2020-01-01 00:00:10.000000Z], epsilon: {10000000, 5000000})
true

iex> refute "2020-01-01 00:00:00.000000Z" ~> iso8601_datetime(roughly: ~U[2020-01-01 00:00:10.000001Z], epsilon: 10000000)
false

iex> refute "2020-01-01 00:00:00.000000Z" ~> iso8601_datetime(roughly: ~U[2020-01-01 00:00:10.000001Z], epsilon: {10000000, 5000000})
false

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(before: :now)
true

iex> assert "2020-01-01T00:00:00.000000Z" ~> iso8601_datetime(before: ~U[3000-01-01 00:00:00.000000Z])
true

iex> assert "3000-01-01T00:00:00.000000Z" ~> iso8601_datetime(after: :now)
true

iex> assert "3000-01-01T00:00:00.000000Z" ~> iso8601_datetime(after: ~U[2020-01-01 00:00:00.000000Z])
true