use std::{
collections::HashMap,
time::{SystemTime, UNIX_EPOCH},
};
use super::{EvalContext, Module, StaticValue, Type, Value};
#[derive(Debug)]
pub struct Time;
impl Module for Time {
fn get_name(&self) -> &'static str {
"time"
}
fn get_static_values(&self) -> HashMap<&'static str, StaticValue> {
[(
"now",
StaticValue::function(Self::now, vec![], Type::Integer),
)]
.into()
}
}
impl Time {
fn now(_: &mut EvalContext, _: Vec<Value>) -> Option<Value> {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.map(|d| d.as_secs())
.and_then(|v| i64::try_from(v).ok())
.map(Value::Integer)
}
}