[−][src]Crate hifitime
hifitime
Precise date and time handling in Rust built on top of a simple f64. The Epoch used is TAI Epoch of 01 Jan 1900 at midnight.
Features
 Leap seconds (as announced by the IETF on a yearly basis)
 Julian dates and Modified Julian dates
 Clock drift via oscillator stability for simulation of time measuring hardware (via the
simulation
feature)  UTC representation with ISO8601 formatting (and parsing in that format #45)
 High fidelity Ephemeris Time / Dynamic Barycentric Time (TDB) computations from ESA's Navipedia (caveat: up to 10ms difference with SPICE near 01 Jan 2000)
 Trivial support of time arithmetic (e.g.
2 * TimeUnit::Hour + TimeUnit::Second * 3
)  Supports ranges of Epochs and TimeSeries (linspace of
Epoch
s andDuration
s)
Almost all examples are validated with external references, as detailed on a testbytest basis.
Leap second support
Each time computing library may decide when the extra leap second exists as explained
in the IETF leap second reference.
To ease computation, hifitime
decides that second is the 60th of a UTC date, if such exists.
Note that this second exists at a different time than defined on
NASA HEASARC. That tool is
used for validation of Julian dates. As an example of how this is handled, check the Julian
day computations for 20150630 23:59:59,
20150630 23:59:60
and 20150701 00:00:00.
Does not include
 Dates only, or times only (i.e. handles only the combination of both), but the
Datetime::{at_midnight, at_noon}
help  Custom formatting of date time objects
 An initializer from machine time
Usage
Put this in your Cargo.toml
:
[dependencies]
hifitime = "2"
And add the following to your crate root:
extern crate hifitime;
Examples:
Time creation
use hifitime::{Epoch, TimeUnit}; use std::str::FromStr; let mut santa = Epoch::from_gregorian_utc(2017, 12, 25, 01, 02, 14, 0); assert_eq!(santa.as_mjd_utc_days(), 58112.043217592596); assert_eq!(santa.as_jde_utc_days(), 2458112.5432175924); santa += 3600 * TimeUnit::Second; assert_eq!( santa, Epoch::from_gregorian_utc(2017, 12, 25, 02, 02, 14, 0), "Could not add one hour to Christmas" ); let dt = Epoch::from_gregorian_utc(2017, 1, 14, 0, 31, 55, 0); assert_eq!(dt, Epoch::from_str("20170114T00:31:55 UTC").unwrap()); // And you can print it too, although by default it will print in TAI assert_eq!(dt.as_gregorian_utc_str(), "20170114T00:31:55 UTC".to_string()); assert_eq!(format!("{}", dt), "20170114T00:32:32 TAI".to_string());
Time differences, time unit, and duration handling
Comparing times will lead to a Duration type. Printing that will automatically select the unit.
use hifitime::{Epoch, TimeUnit, Duration}; let at_midnight = Epoch::from_gregorian_utc_at_midnight(2020, 11, 2); let at_noon = Epoch::from_gregorian_utc_at_noon(2020, 11, 2); assert_eq!(at_noon  at_midnight, 12 * TimeUnit::Hour); assert_eq!(at_noon  at_midnight, 1 * TimeUnit::Day / 2); assert_eq!(at_midnight  at_noon, 1 * TimeUnit::Day / 2); let delta_time = at_noon  at_midnight; assert_eq!(format!("{}", delta_time), "12 h 0 min 0 s".to_string()); // And we can multiply durations by a scalar... let delta2 = 2 * delta_time; assert_eq!(format!("{}", delta2), "1 days 0 h 0 min 0 s".to_string()); // Or divide them by a scalar. assert_eq!(format!("{}", delta2 / 2.0), "12 h 0 min 0 s".to_string()); // And of course, these comparisons account for differences in time systems let at_midnight_utc = Epoch::from_gregorian_utc_at_midnight(2020, 11, 2); let at_noon_tai = Epoch::from_gregorian_tai_at_noon(2020, 11, 2); assert_eq!(format!("{}", at_noon_tai  at_midnight_utc), "11 h 59 min 23 s".to_string());
Iterating over times ("linspace" of epochs)
Finally, something which may come in very handy, line spaces between times with a given step.
use hifitime::{Epoch, TimeUnit, TimeSeries}; let start = Epoch::from_gregorian_utc_at_midnight(2017, 1, 14); let end = Epoch::from_gregorian_utc_at_noon(2017, 1, 14); let step = 2 * TimeUnit::Hour; let time_series = TimeSeries::inclusive(start, end, step); let mut cnt = 0; for epoch in time_series { println!("{}", epoch); cnt += 1 } // Check that there are indeed six twohour periods in a half a day, // including start and end times. assert_eq!(cnt, 6)
Limitations
Barycentric Dynamical Time is computed using the ESA Navipedia reference. In three separate examples, the error with SPICE Ephemeris Time is the following: * 9.536743e07 seconds for 2012Feb7 11:22:33 UTC * 3.814697e06 seconds for 2002Feb7 midnight UTC * 4.291534e06 seconds for 1996Feb7 11:22:33 UTC
Structs
ClockNoise  ClockNoise adds true clock drift to a given Duration measurement. For example, if a vehicle is
measuring the time of flight of a signal with high precision oscillator, the engineering
specifications will include the oscillator stability. This specification bounds the preciseness
of time span calculations. On very short time spans, i.e. less than a few minutes, clock drift
is usually negligible. However, in several high fidelity systems the clock drift may lead to
a significant error (e.g. several kilometers in twoway radar ranging). This module allows high
fidelity simulation systems to test the resilience of algorithms with oscillator stability.
The constructors here are specified in parts per million: for a parts per billion specification
simply multiply the value by 
Duration  Defines generally usable durations for high precision math with Epoch (all data is stored in seconds) 
Epoch  Defines an Epoch in TAI (temps atomique international) in seconds past 1900 January 01 at midnight (like the Network Time Protocol). 
TimeSeries  An iterator of a sequence of evenly spaced Epochs. 
Enums
Errors  Errors handles all oddities which may occur in this library. 
TimeSystem  Enum of the different time systems available 
TimeUnit 
Constants
DAYS_PER_CENTURY 

DAYS_PER_YEAR 

ET_EPOCH_S  The Ephemeris Time epoch, in seconds 
J1900_NAIF  
J1900_OFFSET 

J2000_NAIF  
J2000_OFFSET 

MJD_OFFSET  Modified Julian Date in seconds as defined here. MJD epoch is Modified Julian Day at 17 November 1858 at midnight. 
SECONDS_PER_DAY 

SECONDS_PER_HOUR 

SECONDS_PER_MINUTE 

SECONDS_PER_SIDERAL_YEAR 

SECONDS_PER_TROPICAL_YEAR 

SECONDS_PER_YEAR 

Functions
is_gregorian_valid  Returns true if the provided Gregorian date is valid. Leap second days may have 60 seconds. 
Type Definitions
Decimal  Decimal defines a lossless fraction and is the basis of all Epoch computations. It is recommended to use this time for time operations. 