rhai/src/packages/time_basic.rs

141 lines
3.6 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
#[cfg(feature = "no_float")]
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
2020-04-20 18:24:25 +02:00
use super::math_basic::MAX_INT;
2020-04-21 17:01:10 +02:00
use crate::def_package;
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;
#[cfg(feature = "no_float")]
2020-07-26 09:53:22 +02:00
use crate::parser::INT;
#[cfg(feature = "no_float")]
#[cfg(not(feature = "unchecked"))]
use crate::token::Position;
2020-08-15 06:57:47 +02:00
#[cfg(not(feature = "no_object"))]
use crate::engine::make_getter;
2020-06-17 10:50:57 +02:00
#[cfg(not(target_arch = "wasm32"))]
2020-04-21 17:01:10 +02:00
use crate::stdlib::time::Instant;
2020-06-17 10:50:57 +02:00
#[cfg(target_arch = "wasm32")]
use instant::Instant;
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-08-14 07:43:26 +02:00
set_exported_fn!(lib, "timestamp", create_timestamp);
set_exported_fn!(lib, "elapsed", elapsed);
#[cfg(not(feature = "no_object"))]
set_exported_fn!(lib, make_getter("elapsed"), elapsed);
set_exported_fn!(lib, "-", time_diff);
2020-08-14 18:04:10 +02:00
//lib.combine(exported_module!(time_compare));
2020-08-14 07:43:26 +02:00
2020-08-14 12:58:34 +02:00
lib.set_fn_2("<", |x:Instant, y:Instant| Ok(x < y));
lib.set_fn_2("<=", |x:Instant, y:Instant| Ok(x <= y));
lib.set_fn_2(">", |x:Instant, y:Instant| Ok(x > y));
lib.set_fn_2(">=", |x:Instant, y:Instant| Ok(x >= y));
lib.set_fn_2("==", |x:Instant, y:Instant| Ok(x == y));
lib.set_fn_2("!=", |x:Instant, y:Instant| Ok(x != y));
2020-08-14 07:43:26 +02:00
});
#[export_fn]
fn create_timestamp() -> Instant {
Instant::now()
}
#[cfg(not(feature = "no_float"))]
#[export_fn]
fn elapsed(timestamp: &mut Instant) -> FLOAT {
timestamp.elapsed().as_secs_f64() as FLOAT
}
#[cfg(feature = "no_float")]
#[export_fn(return_raw)]
fn elapsed(timestamp: &mut Instant) -> Result<Dynamic, Box<EvalAltResult>> {
let seconds = timestamp.elapsed().as_secs();
#[cfg(not(feature = "unchecked"))]
if seconds > (MAX_INT as u64) {
return EvalAltResult::ErrorArithmetic(
format!("Integer overflow for timestamp.elapsed: {}", seconds),
Position::none(),
)
.into();
}
Ok((seconds as INT).into())
}
#[cfg(not(feature = "no_float"))]
#[export_fn]
fn time_diff(ts1: Instant, ts2: Instant) -> FLOAT {
if ts2 > ts1 {
-(ts2 - ts1).as_secs_f64() as FLOAT
} else {
(ts1 - ts2).as_secs_f64() as FLOAT
}
2020-08-14 07:43:26 +02:00
}
#[cfg(feature = "no_float")]
#[export_fn(return_raw)]
fn time_diff(ts1: Instant, ts2: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
if ts2 > ts1 {
let seconds = (ts2 - ts1).as_secs();
#[cfg(not(feature = "unchecked"))]
if seconds > (MAX_INT as u64) {
return EvalAltResult::ErrorArithmetic(
format!("Integer overflow for timestamp duration: -{}", seconds),
Position::none(),
)
.into();
}
2020-04-21 17:01:10 +02:00
Ok(Dynamic::from(-(seconds as INT)))
2020-08-14 07:43:26 +02:00
} else {
let seconds = (ts1 - ts2).as_secs();
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "unchecked"))]
2020-07-04 16:53:00 +02:00
if seconds > (MAX_INT as u64) {
2020-08-06 04:17:32 +02:00
return EvalAltResult::ErrorArithmetic(
2020-08-14 07:43:26 +02:00
format!("Integer overflow for timestamp duration: {}", seconds),
2020-07-04 16:53:00 +02:00
Position::none(),
2020-08-14 07:43:26 +02:00
)
.into();
}
2020-07-04 16:53:00 +02:00
2020-08-14 07:43:26 +02:00
Ok((seconds as INT).into())
}
2020-08-14 07:43:26 +02:00
}
2020-08-14 07:43:26 +02:00
#[export_module]
mod time_compare {
pub fn eq(x: Instant, y: Instant) -> bool {
x == y
}
pub fn ne(x: Instant, y: Instant) -> bool {
x != y
}
pub fn lt(x: Instant, y: Instant) -> bool {
x < y
}
pub fn lte(x: Instant, y: Instant) -> bool {
x <= y
}
pub fn gt(x: Instant, y: Instant) -> bool {
x > y
}
pub fn gte(x: Instant, y: Instant) -> bool {
x >= y
}
}