module

Hwaro::Utils::DateUtils

Constants

CONTENT_FORMATS = ["%Y-%m-%dT%H:%M:%S%:z", "%Y-%m-%dT%H:%M:%S%z", "%Y-%m-%d %H:%M:%S %:z", "%Y-%m-%d %H:%M:%S %z", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d"]

Formats accepted by parse_lenient. Zone-bearing formats come FIRST: Crystal's Time.parse ignores trailing input, so a zone-less pattern would happily match 2026-07-01T10:00:00+09:00, silently drop the +09:00, and shift the instant by the whole offset.

IMPORT_FORMATS = CONTENT_FORMATS[0...-1] + ["%Y-%m-%dT%H:%M%:z", "%Y-%m-%d %H:%M %:z", "%Y-%m-%d %H:%M %z", "%Y-%m-%dT%H:%M", "%Y-%m-%d %H:%M", CONTENT_FORMATS.last, "%Y/%m/%d %H:%M:%S %:z", "%Y/%m/%d %H:%M:%S %z", "%Y/%m/%d %H:%M %:z", "%Y/%m/%d %H:%M %z", "%Y/%m/%d %H:%M:%S", "%Y/%m/%d %H:%M", "%Y/%m/%d", "%a, %d %b %Y %H:%M:%S %z"]

Importers additionally accept minute-precision, slash and RFC 822 dates (and prose dates, see parse_import) — what Jekyll, Hexo and Astro accept in front matter. The minute-precision formats must precede the bare %Y-%m-%d (the last CONTENT_FORMATS entry): it ignores trailing input, so it matched 2024-01-15 10:30 and silently dropped the time.

PROSE_DATE_FORMATS = ["%B %d, %Y", "%B %d %Y"]
PROSE_DATE_RE = /\A[A-Za-z]+\.? \d{1,2},? \d{4}\b/

Prose dates (July 8, 2022, and Jul 08 2022 from Astro's blog template, a JS Date string). Tried only on input shaped like Month D YYYY: unguarded, %B %d %Y read May 2022 as day 20 of the year 22.

Instance methods

parse_content_date(time_str : String | Nil) : Time | Nil

The build's front-matter date parser. Format selection is based on the string's shape to avoid exception-based control flow; zone-less values are interpreted in the machine's local zone.

Source
parse_import(date_str : String) : Time | Nil

The importers' parser: parse_lenient over IMPORT_FORMATS, then the guarded prose formats.

Source
parse_lenient(date_str : String, formats : Array(String) = CONTENT_FORMATS) : Time | Nil

Parse a date string in common formats, returns nil on failure. RFC 3339 is probed first, then the format table in UTC.

Source