Functional Weave
Code in Rust

time.unix-to-iso@1.0.0

README.md

1,365 bytes · view raw

# time.unix-to-iso

`unixToIso(1234567890)` is `"2009-02-13T23:31:30Z"`. An API that reads the
clock once (`time.time()`, `Date.now() / 1000`) and needs to show a moment to a
person or another program, an account's `createdAt` or a token's `expiresAt`,
formats it with this, so every language writes the same string.

The output is the one fixed shape `YYYY-MM-DDTHH:MM:SSZ`: UTC, a `Z` rather
than `+00:00`, whole seconds and no fraction. It is valid ISO 8601 and RFC 3339,
sorts correctly as text, and parses with `Date.parse`, Python's
`datetime.fromisoformat` (3.11 and later) and `time.iso-to-unix`.

Seconds before 1970 are negative and are floored, not truncated: `-1` is
`1969-12-31T23:59:59Z`, where dividing by 86,400 and rounding towards zero
would give a time on 1970-01-01. The calendar arithmetic is
`dates.add-days`'s, so leap years (2000 was one, 2100 will not be) come from
one place.

The range is what a four-digit year can write, 0001-01-01T00:00:00Z
(-62135596800) to 9999-12-31T23:59:59Z (253402300799); anything outside it,
or a fractional second count, is an error. Milliseconds (JavaScript's
`Date.now()`) must be divided by 1000 and floored first; passing them
unchanged lands in the year 58,000 and is refused.

Unix time does not count leap seconds (POSIX.1-2017, Base Definitions, 4.16
"Seconds Since the Epoch"), so neither does this.