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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// 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 in the LICENSE file at the
// root of this repository, or online 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.

//! Now utilities.

use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use std::time::SystemTime;

#[cfg(feature = "chrono")]
use chrono::{DateTime, TimeZone, Utc};
use once_cell::sync::Lazy;

/// A type representing the number of milliseconds since the Unix epoch.
pub type EpochMillis = u64;

/// Converts epoch milliseconds to a DateTime.
#[cfg(feature = "chrono")]
// TODO(benesch): rewrite to avoid dangerous use of `as`.
#[allow(clippy::as_conversions)]
pub fn to_datetime(millis: EpochMillis) -> DateTime<Utc> {
    let dur = std::time::Duration::from_millis(millis);
    match Utc
        .timestamp_opt(dur.as_secs() as i64, dur.subsec_nanos())
        .single()
    {
        Some(single) => single,
        None => {
            panic!("Ambiguous timestamp: {millis} millis")
        }
    }
}

/// A function that returns system or mocked time.
// This is a newtype so that it can implement `Debug`, as closures don't
// implement `Debug` by default. It derefs to a callable so that it is
// ergonomically equivalent to a closure.
#[derive(Clone)]
pub struct NowFn<T = EpochMillis>(Arc<dyn Fn() -> T + Send + Sync>);

impl NowFn<EpochMillis> {
    /// Returns now in seconds.
    pub fn as_secs(&self) -> i64 {
        let millis: u64 = (self)();
        // Justification for `unwrap`:
        // Any u64, when divided by 1000, is a valid i64.
        i64::try_from(millis / 1_000).unwrap()
    }
}

impl<T> fmt::Debug for NowFn<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("<now_fn>")
    }
}

impl<T> Deref for NowFn<T> {
    type Target = dyn Fn() -> T + Send + Sync;

    fn deref(&self) -> &Self::Target {
        &(*self.0)
    }
}

impl<F, T> From<F> for NowFn<T>
where
    F: Fn() -> T + Send + Sync + 'static,
{
    fn from(f: F) -> NowFn<T> {
        NowFn(Arc::new(f))
    }
}

fn system_time() -> EpochMillis {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .expect("failed to get millis since epoch")
        .as_millis()
        .try_into()
        .expect("current time did not fit into u64")
}
fn now_zero() -> EpochMillis {
    0
}

/// A [`NowFn`] that returns the actual system time.
pub static SYSTEM_TIME: Lazy<NowFn> = Lazy::new(|| NowFn::from(system_time));

/// A [`NowFn`] that always returns zero.
///
/// For use in tests.
pub static NOW_ZERO: Lazy<NowFn> = Lazy::new(|| NowFn::from(now_zero));

#[cfg(feature = "chrono")]
#[cfg(test)]
mod tests {
    use chrono::NaiveDate;

    use super::to_datetime;

    #[crate::test]
    fn test_to_datetime() {
        let test_cases = [
            (
                0,
                NaiveDate::from_ymd_opt(1970, 1, 1)
                    .unwrap()
                    .and_hms_nano_opt(0, 0, 0, 0)
                    .unwrap(),
            ),
            (
                1600000000000,
                NaiveDate::from_ymd_opt(2020, 9, 13)
                    .unwrap()
                    .and_hms_nano_opt(12, 26, 40, 0)
                    .unwrap(),
            ),
            (
                1658323270293,
                NaiveDate::from_ymd_opt(2022, 7, 20)
                    .unwrap()
                    .and_hms_nano_opt(13, 21, 10, 293_000_000)
                    .unwrap(),
            ),
        ];
        // to_datetime works properly and roundtrips
        for (millis, datetime) in test_cases.into_iter() {
            let converted_datetime = to_datetime(millis).naive_utc();
            assert_eq!(datetime, converted_datetime);
            assert_eq!(
                millis,
                u64::try_from(converted_datetime.and_utc().timestamp_millis()).unwrap()
            )
        }
    }
}