ocibuild_time (ocibuild v0.10.4)

View Source

Central timestamp utilities for reproducible builds.

This module provides timestamp functions that respect the SOURCE_DATE_EPOCH environment variable for reproducible builds. When SOURCE_DATE_EPOCH is set, all timestamps will use that value instead of the current time.

See: https://reproducible-builds.org/docs/source-date-epoch/

Usage:

%% Get Unix timestamp (uses SOURCE_DATE_EPOCH if set)
Timestamp = ocibuild_time:get_timestamp().

%% Get ISO8601 formatted timestamp
ISO8601 = ocibuild_time:get_iso8601().

%% Convert Unix timestamp to ISO8601
ISO8601 = ocibuild_time:unix_to_iso8601(1700000000).

Summary

Functions

Get ISO8601 timestamp using SOURCE_DATE_EPOCH if available.

Get timestamp: SOURCE_DATE_EPOCH if set, else current time.

Parse and normalize an RFC 3339 timestamp to a consistent format.

Parse and validate an RFC 3339 timestamp string.

Convert Unix timestamp to ISO8601 format.

Functions

get_iso8601()

-spec get_iso8601() -> binary().

Get ISO8601 timestamp using SOURCE_DATE_EPOCH if available.

Returns timestamp in format: "2024-01-15T12:30:45Z"

get_timestamp()

-spec get_timestamp() -> non_neg_integer().

Get timestamp: SOURCE_DATE_EPOCH if set, else current time.

Returns Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).

normalize_rfc3339(Bin)

-spec normalize_rfc3339(binary()) -> {ok, binary()} | {error, badarg}.

Parse and normalize an RFC 3339 timestamp to a consistent format.

Accepts RFC 3339 strings with various timezone formats and normalizes to UTC with Z suffix: "YYYY-MM-DDTHH:MM:SSZ"

Example:

{ok, ~"2024-01-01T00:00:00Z"} = ocibuild_time:normalize_rfc3339(~"2024-01-01T00:00:00Z").
{ok, ~"2024-01-01T00:00:00Z"} = ocibuild_time:normalize_rfc3339(~"2024-01-01T01:00:00+01:00").
{error, badarg} = ocibuild_time:normalize_rfc3339(~"invalid").

parse_rfc3339(Bin)

-spec parse_rfc3339(binary()) -> {ok, integer()} | {error, badarg}.

Parse and validate an RFC 3339 timestamp string.

Uses calendar:rfc3339_to_system_time/1 for proper validation including date validity checks (no Feb 30th, etc.).

Returns the system time in seconds if valid.

Example:

{ok, 1704067200} = ocibuild_time:parse_rfc3339(~"2024-01-01T00:00:00Z").
{error, badarg} = ocibuild_time:parse_rfc3339(~"not-a-date").
{error, badarg} = ocibuild_time:parse_rfc3339(~"2024-02-30T00:00:00Z").

unix_to_iso8601(Timestamp)

-spec unix_to_iso8601(non_neg_integer()) -> binary().

Convert Unix timestamp to ISO8601 format.

Example:

ocibuild_time:unix_to_iso8601(0).
%% => ~"1970-01-01T00:00:00Z"

ocibuild_time:unix_to_iso8601(1700000000).
%% => ~"2023-11-14T22:13:20Z"