Skip to main content

Crate anytime

Crate anytime 

Source
Expand description

Astronomical time scales and conversions with nanosecond resolution.

Time stores an instant with a type-level Scale. The scale marker prevents accidental mixing of values while the conversion implementations make changes of scale explicit:

use anytime::{Time, scales::{TAI, UTC}};

let utc = Time::<UTC>::from_jd(2_451_545.0);
let tai: Time<TAI> = utc.into();

Values can also be compared and differenced directly, even when they use different scales. Subtraction returns a chrono::TimeDelta, while comparisons use standard ordering operators:

use anytime::{Time, scales::{TAI, UTC}};
use chrono::TimeDelta;

let earlier = Time::<UTC>::from_jd(2_451_545.0);
let later: Time<TAI> = Time::<UTC>::from_jd(2_451_546.0).into();
let is_ordered = earlier < later;
let difference = later - earlier;

A value can be formatted as a Gregorian date string, including sub-second precision, through Time::gregorian:

use anytime::{Time, scales::UTC};
use chrono::NaiveDate;

let date = NaiveDate::from_ymd_opt(2000, 1, 1)
    .unwrap()
    .and_hms_nano_opt(12, 0, 0, 123_456_789)
    .unwrap();
let date_string = Time::<UTC>::from_gregorian(date)
    .gregorian()
    .to_string();

When the scale is selected at runtime, use AnyTime’s constructors with TimeScale:

use anytime::{AnyTime, TimeScale};

let utc = AnyTime::from_isot_str("2000-01-01T12:00:00", TimeScale::UTC).unwrap();

When the scale for a whole collection is selected at runtime, use AnyTimeSeries. It stores the scale once while retaining a homogeneous typed series internally:

use anytime::{AnyTimeSeries, Time, TimeSeries, scales::{TAI, UTC}};

let tai = TimeSeries::new(vec![Time::<TAI>::from_jd(2_451_545.0)]);
let series: AnyTimeSeries = tai.into();
let utc: TimeSeries<UTC> = series.into();

assert_eq!(utc.len(), 1);

Convert a runtime-selected value to another runtime-selected scale with AnyTime::convert:

use anytime::{AnyTime, TimeScale};

let utc = AnyTime::from_jd(2_451_545.0, TimeScale::UTC);
let tai = utc.convert(TimeScale::TAI);

Typed values also support the same parsing constructors. std::str::FromStr assumes an ISO 8601 T-separated date and time without an offset, interpreted in the type’s scale:

use anytime::{Time, scales::UTC};

let utc: Time<UTC> = "2000-01-01T12:00:00".parse().unwrap();

The crate supports UTC, TAI, TT, TCG, TCB, TDB, and UT1. Its GNSS scales are BDT, GLONASST, GPST, GST, and QZZST. UTC conversions use leap-second data built into the crate. UT1 conversions additionally use Earth-orientation data loaded with load_finals2000a.

§Feature overview

Re-exports§

pub use scales::TimeScale;
pub use scales::TimeScaleParseError;
pub use scales::BDT;
pub use scales::GLONASST;
pub use scales::GPS;
pub use scales::GPST;
pub use scales::GST;
pub use scales::QZZST;
pub use scales::TAI;
pub use scales::TCB;
pub use scales::TCG;
pub use scales::TDB;
pub use scales::TDT;
pub use scales::TT;
pub use scales::UT;
pub use scales::UT1;
pub use scales::UTC;

Modules§

scales
Marker types and runtime selection for the time scales supported by crate::Time.

Structs§

AnyTimeSeriesIntoIter
An owning iterator over values in an AnyTimeSeries.
AnyTimeSeriesIter
An iterator that borrows an AnyTimeSeries and yields AnyTime values.
Time
A time value associated with a specific time scale.
TimeSeries
A collection of time values associated with a specific time scale.
TimeSeriesRange
A lazily generated range of times with an exclusive end bound.

Enums§

AnyTime
A time value whose scale is selected at runtime.
AnyTimeSeries
A collection of time values whose common scale is selected at runtime.
FinalsLoadError
Errors encountered while loading finals2000A.all.

Traits§

Scale
Describes a time scale that can parameterize Time.

Functions§

load_finals2000a
Load and atomically replace the shared finals2000A dataset.