1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
pub const J1900_NAIF: f64 = 2_415_020.0;
pub const J2000_NAIF: f64 = 2_451_545.0;
pub const J1900_OFFSET: f64 = 15_020.0;
pub const J2000_OFFSET: f64 = 51_544.5;
pub const ET_EPOCH_S: i64 = 3_155_716_800;
pub const MJD_OFFSET: f64 = 2_400_000.5;
pub const JDE_OFFSET_DAYS: f64 = J1900_OFFSET + MJD_OFFSET;
pub const JDE_OFFSET_SECONDS: f64 = JDE_OFFSET_DAYS * SECONDS_PER_DAY;
pub const DAYS_PER_YEAR: f64 = 365.25;
pub const DAYS_PER_CENTURY: f64 = 36525.0;
pub const DAYS_PER_CENTURY_I64: i64 = 36525;
pub const SECONDS_PER_MINUTE: f64 = 60.0;
pub const SECONDS_PER_HOUR: f64 = 3_600.0;
pub const SECONDS_PER_DAY: f64 = 86_400.0;
pub const SECONDS_PER_DAY_I64: i64 = 86_400;
pub const SECONDS_PER_CENTURY: f64 = SECONDS_PER_DAY * DAYS_PER_CENTURY;
pub const SECONDS_PER_YEAR: f64 = 31_557_600.0;
pub const SECONDS_PER_YEAR_I64: i64 = 31_557_600;
pub const SECONDS_PER_TROPICAL_YEAR: f64 = 31_556_925.974_7;
pub const SECONDS_PER_SIDERAL_YEAR: f64 = 31_558_150.0;
pub const SECONDS_GPS_TAI_OFFSET: f64 = 80.0 * SECONDS_PER_YEAR + 4.0 * SECONDS_PER_DAY + 19.0;
pub const SECONDS_GPS_TAI_OFFSET_I64: i64 =
80 * SECONDS_PER_YEAR_I64 + 4 * SECONDS_PER_DAY_I64 + 19;
pub const DAYS_GPS_TAI_OFFSET: f64 = SECONDS_GPS_TAI_OFFSET / SECONDS_PER_DAY;
pub const UNIX_REF_EPOCH: Epoch = Epoch::from_tai_duration(Duration {
centuries: 0,
nanoseconds: 2_208_988_800_000_000_000,
});
mod epoch;
pub use epoch::*;
mod duration;
pub use duration::*;
mod timeseries;
pub use timeseries::*;
pub mod prelude {
pub use {Duration, Epoch, Freq, Frequencies, TimeSeries, TimeUnits, Unit};
}
extern crate num_traits;
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
extern crate core;
use core::convert;
use core::fmt;
use core::num::ParseIntError;
use core::str::FromStr;
#[cfg(feature = "std")]
extern crate regex;
#[cfg(feature = "std")]
extern crate serde_derive;
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Errors {
Carry,
ParseError(ParsingErrors),
ConversionOverlapError(f64, f64),
Overflow,
SystemTimeError,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ParsingErrors {
ParseIntError,
TimeSystem,
ISO8601,
UnknownFormat,
UnknownUnit,
UnsupportedTimeSystem,
}
impl fmt::Display for Errors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Carry => write!(f, "a carry error (e.g. 61 seconds)"),
Self::ParseError(kind) => write!(f, "ParseError: {:?}", kind),
Self::ConversionOverlapError(hi, lo) => {
write!(f, "hi and lo values overlap: {}, {}", hi, lo)
}
Self::Overflow => write!(
f,
"overflow occured when trying to convert Duration information"
),
Self::SystemTimeError => write!(f, "std::time::SystemTime returned an error"),
}
}
}
impl convert::From<ParseIntError> for Errors {
fn from(_: ParseIntError) -> Self {
Errors::ParseError(ParsingErrors::ParseIntError)
}
}
#[cfg(feature = "std")]
impl Error for Errors {}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TimeSystem {
ET,
TAI,
TT,
TDB,
UTC,
}
impl FromStr for TimeSystem {
type Err = Errors;
fn from_str(val: &str) -> Result<Self, Self::Err> {
if val == "UTC" {
Ok(TimeSystem::UTC)
} else if val == "TT" {
Ok(TimeSystem::TT)
} else if val == "TAI" {
Ok(TimeSystem::TAI)
} else if val == "TDB" {
Ok(TimeSystem::TDB)
} else if val == "ET" {
Ok(TimeSystem::ET)
} else {
Err(Errors::ParseError(ParsingErrors::TimeSystem))
}
}
}
#[cfg(test)]
mod tests {
use crate::{Errors, ParsingErrors, TimeSystem};
#[test]
fn enum_eq() {
assert!(Errors::Carry == Errors::Carry);
assert!(ParsingErrors::ParseIntError == ParsingErrors::ParseIntError);
assert!(TimeSystem::ET == TimeSystem::ET);
}
}