1
- use hifitime:: { Duration , Epoch , TimeScale , Unit } ;
1
+ use hifitime:: { Epoch , Polynomial , TimeScale } ;
2
2
3
3
/// [TimePolynomial] allows precise [Epoch] translation to another [TimeScale].
4
4
/// For example, |[TimeScale::GPST]-[TimeScale::UTC]| when referencing [TimeScale::GPST] to [TimeScale::UTC].
@@ -9,8 +9,8 @@ pub struct TimePolynomial {
9
9
/// Reference [Epoch] expressed in the [TimeScale] in which the polynomials apply,
10
10
/// with 1 ns accuracy.
11
11
pub ref_epoch : Epoch ,
12
- /// Polynomials (s, s.s⁻¹, s.s⁻²)
13
- pub polynomials : ( f64 , f64 , f64 ) ,
12
+ /// [Polynomial]
13
+ pub polynomial : Polynomial ,
14
14
}
15
15
16
16
impl TimePolynomial {
@@ -21,14 +21,14 @@ impl TimePolynomial {
21
21
ref_week : u32 ,
22
22
ref_tow : u64 ,
23
23
ref_timescale : TimeScale ,
24
- polynomials : ( f64 , f64 , f64 ) ,
24
+ polynomial : Polynomial ,
25
25
) -> Self {
26
26
Self :: from_reference_time_of_week_nanos (
27
27
lhs_timescale,
28
28
ref_week,
29
29
ref_tow * 1_000_000_000 ,
30
30
ref_timescale,
31
- polynomials ,
31
+ polynomial ,
32
32
)
33
33
}
34
34
@@ -39,114 +39,14 @@ impl TimePolynomial {
39
39
ref_week : u32 ,
40
40
ref_tow : u64 ,
41
41
ref_timescale : TimeScale ,
42
- polynomials : ( f64 , f64 , f64 ) ,
42
+ polynomial : Polynomial ,
43
43
) -> Self {
44
44
let ref_epoch = Epoch :: from_time_of_week ( ref_week, ref_tow, ref_timescale) ;
45
45
46
46
Self {
47
47
ref_epoch,
48
48
lhs_timescale,
49
- polynomials ,
49
+ polynomial ,
50
50
}
51
51
}
52
-
53
- pub fn correction_duration ( & self , t : Epoch ) -> Duration {
54
- let t = t. to_time_scale ( self . lhs_timescale ) ;
55
- let t_ref = self . ref_epoch . to_time_scale ( self . lhs_timescale ) ;
56
-
57
- let dt_s = ( t - t_ref) . to_seconds ( ) ;
58
- let ( a0, a1, a2) = self . polynomials ;
59
-
60
- Duration :: from_seconds ( a0 + a1 * dt_s + a2 * dt_s. powi ( 2 ) )
61
- }
62
-
63
- /// Converts and corrects provided [Epoch] into provided [TimeScale], with 1 ns accuracy.
64
- pub fn epoch_time_correction ( & self , forward : bool , t : Epoch , timescale : TimeScale ) -> Epoch {
65
- let t_ref = self . ref_epoch . to_time_scale ( t. time_scale ) ;
66
-
67
- // supports any offset to reference instant.
68
- // While usually this should remain within the same week (or even much closer)
69
- let dt_s = ( t - t_ref) . to_seconds ( ) ;
70
-
71
- let ( a0, a1, a2) = self . polynomials ;
72
- let dt_s = a0 + a1 * dt_s + a2 * dt_s. powi ( 2 ) ;
73
-
74
- let converted = t. to_time_scale ( timescale) ;
75
-
76
- if forward {
77
- converted - dt_s * Unit :: Second
78
- } else {
79
- converted + dt_s * Unit :: Second
80
- }
81
- }
82
- }
83
-
84
- #[ cfg( test) ]
85
- mod test {
86
- use crate :: TimePolynomial ;
87
- use hifitime:: { Epoch , TimeScale , Unit } ;
88
-
89
- #[ test]
90
- fn test_time_polynomials ( ) {
91
- let t_ref_gpst = Epoch :: from_gregorian ( 2020 , 1 , 1 , 0 , 0 , 0 , 0 , TimeScale :: GPST ) ;
92
-
93
- let ( a0, a1, a2) = ( 1.0E-9 , 1.0E-10 , 0.0 ) ;
94
-
95
- let time_offset = TimePolynomial {
96
- ref_epoch : t_ref_gpst,
97
- polynomials : ( a0, a1, a2) ,
98
- lhs_timescale : TimeScale :: UTC ,
99
- } ;
100
-
101
- // +1 s
102
- let t = t_ref_gpst + 1.0 * Unit :: Second ;
103
-
104
- let t_utc = time_offset. epoch_time_correction ( true , t, TimeScale :: UTC ) ;
105
-
106
- assert_eq ! ( t_utc. time_scale, TimeScale :: UTC ) ;
107
-
108
- assert_eq ! (
109
- t_utc,
110
- t. to_time_scale( TimeScale :: UTC ) - ( a0 * Unit :: Second + 1.0 * Unit :: Second * a1)
111
- ) ;
112
-
113
- let reversed = time_offset. epoch_time_correction ( false , t_utc, TimeScale :: GPST ) ;
114
- assert_eq ! ( reversed, t) ;
115
-
116
- // test backwards
117
- let backwards = time_offset. epoch_time_correction ( false , t, TimeScale :: GPST ) ;
118
- assert_eq ! ( backwards. time_scale, TimeScale :: GPST ) ;
119
-
120
- // +1 ns
121
- let t = t_ref_gpst + 1.0 * Unit :: Nanosecond ;
122
-
123
- let t_utc = time_offset. epoch_time_correction ( true , t, TimeScale :: UTC ) ;
124
-
125
- assert_eq ! ( t_utc. time_scale, TimeScale :: UTC ) ;
126
-
127
- let correction_s = a0 + 1.0E-9 * a1;
128
- assert_eq ! (
129
- t_utc,
130
- t. to_time_scale( TimeScale :: UTC ) - correction_s * Unit :: Second
131
- ) ;
132
-
133
- let reversed = time_offset. epoch_time_correction ( false , t_utc, TimeScale :: GPST ) ;
134
- assert_eq ! ( reversed, t) ;
135
-
136
- // +1 us
137
- let t = t_ref_gpst + 1000.0 * Unit :: Nanosecond ;
138
-
139
- let t_utc = time_offset. epoch_time_correction ( true , t, TimeScale :: UTC ) ;
140
-
141
- assert_eq ! ( t_utc. time_scale, TimeScale :: UTC ) ;
142
-
143
- let correction_s = a0 + 1000.0E-9 * a1;
144
- assert_eq ! (
145
- t_utc,
146
- t. to_time_scale( TimeScale :: UTC ) - correction_s * Unit :: Second
147
- ) ;
148
-
149
- let reversed = time_offset. epoch_time_correction ( false , t_utc, TimeScale :: GPST ) ;
150
- assert_eq ! ( reversed, t) ;
151
- }
152
52
}
0 commit comments