Skip to main content

Module date_utils

Module date_utils 

Source
Expand description

Date and datetime utilities.

EDTF level 0 (ISO 8601 subset used by DataCite and InvenioRDM) is the primary parser. A manual fallback handles malformed dates from upstream sources (e.g. single-digit months like “2023-8-25”).

Mirrors the Go dateutils package in front-matter/commonmeta.

Functions§

date_from_parts
Build an ISO 8601 partial date from numeric parts. Pass 0 for month or day to omit that component. date_from_parts(2023, 8, 5)"2023-08-05".
date_from_str_parts
Build an ISO 8601 partial date from string parts (empty string = omit). Used for Crossref XML and RIS date fields.
date_only
Return only the date portion of a datetime string (first 10 characters). "2024-01-15T12:34:56Z""2024-01-15". Pass-through for shorter strings.
normalize_date
Pad single-digit month/day components to two digits so they are valid EDTF/ISO 8601. "2023-8-5""2023-08-05", "2023-8-5T12:00:00""2023-08-05T12:00:00".
parse_date
Parse a date or datetime string and return an ISO 8601 date ("YYYY", "YYYY-MM", or "YYYY-MM-DD"). Time components are discarded.
parse_datetime
Parse a date or datetime string and return an ISO 8601 datetime ("YYYY-MM-DDTHH:MM:SSZ"), or a date-only string when the time is midnight. Returns "" on failure.
sanitize_date
Sanitize an arbitrary date string from upstream sources into valid EDTF / ISO 8601. Handles, in order:
strip_milliseconds
Remove sub-second fractions from an ISO 8601 datetime string. A midnight time component (T00:00:00) is stripped entirely, leaving only the date. Normalises +00:00 timezone suffix to Z.
unix_to_date
Convert a Unix timestamp to an ISO 8601 date string ("YYYY-MM-DD").
unix_to_datetime
Convert a Unix timestamp to an ISO 8601 datetime string ("YYYY-MM-DDTHH:MM:SSZ").