rhai/src/packages/time_basic.rs

238 lines
8.0 KiB
Rust
Raw Normal View History

2020-06-17 10:50:57 +02:00
#![cfg(not(feature = "no_std"))]
2020-07-26 04:03:59 +02:00
2020-09-26 13:45:33 +02:00
use super::{arithmetic::make_err as make_arithmetic_err, math_basic::MAX_INT};
2020-09-26 13:45:33 +02:00
use crate::any::Dynamic;
2020-04-21 17:01:10 +02:00
use crate::def_package;
2020-09-26 13:45:33 +02:00
use crate::parser::INT;
2020-08-14 07:43:26 +02:00
use crate::plugin::*;
2020-04-20 18:24:25 +02:00
use crate::result::EvalAltResult;
2020-07-26 04:03:59 +02:00
#[cfg(not(feature = "no_float"))]
use crate::parser::FLOAT;
2020-09-26 13:45:33 +02:00
use crate::stdlib::boxed::Box;
2020-07-26 09:53:22 +02:00
2020-06-17 10:50:57 +02:00
#[cfg(not(target_arch = "wasm32"))]
2020-09-26 13:45:33 +02:00
use crate::stdlib::time::{Duration, Instant};
2020-06-17 10:50:57 +02:00
#[cfg(target_arch = "wasm32")]
2020-09-26 13:45:33 +02:00
use instant::{Duration, Instant};
2020-06-17 10:50:57 +02:00
2020-04-22 08:55:40 +02:00
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
2020-04-24 06:39:24 +02:00
// Register date/time functions
2020-09-13 16:12:11 +02:00
combine_with_exported_module!(lib, "time", time_functions);
2020-08-14 07:43:26 +02:00
});
#[export_module]
mod time_functions {
pub fn timestamp() -> Instant {
Instant::now()
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = "elapsed", get = "elapsed", return_raw)]
pub fn elapsed(timestamp: &mut Instant) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]
{
Ok((timestamp.elapsed().as_secs_f64() as FLOAT).into())
}
2020-08-14 07:43:26 +02:00
#[cfg(feature = "no_float")]
{
let seconds = timestamp.elapsed().as_secs();
2020-08-14 07:43:26 +02:00
2020-08-31 05:46:32 +02:00
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2020-08-31 05:46:32 +02:00
"Integer overflow for timestamp.elapsed: {}",
seconds
)))
} else {
Ok((seconds as INT).into())
}
2020-08-14 07:43:26 +02:00
}
}
2020-04-21 17:01:10 +02:00
#[rhai_fn(return_raw, name = "-")]
2020-08-24 04:38:15 +02:00
pub fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]
{
Ok(if ts2 > ts1 {
-(ts2 - ts1).as_secs_f64() as FLOAT
} else {
(ts1 - ts2).as_secs_f64() as FLOAT
}
.into())
}
2020-07-04 16:53:00 +02:00
#[cfg(feature = "no_float")]
if ts2 > ts1 {
let seconds = (ts2 - ts1).as_secs();
2020-08-31 05:46:32 +02:00
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2020-08-31 05:46:32 +02:00
"Integer overflow for timestamp duration: -{}",
seconds
)))
} else {
2020-09-26 13:45:33 +02:00
Ok((-(seconds as INT)).into())
}
} else {
let seconds = (ts1 - ts2).as_secs();
2020-08-31 05:46:32 +02:00
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2020-08-31 05:46:32 +02:00
"Integer overflow for timestamp duration: {}",
seconds
)))
} else {
Ok((seconds as INT).into())
}
}
}
2020-09-26 13:45:33 +02:00
#[cfg(not(feature = "no_float"))]
pub mod float_functions {
2020-09-27 16:15:35 +02:00
fn add_impl(x: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0.0 {
2020-09-27 16:15:35 +02:00
subtract_impl(x, -seconds)
} else if cfg!(not(feature = "unchecked")) {
2020-09-26 13:45:33 +02:00
if seconds > (MAX_INT as FLOAT) {
Err(make_arithmetic_err(format!(
"Integer overflow for timestamp add: {}",
seconds
)))
} else {
x.checked_add(Duration::from_millis((seconds * 1000.0) as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
}
} else {
2020-09-27 16:15:35 +02:00
Ok(x + Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
}
}
2020-09-27 16:15:35 +02:00
fn subtract_impl(x: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0.0 {
2020-09-27 16:15:35 +02:00
add_impl(x, -seconds)
} else if cfg!(not(feature = "unchecked")) {
2020-09-26 13:45:33 +02:00
if seconds > (MAX_INT as FLOAT) {
Err(make_arithmetic_err(format!(
"Integer overflow for timestamp add: {}",
seconds
)))
} else {
x.checked_sub(Duration::from_millis((seconds * 1000.0) as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
}
} else {
2020-09-27 16:15:35 +02:00
Ok(x - Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
}
}
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+")]
pub fn add(x: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
add_impl(x, seconds).map(Into::<Dynamic>::into)
}
#[rhai_fn(return_raw, name = "+=")]
pub fn add_assign(x: &mut Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
*x = add_impl(*x, seconds)?;
Ok(().into())
}
#[rhai_fn(return_raw, name = "-")]
pub fn subtract(x: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
subtract_impl(x, seconds).map(Into::<Dynamic>::into)
}
#[rhai_fn(return_raw, name = "-=")]
pub fn subtract_assign(
x: &mut Instant,
seconds: FLOAT,
) -> Result<Dynamic, Box<EvalAltResult>> {
*x = subtract_impl(*x, seconds)?;
Ok(().into())
2020-09-26 13:45:33 +02:00
}
2020-09-27 16:15:35 +02:00
}
2020-09-26 13:45:33 +02:00
2020-09-27 16:15:35 +02:00
fn add_impl(x: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
if seconds < 0 {
subtract_impl(x, -seconds)
} else if cfg!(not(feature = "unchecked")) {
2020-09-26 13:45:33 +02:00
x.checked_add(Duration::from_secs(seconds as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
} else {
2020-09-27 16:15:35 +02:00
Ok(x + Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
}
}
2020-09-27 16:15:35 +02:00
fn subtract_impl(x: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0 {
2020-09-27 16:15:35 +02:00
add_impl(x, -seconds)
} else if cfg!(not(feature = "unchecked")) {
2020-09-26 13:45:33 +02:00
x.checked_sub(Duration::from_secs(seconds as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
} else {
2020-09-27 16:15:35 +02:00
Ok(x - Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
}
}
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+")]
pub fn add(x: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
add_impl(x, seconds).map(Into::<Dynamic>::into)
}
#[rhai_fn(return_raw, name = "+=")]
pub fn add_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
*x = add_impl(*x, seconds)?;
Ok(().into())
}
#[rhai_fn(return_raw, name = "-")]
pub fn subtract(x: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
subtract_impl(x, seconds).map(Into::<Dynamic>::into)
}
#[rhai_fn(return_raw, name = "-=")]
pub fn subtract_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
*x = subtract_impl(*x, seconds)?;
Ok(().into())
}
#[rhai_fn(name = "==")]
2020-08-14 07:43:26 +02:00
pub fn eq(x: Instant, y: Instant) -> bool {
x == y
}
#[rhai_fn(name = "!=")]
2020-08-14 07:43:26 +02:00
pub fn ne(x: Instant, y: Instant) -> bool {
x != y
}
#[rhai_fn(name = "<")]
2020-08-14 07:43:26 +02:00
pub fn lt(x: Instant, y: Instant) -> bool {
x < y
}
#[rhai_fn(name = "<=")]
2020-08-14 07:43:26 +02:00
pub fn lte(x: Instant, y: Instant) -> bool {
x <= y
}
#[rhai_fn(name = ">")]
2020-08-14 07:43:26 +02:00
pub fn gt(x: Instant, y: Instant) -> bool {
x > y
}
#[rhai_fn(name = ">=")]
2020-08-14 07:43:26 +02:00
pub fn gte(x: Instant, y: Instant) -> bool {
x >= y
}
}