common_time/
time.rs

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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::hash::{Hash, Hasher};

use chrono::{NaiveDateTime, NaiveTime, TimeZone as ChronoTimeZone, Utc};
use serde::{Deserialize, Serialize};

use crate::timestamp::TimeUnit;
use crate::timezone::{get_timezone, Timezone};

/// Time value, represents the elapsed time since midnight in the unit of `TimeUnit`.
#[derive(Debug, Clone, Default, Copy, Serialize, Deserialize)]
pub struct Time {
    value: i64,
    unit: TimeUnit,
}

impl Time {
    /// Creates the time by value and `TimeUnit`.
    pub fn new(value: i64, unit: TimeUnit) -> Self {
        Self { value, unit }
    }

    /// Creates the time in nanosecond.
    pub fn new_nanosecond(value: i64) -> Self {
        Self {
            value,
            unit: TimeUnit::Nanosecond,
        }
    }

    /// Creates the time in second.
    pub fn new_second(value: i64) -> Self {
        Self {
            value,
            unit: TimeUnit::Second,
        }
    }

    /// Creates the time in millisecond.
    pub fn new_millisecond(value: i64) -> Self {
        Self {
            value,
            unit: TimeUnit::Millisecond,
        }
    }

    /// Creates the time in microsecond.
    pub fn new_microsecond(value: i64) -> Self {
        Self {
            value,
            unit: TimeUnit::Microsecond,
        }
    }

    /// Returns the `TimeUnit` of the time.
    pub fn unit(&self) -> &TimeUnit {
        &self.unit
    }

    /// Returns the value of the time.
    pub fn value(&self) -> i64 {
        self.value
    }

    /// Convert a time to given time unit.
    /// Return `None` if conversion causes overflow.
    pub fn convert_to(&self, unit: TimeUnit) -> Option<Time> {
        if self.unit().factor() >= unit.factor() {
            let mul = self.unit().factor() / unit.factor();
            let value = self.value.checked_mul(mul as i64)?;
            Some(Time::new(value, unit))
        } else {
            let mul = unit.factor() / self.unit().factor();
            Some(Time::new(self.value.div_euclid(mul as i64), unit))
        }
    }

    /// Split a [Time] into seconds part and nanoseconds part.
    /// Notice the seconds part of split result is always rounded down to floor.
    fn split(&self) -> (i64, u32) {
        let sec_mul = (TimeUnit::Second.factor() / self.unit.factor()) as i64;
        let nsec_mul = (self.unit.factor() / TimeUnit::Nanosecond.factor()) as i64;

        let sec_div = self.value.div_euclid(sec_mul);
        let sec_mod = self.value.rem_euclid(sec_mul);
        // safety:  the max possible value of `sec_mod` is 999,999,999
        let nsec = u32::try_from(sec_mod * nsec_mul).unwrap();
        (sec_div, nsec)
    }

    /// Format Time to ISO8601 string. If the time exceeds what chrono time can
    /// represent, this function simply print the time unit and value in plain string.
    pub fn to_iso8601_string(&self) -> String {
        self.as_formatted_string("%H:%M:%S%.f%z", None)
    }

    /// Format Time for system timeszone.
    pub fn to_system_tz_string(&self) -> String {
        self.as_formatted_string("%H:%M:%S%.f", None)
    }

    /// Format Time for given timezone.
    /// When timezone is None, using system timezone by default.
    pub fn to_timezone_aware_string(&self, tz: Option<&Timezone>) -> String {
        self.as_formatted_string("%H:%M:%S%.f", tz)
    }

    fn as_formatted_string(self, pattern: &str, timezone: Option<&Timezone>) -> String {
        if let Some(time) = self.to_chrono_time() {
            let date = Utc::now().date_naive();
            let datetime = NaiveDateTime::new(date, time);
            match get_timezone(timezone) {
                Timezone::Offset(offset) => {
                    format!("{}", offset.from_utc_datetime(&datetime).format(pattern))
                }
                Timezone::Named(tz) => {
                    format!("{}", tz.from_utc_datetime(&datetime).format(pattern))
                }
            }
        } else {
            format!("[Time{}: {}]", self.unit, self.value)
        }
    }

    /// Cast the [Time] into chrono NaiveDateTime
    pub fn to_chrono_time(&self) -> Option<NaiveTime> {
        let (sec, nsec) = self.split();
        if let Ok(sec) = u32::try_from(sec) {
            NaiveTime::from_num_seconds_from_midnight_opt(sec, nsec)
        } else {
            None
        }
    }

    pub fn negative(mut self) -> Self {
        self.value = -self.value;
        self
    }
}

impl From<i64> for Time {
    fn from(v: i64) -> Self {
        Self {
            value: v,
            unit: TimeUnit::Millisecond,
        }
    }
}

impl From<Time> for i64 {
    fn from(t: Time) -> Self {
        t.value
    }
}

impl From<Time> for i32 {
    fn from(t: Time) -> Self {
        t.value as i32
    }
}

impl From<Time> for serde_json::Value {
    fn from(d: Time) -> Self {
        serde_json::Value::String(d.to_iso8601_string())
    }
}

impl PartialOrd for Time {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Time {
    fn cmp(&self, other: &Self) -> Ordering {
        // fast path: most comparisons use the same unit.
        if self.unit == other.unit {
            return self.value.cmp(&other.value);
        }

        let (s_sec, s_nsec) = self.split();
        let (o_sec, o_nsec) = other.split();
        match s_sec.cmp(&o_sec) {
            Ordering::Less => Ordering::Less,
            Ordering::Greater => Ordering::Greater,
            Ordering::Equal => s_nsec.cmp(&o_nsec),
        }
    }
}

impl PartialEq for Time {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl Eq for Time {}

impl Hash for Time {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let (sec, nsec) = self.split();
        state.write_i64(sec);
        state.write_u32(nsec);
    }
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::DefaultHasher;

    use serde_json::Value;

    use super::*;
    use crate::timezone::set_default_timezone;

    #[test]
    fn test_time() {
        let t = Time::new(1, TimeUnit::Millisecond);
        assert_eq!(TimeUnit::Millisecond, *t.unit());
        assert_eq!(1, t.value());
        assert_eq!(Time::new(1000, TimeUnit::Microsecond), t);
        assert!(t > Time::new(999, TimeUnit::Microsecond));
    }

    #[test]
    fn test_cmp_time() {
        let t1 = Time::new(0, TimeUnit::Millisecond);
        let t2 = Time::new(0, TimeUnit::Second);
        assert_eq!(t2, t1);

        let t1 = Time::new(100_100, TimeUnit::Millisecond);
        let t2 = Time::new(100, TimeUnit::Second);
        assert!(t1 > t2);

        let t1 = Time::new(10_010_001, TimeUnit::Millisecond);
        let t2 = Time::new(100, TimeUnit::Second);
        assert!(t1 > t2);

        let t1 = Time::new(10_010_001, TimeUnit::Nanosecond);
        let t2 = Time::new(100, TimeUnit::Second);
        assert!(t1 < t2);

        let t1 = Time::new(i64::MAX / 1000 * 1000, TimeUnit::Millisecond);
        let t2 = Time::new(i64::MAX / 1000, TimeUnit::Second);
        assert_eq!(t2, t1);

        let t1 = Time::new(i64::MAX, TimeUnit::Millisecond);
        let t2 = Time::new(i64::MAX / 1000 + 1, TimeUnit::Second);
        assert!(t2 > t1);

        let t1 = Time::new(i64::MAX, TimeUnit::Millisecond);
        let t2 = Time::new(i64::MAX / 1000, TimeUnit::Second);
        assert!(t2 < t1);

        let t1 = Time::new(10_010_001, TimeUnit::Millisecond);
        let t2 = Time::new(100, TimeUnit::Second);
        assert!(t1 > t2);

        let t1 = Time::new(-100 * 10_001, TimeUnit::Millisecond);
        let t2 = Time::new(-100, TimeUnit::Second);
        assert!(t2 > t1);
    }

    fn check_hash_eq(t1: Time, t2: Time) {
        let mut hasher = DefaultHasher::new();
        t1.hash(&mut hasher);
        let t1_hash = hasher.finish();

        let mut hasher = DefaultHasher::new();
        t2.hash(&mut hasher);
        let t2_hash = hasher.finish();
        assert_eq!(t2_hash, t1_hash);
    }

    #[test]
    fn test_hash() {
        check_hash_eq(
            Time::new(0, TimeUnit::Millisecond),
            Time::new(0, TimeUnit::Second),
        );
        check_hash_eq(
            Time::new(1000, TimeUnit::Millisecond),
            Time::new(1, TimeUnit::Second),
        );
        check_hash_eq(
            Time::new(1_000_000, TimeUnit::Microsecond),
            Time::new(1, TimeUnit::Second),
        );
        check_hash_eq(
            Time::new(1_000_000_000, TimeUnit::Nanosecond),
            Time::new(1, TimeUnit::Second),
        );
    }

    #[test]
    pub fn test_from_i64() {
        let t: Time = 42.into();
        assert_eq!(42, t.value());
        assert_eq!(TimeUnit::Millisecond, *t.unit());
    }

    #[test]
    fn test_to_iso8601_string() {
        set_default_timezone(Some("+10:00")).unwrap();
        let time_millis = 1000001;
        let ts = Time::new_millisecond(time_millis);
        assert_eq!("10:16:40.001+1000", ts.to_iso8601_string());

        let time_millis = 1000;
        let ts = Time::new_millisecond(time_millis);
        assert_eq!("10:00:01+1000", ts.to_iso8601_string());

        let time_millis = 1;
        let ts = Time::new_millisecond(time_millis);
        assert_eq!("10:00:00.001+1000", ts.to_iso8601_string());

        let time_seconds = 9 * 3600;
        let ts = Time::new_second(time_seconds);
        assert_eq!("19:00:00+1000", ts.to_iso8601_string());

        let time_seconds = 23 * 3600;
        let ts = Time::new_second(time_seconds);
        assert_eq!("09:00:00+1000", ts.to_iso8601_string());
    }

    #[test]
    fn test_serialize_to_json_value() {
        set_default_timezone(Some("+10:00")).unwrap();
        assert_eq!(
            "10:00:01+1000",
            match serde_json::Value::from(Time::new(1, TimeUnit::Second)) {
                Value::String(s) => s,
                _ => unreachable!(),
            }
        );

        assert_eq!(
            "10:00:00.001+1000",
            match serde_json::Value::from(Time::new(1, TimeUnit::Millisecond)) {
                Value::String(s) => s,
                _ => unreachable!(),
            }
        );

        assert_eq!(
            "10:00:00.000001+1000",
            match serde_json::Value::from(Time::new(1, TimeUnit::Microsecond)) {
                Value::String(s) => s,
                _ => unreachable!(),
            }
        );

        assert_eq!(
            "10:00:00.000000001+1000",
            match serde_json::Value::from(Time::new(1, TimeUnit::Nanosecond)) {
                Value::String(s) => s,
                _ => unreachable!(),
            }
        );
    }

    #[test]
    fn test_to_timezone_aware_string() {
        set_default_timezone(Some("+10:00")).unwrap();

        assert_eq!(
            "10:00:00.001",
            Time::new(1, TimeUnit::Millisecond).to_timezone_aware_string(None)
        );
        std::env::set_var("TZ", "Asia/Shanghai");
        assert_eq!(
            "08:00:00.001",
            Time::new(1, TimeUnit::Millisecond)
                .to_timezone_aware_string(Some(&Timezone::from_tz_string("SYSTEM").unwrap()))
        );
        assert_eq!(
            "08:00:00.001",
            Time::new(1, TimeUnit::Millisecond)
                .to_timezone_aware_string(Some(&Timezone::from_tz_string("+08:00").unwrap()))
        );
        assert_eq!(
            "07:00:00.001",
            Time::new(1, TimeUnit::Millisecond)
                .to_timezone_aware_string(Some(&Timezone::from_tz_string("+07:00").unwrap()))
        );
        assert_eq!(
            "23:00:00.001",
            Time::new(1, TimeUnit::Millisecond)
                .to_timezone_aware_string(Some(&Timezone::from_tz_string("-01:00").unwrap()))
        );
        assert_eq!(
            "08:00:00.001",
            Time::new(1, TimeUnit::Millisecond).to_timezone_aware_string(Some(
                &Timezone::from_tz_string("Asia/Shanghai").unwrap()
            ))
        );
        assert_eq!(
            "00:00:00.001",
            Time::new(1, TimeUnit::Millisecond)
                .to_timezone_aware_string(Some(&Timezone::from_tz_string("UTC").unwrap()))
        );
        assert_eq!(
            "03:00:00.001",
            Time::new(1, TimeUnit::Millisecond).to_timezone_aware_string(Some(
                &Timezone::from_tz_string("Europe/Moscow").unwrap()
            ))
        );
    }
}