Skip to content

Epoch and Duration

STKCraft edited this page Nov 12, 2026 · 1 revision

Epoch and duration

Epoch

pub struct Epoch { /* i64 seconds + u32 nanoseconds */ }

An Epoch is a continuous TAI instant stored as whole seconds plus nanoseconds since 2000-01-01T00:00:00 TAI. Because TAI has no leap seconds, the value is monotonic and Ord is meaningful.

Construction

Function Behaviour
Epoch::from_parts(sec, nanos) Normalises nanoseconds; panics on overflow
Epoch::from_tai_seconds(f64) Panics on NaN, infinity or overflow
Epoch::checked_from_parts Returns Option<Epoch> for untrusted input
Epoch::checked_from_tai_seconds Returns Option<Epoch> for untrusted input

The unchecked forms deliberately panic rather than wrap. A silent wrap at i64::MAX would move an epoch by 585 billion years; failing loudly is the only safe behaviour for a time library.

Access and arithmetic

epoch.sec();            // i64 whole TAI seconds
epoch.nanos();          // u32, always < 1e9
epoch.tai_seconds();    // f64
epoch.checked_add(Duration);
epoch.checked_sub(Duration);

Add/Sub with a Duration are available and panic on overflow; the checked_* forms return Option.

Duration

pub struct Duration { /* i64 seconds + i32 nanoseconds, normalised */ }
  • nanos() is always in 0..1e9; the sign lives in the seconds field.
  • total_nanoseconds() returns i128 and is exact for the whole domain.
  • checked_neg, checked_add, checked_sub, checked_mul_i64 all compute in i128 and accept representable extrema, including a - a = 0 for the most negative value.
  • floor(step), ceil(step) and round(step) work on timestamps with a grid step.

TimeSeries

TimeSeries::inclusive(start, end, step);
TimeSeries::exclusive(start, end, step);
  • A zero or negative step yields an empty iterator rather than looping.
  • The cursor uses checked addition, so the last representable epoch is delivered before iteration ends instead of overflowing.
  • The series iterates Epoch values; format them through the conversions.

Nanoseconds are a representation step

The nanosecond field makes the representation reversible and keeps UTC fractions from being lost in f64. It does not claim nanosecond accuracy: the accuracy of any result is bounded by the model and the data, as stated in TDB models and Verification.

no_std

Every type on this page is available in no_std builds with alloc. Arithmetic is integer-based; no floating-point accumulation is used for Epoch or Duration. See no_std and embedded.

Clone this wiki locally