Functional Weave
Code in Python

time.unix-to-iso@1.0.0

impl/python.py

969 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .dates_add_days import iso_from_epoch_day  ← from dates.add-days ^1.0.0 · built alongside by fune

#: 0001-01-01T00:00:00Z and 9999-12-31T23:59:59Z in Unix seconds.
MIN_SECONDS = -62135596800
MAX_SECONDS = 253402300799


def unix_to_iso(seconds: int) -> str:
    """Unix seconds as YYYY-MM-DDTHH:MM:SSZ.

    Written on dates.add-days rather than datetime so the calendar comes from
    the same code in every language; // floors, so a negative time falls on
    the day before the epoch.
    """
    if isinstance(seconds, bool) or not isinstance(seconds, int):
        raise TypeError("seconds must be a whole number, received %s" % (seconds,))
    if seconds < MIN_SECONDS or seconds > MAX_SECONDS:
        raise ValueError(
            "seconds must be from %d to %d (years 0001 to 9999), received %d" % (MIN_SECONDS, MAX_SECONDS, seconds)
        )
    day = seconds // 86400
    rest = seconds - day * 86400
    return "%sT%02d:%02d:%02dZ" % (iso_from_epoch_day(day), rest // 3600, (rest % 3600) // 60, rest % 60)