2020-04-20 18:11:25 +02:00
|
|
|
use super::logic::{eq, gt, gte, lt, lte, ne};
|
2020-04-20 18:24:25 +02:00
|
|
|
use super::math_basic::MAX_INT;
|
2020-04-21 17:01:10 +02:00
|
|
|
use super::{reg_binary, reg_none, reg_unary};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
use crate::def_package;
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::fn_register::{map_dynamic as map, map_result as result};
|
|
|
|
use crate::parser::INT;
|
2020-04-20 18:24:25 +02:00
|
|
|
use crate::result::EvalAltResult;
|
|
|
|
use crate::token::Position;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
use crate::stdlib::time::Instant;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
def_package!(BasicTimePackage:"Basic timing utilities.", lib, {
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
|
|
|
{
|
|
|
|
// Register date/time functions
|
|
|
|
reg_none(lib, "timestamp", || Instant::now(), map);
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
reg_binary(
|
|
|
|
lib,
|
|
|
|
"-",
|
|
|
|
|ts1: Instant, ts2: Instant| {
|
|
|
|
if ts2 > ts1 {
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
return Ok(-(ts2 - ts1).as_secs_f64());
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
{
|
|
|
|
let seconds = (ts2 - ts1).as_secs();
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2020-04-20 18:11:25 +02:00
|
|
|
{
|
2020-04-21 17:01:10 +02:00
|
|
|
if seconds > (MAX_INT as u64) {
|
2020-04-21 17:25:12 +02:00
|
|
|
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
2020-04-21 17:01:10 +02:00
|
|
|
format!(
|
|
|
|
"Integer overflow for timestamp duration: {}",
|
|
|
|
-(seconds as i64)
|
|
|
|
),
|
|
|
|
Position::none(),
|
2020-04-21 17:25:12 +02:00
|
|
|
)));
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
return Ok(-(seconds as INT));
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
return Ok((ts1 - ts2).as_secs_f64());
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(feature = "no_float")]
|
2020-04-20 18:11:25 +02:00
|
|
|
{
|
2020-04-21 17:01:10 +02:00
|
|
|
let seconds = (ts1 - ts2).as_secs();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
{
|
|
|
|
if seconds > (MAX_INT as u64) {
|
2020-04-21 17:25:12 +02:00
|
|
|
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
2020-04-21 17:01:10 +02:00
|
|
|
format!("Integer overflow for timestamp duration: {}", seconds),
|
|
|
|
Position::none(),
|
2020-04-21 17:25:12 +02:00
|
|
|
)));
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
return Ok(seconds as INT);
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
result,
|
|
|
|
);
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
|
|
|
|
reg_binary(lib, "<", lt::<Instant>, map);
|
|
|
|
reg_binary(lib, "<=", lte::<Instant>, map);
|
|
|
|
reg_binary(lib, ">", gt::<Instant>, map);
|
|
|
|
reg_binary(lib, ">=", gte::<Instant>, map);
|
|
|
|
reg_binary(lib, "==", eq::<Instant>, map);
|
|
|
|
reg_binary(lib, "!=", ne::<Instant>, map);
|
|
|
|
|
|
|
|
reg_unary(
|
|
|
|
lib,
|
|
|
|
"elapsed",
|
|
|
|
|timestamp: Instant| {
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
return Ok(timestamp.elapsed().as_secs_f64());
|
|
|
|
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
{
|
|
|
|
let seconds = timestamp.elapsed().as_secs();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
{
|
|
|
|
if seconds > (MAX_INT as u64) {
|
|
|
|
return Err(EvalAltResult::ErrorArithmetic(
|
|
|
|
format!("Integer overflow for timestamp.elapsed(): {}", seconds),
|
|
|
|
Position::none(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(seconds as INT);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
result,
|
|
|
|
);
|
|
|
|
});
|