SiteMaps.Date (site_maps v0.1.0)

Copy Markdown View Source

W3C datetime normalization and formatting for sitemap dates.

A NaiveDateTime is treated as UTC, a DateTime or ISO 8601 string keeps its offset and a Date is written as YYYY-MM-DD. Sub-second precision is dropped.

Summary

Functions

Casts a value to a DateTime or Date.

Formats a date in the W3C datetime format.

Types

t()

@type t() :: DateTime.t() | Date.t()

Functions

cast!(value, field \\ :date)

@spec cast!(term(), atom()) :: t() | nil

Casts a value to a DateTime or Date.

Accepts DateTime, NaiveDateTime (as UTC), Date and ISO 8601 strings. nil stays nil. Raises ArgumentError for anything else, including integers.

w3c(datetime)

@spec w3c(DateTime.t() | NaiveDateTime.t() | Date.t()) :: String.t()

Formats a date in the W3C datetime format.

iex> SiteMaps.Date.w3c(~U[2026-03-01 10:00:00.123Z])
"2026-03-01T10:00:00+00:00"

iex> SiteMaps.Date.w3c(~N[2026-03-01 10:00:00])
"2026-03-01T10:00:00+00:00"

iex> SiteMaps.Date.w3c(~D[2026-03-01])
"2026-03-01"