Skip to content

Formatting

STKCraft edited this page Nov 12, 2026 · 1 revision

Formatting

All formatters live in stkcraft_time::format. They take a UtcDateTime plus a precision and return a String; none of them performs a conversion or reads a kernel by itself.

Function Output
iso8601(dt, p) 2000-01-01T12:00:00.000Z
rfc3339(dt, p) 2000-01-01T12:00:00.000+00:00
rfc2822(dt) Sat, 01 Jan 2000 12:00:00 +0000
spice_calendar(dt, p) 2000 JAN 01 12:00:00.000
strftime(dt, pattern) C89 tokens, see below
etcal(et, p) SPICE calendar string from ET, no kernel

Precision

  • Fractional digits are clamped to 9, the resolution of the internal representation. Larger values allocate memory without adding data.
  • %f in strftime is always nanoseconds.

Rounding and carry

Rounding carries into the next minute, hour, day, month and year; it never prints a second that does not exist.

A leap second can only be inserted or removed in the last minute of a day, so the day length applies only when hour == 23 && minute == 59:

Day length Valid seconds Rounding at 59.9999 (p = 3)
86 401 s 0..61 23:59:60.000
86 400 s 0..60 next minute (00:00:00 at 23:59)
86 399 s 0..59 next minute

Every other minute of a leap-second day still ends at 59.999:

2016-12-31 12:34:59.9999  (p=3)  ->  2016-12-31T12:35:00.000Z
2016-12-31 23:58:59.9999  (p=3)  ->  2016-12-31T23:59:00.000Z
2016-12-31 23:59:59.9999  (p=3)  ->  2016-12-31T23:59:60.000Z

To know whether 23:59 is 60 seconds or 61, the formatter needs the leap second table. That is what FormatOptions supplies.

FormatOptions

use stkcraft_time::calendar::Calendar;
use stkcraft_time::format::FormatOptions;

let options = FormatOptions::new(3)
    .with_calendar(Calendar::Julian)
    .with_leap_seconds(&lsk);
Method Effect
FormatOptions::new(precision) Gregorian calendar, no leap-second table
.with_calendar(Calendar) Calendar used when a carry moves the date
.with_leap_seconds(&LeapSecondTable) Day length for the final minute

Table-aware entry points: iso8601_with, rfc3339_with, spice_calendar_with, strftime_with, and Formatter::with_options.

Without a table, a day is assumed to be 86 400 s unless the value already has second >= 60, in which case a 86 401-second day is assumed so an actual leap second still prints. That assumption is documented, not hidden.

Calendars

The calendar decides how a carry moves the date and how %j, %a and %A are computed.

Julian  1500-02-28 23:59:59.9999  ->  1500-02-29T00:00:00.000Z
Gregorian 1500-02-28 23:59:59.9999 ->  1500-03-01T00:00:00.000Z

Calendar::Mixed switches at 1582-10-15 (GREGORIAN_SWITCH_JDN).

strftime tokens

Token Meaning
%Y %y four- and two-digit year
%m %d %e month, zero-padded day, space-padded day
%H %M %S hour, minute, second
%j day of year
%a %A short and long weekday
%b %B short and long month
%z %Z +0000, UTC
%f nanoseconds
%% literal %

An unsupported token returns TimeError::InvalidFormat.

etcal

etcal converts ET (TDB seconds past J2000) to a SPICE calendar string without a leap-second kernel, matching NAIF etcal.

It splits the value into whole days and seconds before adding an absolute Julian Date. Adding a microsecond to 2451545.0 would round it away at f64 resolution, so the split is what preserves microseconds:

etcal(0.000001, 9)  ->  2000 JAN 01 12:00:00.000001000
etcal(-0.000001, 9) ->  2000 JAN 01 11:59:59.999999000
etcal(NaN, 9)       ->  INVALID ET

A non-finite ET returns the literal INVALID ET rather than a fabricated date.

Round-tripping

Formatting is a presentation step. iso8601 output is not parsed back by this crate: there is no str2et-style string parser, and no attempt is made to guess a calendar or a leap-second policy from text. Construct UtcDateTime explicitly.

Clone this wiki locally