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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! This module contains the different time representations of the interpreter.
//!
//! ## Time Representations
//! The RTLola interpreter supports multiple representations of time in its input and output.
//! If run in offline mode, meaning the time for an event is parsed from the input source,
//! the format in which the time is present in the input has to be set. Consider the following example CSV file:
//!
//! <pre>
//! a,b,time
//! 0,1,0.1
//! 2,3,0.2
//! 4,5,0.3
//! </pre>
//!
//! The supported time representations are:
//!
//! #### Relative
//! Time is considered relative to a fixed point in time. Call this point in time `x` then in the example above
//! the first event gets the timestamp `x + 0.1`, the second one `x + 0.2` and so forth.
//!
//! #### Incremental
//! Time is considered relative to the preceding event. This induces the following timestamps for the above example:
//!
//! <pre>
//! a,b, time
//! 0,1, x + 0.1
//! 2,3, x + 0.3
//! 4,5, x + 0.6
//! </pre>
//!
//! #### Absolute
//! Time is parsed as absolute timestamps.
//!
//! **Note**: The evaluation of periodic streams depends on the time passed between events.
//! Depending on the representation, determining the time that passed before the first event is not obvious.
//! While the relative and incremental representations do not strictly need a point of reference to determine
//! the time passed, the absolute representation requires such a point of reference.
//! This point of time can either be directly supplied during configuration using the [start_time](crate::ConfigBuilder::start_time) method
//! or inferred as the time of the first event.
//! The latter consequently assumes that no time has passed before the first event in the input.

use std::fmt::Debug;
use std::ops::Sub;
use std::str::FromStr;
use std::sync::RwLock;
use std::time::{Duration, SystemTime};

#[cfg(not(feature = "serde"))]
use humantime::Rfc3339Timestamp;
use lazy_static::lazy_static;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{CondDeserialize, CondSerialize, Time};

const NANOS_IN_SECOND: u64 = 1_000_000_000;

lazy_static! {
    /// Used to synchronise the start time across the program
    static ref START_TIME: RwLock<Option<SystemTime>> = RwLock::new(None);
}

pub(crate) fn init_start_time<T: TimeRepresentation>(start_time: Option<SystemTime>) {
    *START_TIME.write().unwrap() = start_time.or_else(T::default_start_time);
}

/// Precisely parses an duration from a string of the form '{secs}.{sub-secs}'
pub fn parse_float_time(s: &str) -> Result<Duration, String> {
    let num = Decimal::from_str(s).map_err(|e| e.to_string())?;
    let nanos = (num.fract() * Decimal::from(NANOS_IN_SECOND))
        .to_u32()
        .ok_or("Could not convert nano seconds")?;
    let secs = num.trunc().to_u64().ok_or("Could not convert seconds")?;
    Ok(Duration::new(secs, nanos))
}

/// The functionality a time format has to provide.
pub trait TimeRepresentation: TimeMode + Default + Clone + Send + CondSerialize + CondDeserialize + 'static {
    /// The internal representation of the time format.
    type InnerTime: Debug + Clone + Clone + Send + CondSerialize + CondDeserialize;

    /// Convert from the internal time representation to the monitor time.
    fn convert_from(&mut self, inner: Self::InnerTime) -> Time;
    /// Convert from monitor time to the internal representation.
    fn convert_into(&self, ts: Time) -> Self::InnerTime;

    /// Convert the internal representation into a string.
    fn to_string(&self, ts: Self::InnerTime) -> String;
    /// Parse the internal representation from a string and convert it into monitor time.
    fn parse(s: &'_ str) -> Result<Self::InnerTime, String>;

    /// Returns a default start time if applicable for the time representation.
    fn default_start_time() -> Option<SystemTime> {
        Some(SystemTime::now())
    }
}

/// This trait captures whether the time is given explicitly through a timestamp or is indirectly obtained through measurements.
pub trait TimeMode {
    /// Returns whether the time [TimeRepresentation] require an explicit timestamp
    fn requires_timestamp() -> bool {
        true
    }
}

/// This trait captures the subset [TimeRepresentation]s suitable for output.
pub trait OutputTimeRepresentation: TimeRepresentation {}

/// Time represented as the unsigned number of nanoseconds relative to a fixed start time.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct RelativeNanos {}

impl TimeRepresentation for RelativeNanos {
    type InnerTime = u64;

    fn convert_from(&mut self, nanos: Self::InnerTime) -> Time {
        Duration::from_nanos(nanos)
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts.as_nanos() as u64
    }

    fn to_string(&self, ts: Self::InnerTime) -> String {
        ts.to_string()
    }

    fn parse(s: &'_ str) -> Result<u64, String> {
        u64::from_str(s).map_err(|e| e.to_string())
    }
}
impl OutputTimeRepresentation for RelativeNanos {}
impl TimeMode for RelativeNanos {}

/// Time represented as a positive real number representing seconds and sub-seconds relative to a fixed start time.
/// ie. 5.2
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct RelativeFloat {}

impl TimeRepresentation for RelativeFloat {
    type InnerTime = Duration;

    fn convert_from(&mut self, ts: Self::InnerTime) -> Time {
        ts
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts
    }

    fn to_string(&self, ts: Self::InnerTime) -> String {
        let dur = self.convert_into(ts);
        format! {"{}.{:09}", dur.as_secs(), dur.subsec_nanos()}
    }

    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }
}
impl OutputTimeRepresentation for RelativeFloat {}
impl TimeMode for RelativeFloat {}

/// Time represented as the unsigned number in nanoseconds as the offset to the preceding event.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct OffsetNanos {
    current: Time,
    last_time: Time,
}

impl TimeRepresentation for OffsetNanos {
    type InnerTime = u64;

    fn convert_from(&mut self, raw: Self::InnerTime) -> Time {
        self.last_time = self.current;
        self.current += Duration::from_nanos(raw);
        self.current
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts.sub(self.last_time).as_nanos() as u64
    }

    fn to_string(&self, ts: Self::InnerTime) -> String {
        ts.to_string()
    }

    fn parse(s: &'_ str) -> Result<u64, String> {
        u64::from_str(s).map_err(|e| e.to_string())
    }
}
impl TimeMode for OffsetNanos {}

/// Time represented as a positive real number representing seconds and sub-seconds as the offset to the preceding event.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct OffsetFloat {
    current: Time,
    last_time: Time,
}

impl TimeRepresentation for OffsetFloat {
    type InnerTime = Duration;

    fn convert_from(&mut self, ts: Duration) -> Time {
        self.last_time = self.current;
        self.current += ts;
        self.current
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts - self.last_time
    }

    fn to_string(&self, ts: Time) -> String {
        let dur = self.convert_into(ts);
        format! {"{}.{:09}", dur.as_secs(), dur.subsec_nanos()}
    }

    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }
}

impl TimeMode for OffsetFloat {}

/// Time represented as wall clock time given as a positive real number representing seconds and sub-seconds since the start of the Unix Epoch.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct AbsoluteFloat {}

impl TimeRepresentation for AbsoluteFloat {
    type InnerTime = Duration;

    fn convert_from(&mut self, ts: Duration) -> Time {
        let current = SystemTime::UNIX_EPOCH + ts;
        let st_read = *START_TIME.read().unwrap();
        if let Some(st) = st_read {
            current.duration_since(st).expect("Time did not behave monotonically!")
        } else {
            *START_TIME.write().unwrap() = Some(current);
            Duration::ZERO
        }
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        let ts = START_TIME.read().unwrap().unwrap() + ts;
        ts.duration_since(SystemTime::UNIX_EPOCH)
            .expect("Time did not behave monotonically!")
    }

    fn to_string(&self, ts: Time) -> String {
        let dur = self.convert_into(ts);
        format! {"{}.{:09}", dur.as_secs(), dur.subsec_nanos()}
    }

    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }

    fn default_start_time() -> Option<SystemTime> {
        None
    }
}
impl OutputTimeRepresentation for AbsoluteFloat {}
impl TimeMode for AbsoluteFloat {}

/// Time represented as wall clock time in RFC3339 format.
#[cfg(not(feature = "serde"))]
#[derive(Debug, Copy, Clone, Default)]
pub struct AbsoluteRfc {}

#[cfg(not(feature = "serde"))]
impl TimeRepresentation for AbsoluteRfc {
    type InnerTime = Rfc3339Timestamp;

    fn convert_from(&mut self, rfc: Self::InnerTime) -> Time {
        let current = rfc.get_ref();
        let st_read = *START_TIME.read().unwrap();
        if let Some(st) = st_read {
            current.duration_since(st).expect("Time did not behave monotonically!")
        } else {
            *START_TIME.write().unwrap() = Some(*current);
            Duration::ZERO
        }
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        let ts = START_TIME.read().unwrap().unwrap() + ts;
        humantime::format_rfc3339(ts)
    }

    fn to_string(&self, ts: Self::InnerTime) -> String {
        ts.to_string()
    }

    fn parse(s: &'_ str) -> Result<Self::InnerTime, String> {
        let ts = humantime::parse_rfc3339(s).map_err(|e| e.to_string())?;
        Ok(humantime::format_rfc3339(ts))
    }

    fn default_start_time() -> Option<SystemTime> {
        None
    }
}
#[cfg(not(feature = "serde"))]
impl OutputTimeRepresentation for AbsoluteRfc {}
#[cfg(not(feature = "serde"))]
impl TimeMode for AbsoluteRfc {}

/// Time is set to be a fixed delay between input events.
/// The time given is ignored, and the fixed delay is applied.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Default)]
pub struct DelayTime {
    current: Duration,
    delay: Duration,
}

impl DelayTime {
    /// Creates a new DelayTime with a given delay.
    pub fn new(delay: Duration) -> Self {
        DelayTime {
            current: Default::default(),
            delay,
        }
    }
}

impl TimeRepresentation for DelayTime {
    type InnerTime = ();

    fn convert_from(&mut self, _inner: Self::InnerTime) -> Time {
        self.current += self.delay;
        self.current
    }

    fn convert_into(&self, _ts: Time) -> Self::InnerTime {}

    fn parse(_s: &str) -> Result<(), String> {
        Ok(())
    }

    fn to_string(&self, _ts: Self::InnerTime) -> String {
        format! {"{}.{:09}", self.current.as_secs(), self.current.subsec_nanos()}
    }
}

impl TimeMode for DelayTime {
    fn requires_timestamp() -> bool {
        false
    }
}

/// Time is set to be real-time. I.e. the input time is ignored and the current timestamp in rfc3339 format is taken instead.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct RealTime {
    last_ts: Time,
}
impl TimeRepresentation for RealTime {
    type InnerTime = ();

    fn convert_from(&mut self, _inner: Self::InnerTime) -> Time {
        let current = SystemTime::now();
        let st_read = *START_TIME.read().unwrap();
        self.last_ts = if let Some(st) = st_read {
            current.duration_since(st).expect("Time did not behave monotonically!")
        } else {
            *START_TIME.write().unwrap() = Some(current);
            Duration::ZERO
        };
        self.last_ts
    }

    fn convert_into(&self, _ts: Time) -> Self::InnerTime {}

    fn to_string(&self, _ts: Self::InnerTime) -> String {
        let ts = START_TIME.read().unwrap().unwrap() + self.last_ts;
        humantime::format_rfc3339(ts).to_string()
    }

    fn parse(_s: &str) -> Result<(), String> {
        Ok(())
    }
}

impl TimeMode for RealTime {
    fn requires_timestamp() -> bool {
        false
    }
}