#![allow(dead_code)]
use std::time::SystemTime;
#[derive(Copy, Clone)]
pub struct Timestamp {
data: u64,
}
impl Timestamp {
pub fn now() -> Result<Self, String> {
if let Ok(dur) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(Self {
data: dur.as_secs(),
})
} else {
Err(String::from(
"Error: Couldn't create a Timestamp, the current time is before the Unix epoch.",
))
}
}
pub fn get_current_timestamp(&self) -> u64 {
self.data
}
}
impl Default for Timestamp {
fn default() -> Self {
Self {
data: Default::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_now() {
const UNIX_EPOCH: u64 = 0;
let current_timestamp = Timestamp::now().unwrap();
assert_eq!(true, current_timestamp.data > UNIX_EPOCH);
}
#[test]
fn test_default() {
const UNIX_EPOCH: u64 = 0;
let current_timestamp = Timestamp::default();
assert_eq!(UNIX_EPOCH, current_timestamp.data);
}
}