Skip to main content

LeapSeconds

Struct LeapSeconds 

Source
pub struct LeapSeconds { /* private fields */ }
Expand description

An immutable leap-second schedule.

Use LeapSeconds::known() to get the built-in table with all historical leap seconds through 2017-01-01. Works in no_std, no allocation, deterministic.

For custom tables (testing, simulation), use LeapSeconds::builder() (requires std feature).

LeapSeconds is Send + Sync — safe to share across threads. The known() table returns &'static LeapSeconds, so it can be used from any thread without cloning.

Implementations§

Source§

impl LeapSeconds

Source

pub fn known() -> &'static Self

Returns the built-in table with all historical leap seconds through 2017-01-01.

Works in no_std, requires no allocation, and is fully deterministic. Timestamps after 2017-01-01 use the last known offset (37) because no new leap seconds have been inserted since then.

For custom tables, see LeapSeconds::builder().

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let tai = leaps.utc_to_tai(UtcUnixSeconds(1_700_000_000)).unwrap();
assert_eq!(tai, TaiSeconds(1_700_000_037));
Source

pub fn utc_to_tai(&self, utc: UtcUnixSeconds) -> Result<TaiSeconds, Error>

Convert a UTC Unix timestamp to TAI seconds.

§Errors

Returns Error::OutOfRange if utc is before the first entry in the table.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let tai = leaps.utc_to_tai(UtcUnixSeconds(1_700_000_000)).unwrap();
assert_eq!(tai, TaiSeconds(1_700_000_037));
Source

pub fn utc_to_tai_nanos(&self, utc: UtcUnixNanos) -> Result<TaiNanos, Error>

Convert UTC Unix nanoseconds to TAI nanoseconds.

The offset is applied in whole seconds; the sub-second fraction is preserved exactly.

§Errors

Returns Error::OutOfRange if the timestamp is before 1972-01-01.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let tai = leaps.utc_to_tai_nanos(UtcUnixNanos(1_700_000_000_500_000_000)).unwrap();
assert_eq!(tai, TaiNanos(1_700_000_037_500_000_000));
Source

pub fn tai_to_utc(&self, tai: TaiSeconds) -> Result<UtcUnixSeconds, Error>

Convert TAI seconds to a UTC Unix timestamp.

§Errors

Returns Error::OutOfRange if tai is before the first entry in the table.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let utc = leaps.tai_to_utc(TaiSeconds(1_700_000_037)).unwrap();
assert_eq!(utc, UtcUnixSeconds(1_700_000_000));
Source

pub fn tai_to_utc_nanos(&self, tai: TaiNanos) -> Result<UtcUnixNanos, Error>

Convert TAI nanoseconds to UTC Unix nanoseconds.

§Errors

Returns Error::OutOfRange if the timestamp is before the table’s TAI range.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let utc = leaps.tai_to_utc_nanos(TaiNanos(1_700_000_037_500_000_000)).unwrap();
assert_eq!(utc, UtcUnixNanos(1_700_000_000_500_000_000));
Source

pub fn utc_to_gpst(&self, utc: UtcUnixSeconds) -> Result<GpstSeconds, Error>

Convert a UTC Unix timestamp to GPS Time.

Composes utc_to_tai then tai_to_gpst (TAI − 19).

§Errors

Returns Error::OutOfRange if the timestamp is before 1972-01-01.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let gpst = leaps.utc_to_gpst(UtcUnixSeconds(1_700_000_000)).unwrap();
assert_eq!(gpst, GpstSeconds(1_700_000_018));
Source

pub fn utc_to_gpst_nanos(&self, utc: UtcUnixNanos) -> Result<GpstNanos, Error>

Convert UTC Unix nanoseconds to GPST nanoseconds.

§Errors

Returns Error::OutOfRange if the timestamp is before 1972-01-01.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let gpst = leaps.utc_to_gpst_nanos(UtcUnixNanos(1_700_000_000_500_000_000)).unwrap();
assert_eq!(gpst, GpstNanos(1_700_000_018_500_000_000));
Source

pub fn gpst_to_utc(&self, gpst: GpstSeconds) -> Result<UtcUnixSeconds, Error>

Convert GPS Time to a UTC Unix timestamp.

Composes gpst_to_tai (GPST + 19) then tai_to_utc.

§Errors

Returns Error::OutOfRange if the resulting TAI is before the table’s range.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let utc = leaps.gpst_to_utc(GpstSeconds(1_700_000_018)).unwrap();
assert_eq!(utc, UtcUnixSeconds(1_700_000_000));
Source

pub fn gpst_to_utc_nanos(&self, gpst: GpstNanos) -> Result<UtcUnixNanos, Error>

Convert GPST nanoseconds to UTC Unix nanoseconds.

§Errors

Returns Error::OutOfRange if the resulting TAI is before the table’s range.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let utc = leaps.gpst_to_utc_nanos(GpstNanos(1_700_000_018_500_000_000)).unwrap();
assert_eq!(utc, UtcUnixNanos(1_700_000_000_500_000_000));
Source

pub fn tai_utc_offset(&self, utc: UtcUnixSeconds) -> Result<i32, Error>

Get the TAI−UTC offset at a given UTC instant.

§Errors

Returns Error::OutOfRange if the timestamp is before 1972-01-01.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
assert_eq!(leaps.tai_utc_offset(UtcUnixSeconds(1_700_000_000)).unwrap(), 37);
assert_eq!(leaps.tai_utc_offset(UtcUnixSeconds(63_072_000)).unwrap(), 10);
Source

pub fn tai_utc_offset_at_tai(&self, tai: TaiSeconds) -> Result<i32, Error>

Get the TAI−UTC offset at a given TAI instant.

§Errors

Returns Error::OutOfRange if the TAI timestamp is before the table’s range.

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
assert_eq!(leaps.tai_utc_offset_at_tai(TaiSeconds(1_700_000_037)).unwrap(), 37);
Source

pub fn is_during_leap_second(&self, utc: UtcUnixSeconds) -> bool

Returns true if utc falls exactly on a positive leap-second insertion.

At such an instant the POSIX timestamp is ambiguous: the UTC wall clock reads 23:59:60 but POSIX folds it to the same value as 00:00:00.

Returns false for:

  • The initial 1972-01-01 epoch (offset = 10) — not an insertion, it is the starting offset of the modern UTC system.
  • Negative leap seconds (where the offset decreases) — there is no extra second, so no ambiguity. No negative leap second has ever been applied, but the table format supports them.
§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();

// 2017-01-01 — the last leap second insertion (2016-12-31 23:59:60)
assert!(leaps.is_during_leap_second(UtcUnixSeconds(1_483_228_800)));

// A normal timestamp — not during a leap second
assert!(!leaps.is_during_leap_second(UtcUnixSeconds(1_700_000_000)));
Source

pub fn valid_range(&self) -> (UtcUnixSeconds, UtcUnixSeconds)

Returns the valid range of this table as (first_entry, last_entry).

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let (start, end) = leaps.valid_range();
assert_eq!(start, UtcUnixSeconds(63_072_000));    // 1972-01-01
assert_eq!(end, UtcUnixSeconds(1_483_228_800));   // 2017-01-01
Source

pub const fn is_expired(&self) -> bool

Returns false for the built-in known() table.

For tables with an expiration timestamp, this would return true if the table has expired. In v0.1 there is no clock access — this simply checks whether an expiration is set.

Source

pub fn expires_at(&self) -> Option<UtcUnixSeconds>

Returns the expiration timestamp, if one was set.

The built-in known() table returns None (it has no expiration concept). Tables constructed via the builder may have an expiration set.

§Example
use leap_sec::prelude::*;

assert_eq!(LeapSeconds::known().expires_at(), None);
Source

pub fn latest_entry(&self) -> (UtcUnixSeconds, i32)

Returns the most recent leap-second entry: (effective_utc, tai_minus_utc).

For the built-in table this is (2017-01-01, 37).

§Example
use leap_sec::prelude::*;

let leaps = LeapSeconds::known();
let (date, offset) = leaps.latest_entry();
assert_eq!(date, UtcUnixSeconds(1_483_228_800));
assert_eq!(offset, 37);
Source§

impl LeapSeconds

Source

pub const fn builder() -> LeapSecondsBuilder

Create a builder for constructing a custom leap-second table.

Trait Implementations§

Source§

impl Clone for LeapSeconds

Source§

fn clone(&self) -> LeapSeconds

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LeapSeconds

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.