anki/rslib/src/timestamp.rs

51 lines
1.1 KiB
Rust
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::time;
#[repr(transparent)]
2020-03-26 04:06:02 +01:00
#[derive(Debug, Clone, Copy)]
pub struct TimestampSecs(pub i64);
impl TimestampSecs {
pub fn now() -> Self {
Self(elapsed().as_secs() as i64)
}
}
#[repr(transparent)]
2020-03-26 04:06:02 +01:00
#[derive(Debug, Clone, Copy)]
pub struct TimestampMillis(pub i64);
impl TimestampMillis {
pub fn now() -> Self {
Self(elapsed().as_millis() as i64)
}
2020-03-22 04:17:00 +01:00
}
#[cfg(not(test))]
fn elapsed() -> time::Duration {
time::SystemTime::now()
.duration_since(time::SystemTime::UNIX_EPOCH)
.unwrap()
2020-03-22 04:17:00 +01:00
}
// when running in CI, shift the current time away from the cutoff point
// to accomodate unit tests that depend on the current time
#[cfg(test)]
fn elapsed() -> time::Duration {
use chrono::{Local, Timelike};
let now = Local::now();
let mut elap = time::SystemTime::now()
.duration_since(time::SystemTime::UNIX_EPOCH)
.unwrap();
if now.hour() >= 2 && now.hour() < 4 {
elap -= time::Duration::from_secs(60 * 60 * 2);
}
elap
}