rhai/src/packages/time_basic.rs

254 lines
8.8 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-08-14 07:43:26 +02:00
use crate::plugin::*;
2020-11-16 16:10:14 +01:00
use crate::{def_package, Dynamic, EvalAltResult, INT};
2021-04-17 09:15:54 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2020-07-26 04:03:59 +02:00
#[cfg(not(feature = "no_float"))]
2020-10-29 04:37:51 +01:00
use crate::FLOAT;
2020-07-26 04:03:59 +02:00
2021-02-19 08:50:48 +01:00
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
2021-04-17 09:15:54 +02:00
use std::time::{Duration, Instant};
2021-02-19 08:50:48 +01:00
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
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, {
lib.standard = true;
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: Instant) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]
if timestamp > Instant::now() {
2021-02-19 08:50:48 +01:00
Err(make_arithmetic_err("Time-stamp is later than now"))
} else {
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 if timestamp > Instant::now() {
Err(make_arithmetic_err("Time-stamp is later than now"))
2020-08-31 05:46:32 +02:00
} 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 = "-")]
pub fn time_diff(
2021-08-13 07:42:39 +02:00
timestamp1: Instant,
timestamp2: Instant,
) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]
2021-08-13 07:42:39 +02:00
return Ok(if timestamp2 > timestamp1 {
-(timestamp2 - timestamp1).as_secs_f64() as FLOAT
2021-02-19 08:50:48 +01:00
} else {
2021-08-13 07:42:39 +02:00
(timestamp1 - timestamp2).as_secs_f64() as FLOAT
}
2021-02-19 08:50:48 +01:00
.into());
2020-07-04 16:53:00 +02:00
#[cfg(feature = "no_float")]
2021-08-13 08:04:27 +02:00
if timestamp2 > timestamp1 {
let seconds = (timestamp2 - timestamp1).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 {
2021-08-13 08:04:27 +02:00
let seconds = (timestamp1 - timestamp2).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 {
fn add_impl(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0.0 {
subtract_impl(timestamp, -seconds)
2020-09-27 16:15:35 +02:00
} 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 {
timestamp
.checked_add(Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
}
} else {
Ok(timestamp + Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
}
}
fn subtract_impl(
timestamp: Instant,
seconds: FLOAT,
) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0.0 {
add_impl(timestamp, -seconds)
2020-09-27 16:15:35 +02:00
} 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 {
timestamp
.checked_sub(Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
}
} else {
Ok(timestamp - 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 = "+")]
2021-03-22 04:18:09 +01:00
pub fn add(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
add_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "+=")]
pub fn add_assign(
timestamp: &mut Instant,
seconds: FLOAT,
2021-03-22 04:18:09 +01:00
) -> Result<(), Box<EvalAltResult>> {
*timestamp = add_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "-")]
2021-03-22 04:18:09 +01:00
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> Result<Instant, Box<EvalAltResult>> {
subtract_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "-=")]
pub fn subtract_assign(
timestamp: &mut Instant,
2020-09-27 16:15:35 +02:00
seconds: FLOAT,
2021-03-22 04:18:09 +01:00
) -> Result<(), Box<EvalAltResult>> {
*timestamp = subtract_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-26 13:45:33 +02:00
}
2020-09-27 16:15:35 +02:00
}
2020-09-26 13:45:33 +02:00
fn add_impl(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-27 16:15:35 +02:00
if seconds < 0 {
subtract_impl(timestamp, -seconds)
2020-09-27 16:15:35 +02:00
} else if cfg!(not(feature = "unchecked")) {
timestamp
.checked_add(Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
} else {
Ok(timestamp + Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
}
}
fn subtract_impl(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
2020-09-26 13:45:33 +02:00
if seconds < 0 {
add_impl(timestamp, -seconds)
2020-09-27 16:15:35 +02:00
} else if cfg!(not(feature = "unchecked")) {
timestamp
.checked_sub(Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {} second(s)",
seconds
))
})
} else {
Ok(timestamp - 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 = "+")]
2021-03-22 04:18:09 +01:00
pub fn add(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
add_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "+=")]
2021-03-22 04:18:09 +01:00
pub fn add_assign(timestamp: &mut Instant, seconds: INT) -> Result<(), Box<EvalAltResult>> {
*timestamp = add_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "-")]
2021-03-22 04:18:09 +01:00
pub fn subtract(timestamp: Instant, seconds: INT) -> Result<Instant, Box<EvalAltResult>> {
subtract_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(return_raw, name = "-=")]
pub fn subtract_assign(
timestamp: &mut Instant,
seconds: INT,
2021-03-22 04:18:09 +01:00
) -> Result<(), Box<EvalAltResult>> {
*timestamp = subtract_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
#[rhai_fn(name = "==")]
2021-08-13 07:42:39 +02:00
pub fn eq(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 == timestamp2
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = "!=")]
2021-08-13 07:42:39 +02:00
pub fn ne(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 != timestamp2
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = "<")]
2021-08-13 07:42:39 +02:00
pub fn lt(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 < timestamp2
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = "<=")]
2021-08-13 07:42:39 +02:00
pub fn lte(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 <= timestamp2
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = ">")]
2021-08-13 07:42:39 +02:00
pub fn gt(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 > timestamp2
2020-08-14 07:43:26 +02:00
}
#[rhai_fn(name = ">=")]
2021-08-13 07:42:39 +02:00
pub fn gte(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 >= timestamp2
2020-08-14 07:43:26 +02:00
}
}