rhai/src/packages/time_basic.rs

268 lines
9.9 KiB
Rust
Raw Normal View History

2022-01-25 10:55:53 +01:00
#![cfg(not(feature = "no_std"))]
2022-01-03 16:16:47 +01:00
use super::arithmetic::make_err as make_arithmetic_err;
2020-08-14 07:43:26 +02:00
use crate::plugin::*;
2021-12-25 16:49:14 +01:00
use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT};
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
2022-01-12 01:12:28 +01:00
#[cfg(not(target_family = "wasm"))]
2021-04-17 09:15:54 +02:00
use std::time::{Duration, Instant};
2022-01-12 01:12:28 +01:00
#[cfg(target_family = "wasm")]
2020-09-26 13:45:33 +02:00
use instant::{Duration, Instant};
2020-06-17 10:50:57 +02:00
2021-12-20 04:42:39 +01:00
def_package! {
/// Package of basic timing utilities.
2022-02-10 05:33:48 +01:00
pub BasicTimePackage(lib) {
2021-12-20 04:42:39 +01:00
lib.standard = true;
2021-12-20 04:42:39 +01:00
// Register date/time functions
combine_with_exported_module!(lib, "time", time_functions);
}
}
2020-08-14 07:43:26 +02:00
#[export_module]
mod time_functions {
/// Create a timestamp containing the current system time.
2022-07-31 07:23:15 +02:00
///
/// # Example
///
/// ```rhai
/// let now = timestamp();
///
/// sleep(10.0); // sleep for 10 seconds
///
/// print(now.elapsed); // prints 10.???
/// ```
pub fn timestamp() -> Instant {
Instant::now()
2020-08-14 07:43:26 +02:00
}
/// Return the number of seconds between the current system time and the timestamp.
2022-03-09 02:25:55 +01:00
///
/// # Example
///
/// ```rhai
/// let now = timestamp();
///
/// sleep(10.0); // sleep for 10 seconds
///
/// print(now.elapsed); // prints 10.???
/// ```
#[rhai_fn(name = "elapsed", get = "elapsed", return_raw)]
2021-12-25 16:49:14 +01:00
pub fn elapsed(timestamp: Instant) -> RhaiResult {
#[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
2022-01-03 16:16:47 +01:00
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2022-08-11 13:01:23 +02:00
"Integer overflow for timestamp.elapsed: {seconds}"
2020-08-31 05:46:32 +02:00
)))
} 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
/// Return the number of seconds between two timestamps.
#[rhai_fn(return_raw, name = "-")]
2021-12-25 16:49:14 +01:00
pub fn time_diff(timestamp1: Instant, timestamp2: Instant) -> RhaiResult {
#[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();
2022-01-03 16:16:47 +01:00
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2022-08-11 13:01:23 +02:00
"Integer overflow for timestamp duration: -{seconds}"
2020-08-31 05:46:32 +02:00
)))
} 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();
2022-01-03 16:16:47 +01:00
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2022-08-11 13:01:23 +02:00
"Integer overflow for timestamp duration: {seconds}"
2020-08-31 05:46:32 +02:00
)))
} else {
Ok((seconds as INT).into())
}
}
}
2020-09-26 13:45:33 +02:00
#[cfg(not(feature = "no_float"))]
pub mod float_functions {
2021-12-25 16:49:14 +01:00
fn add_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
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")) {
2022-01-03 16:16:47 +01:00
if seconds > (INT::MAX as FLOAT) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2022-08-11 13:01:23 +02:00
"Integer overflow for timestamp add: {seconds}"
2020-09-26 13:45:33 +02:00
)))
} 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!(
2022-08-11 13:01:23 +02:00
"Timestamp overflow when adding {seconds} second(s)"
2020-09-26 13:45:33 +02:00
))
})
}
} else {
Ok(timestamp + Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
}
}
2021-12-25 16:49:14 +01:00
fn subtract_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
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")) {
2022-01-03 16:16:47 +01:00
if seconds > (INT::MAX as FLOAT) {
2020-09-26 13:45:33 +02:00
Err(make_arithmetic_err(format!(
2022-08-11 13:01:23 +02:00
"Integer overflow for timestamp add: {seconds}"
2020-09-26 13:45:33 +02:00
)))
} 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!(
2022-08-11 13:01:23 +02:00
"Timestamp overflow when adding {seconds} second(s)"
2020-09-26 13:45:33 +02:00
))
})
}
} else {
Ok(timestamp - Duration::from_millis((seconds * 1000.0) as u64))
2020-09-26 13:45:33 +02:00
}
}
/// Add the specified number of `seconds` to the timestamp and return it as a new timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+")]
2021-12-25 16:49:14 +01:00
pub fn add(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
2021-03-22 04:18:09 +01:00
add_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
/// Add the specified number of `seconds` to the timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+=")]
2021-12-25 16:49:14 +01:00
pub fn add_assign(timestamp: &mut Instant, seconds: FLOAT) -> RhaiResultOf<()> {
*timestamp = add_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
/// Subtract the specified number of `seconds` from the timestamp and return it as a new timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "-")]
2021-12-25 16:49:14 +01:00
pub fn subtract(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
2021-03-22 04:18:09 +01:00
subtract_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
/// Subtract the specified number of `seconds` from the timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "-=")]
2021-12-25 16:49:14 +01:00
pub fn subtract_assign(timestamp: &mut Instant, seconds: FLOAT) -> RhaiResultOf<()> {
*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
2021-12-25 16:49:14 +01:00
fn add_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
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!(
2022-08-11 13:01:23 +02:00
"Timestamp overflow when adding {seconds} second(s)"
2020-09-26 13:45:33 +02:00
))
})
} else {
Ok(timestamp + Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
}
}
2021-12-25 16:49:14 +01:00
fn subtract_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
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!(
2022-08-11 13:01:23 +02:00
"Timestamp overflow when adding {seconds} second(s)"
2020-09-26 13:45:33 +02:00
))
})
} else {
Ok(timestamp - Duration::from_secs(seconds as u64))
2020-09-26 13:45:33 +02:00
}
}
/// Add the specified number of `seconds` to the timestamp and return it as a new timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+")]
2021-12-25 16:49:14 +01:00
pub fn add(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
2021-03-22 04:18:09 +01:00
add_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
/// Add the specified number of `seconds` to the timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "+=")]
2021-12-25 16:49:14 +01:00
pub fn add_assign(timestamp: &mut Instant, seconds: INT) -> RhaiResultOf<()> {
*timestamp = add_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
/// Subtract the specified number of `seconds` from the timestamp and return it as a new timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "-")]
2021-12-25 16:49:14 +01:00
pub fn subtract(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
2021-03-22 04:18:09 +01:00
subtract_impl(timestamp, seconds)
2020-09-27 16:15:35 +02:00
}
/// Subtract the specified number of `seconds` from the timestamp.
2020-09-27 16:15:35 +02:00
#[rhai_fn(return_raw, name = "-=")]
2021-12-25 16:49:14 +01:00
pub fn subtract_assign(timestamp: &mut Instant, seconds: INT) -> RhaiResultOf<()> {
*timestamp = subtract_impl(*timestamp, seconds)?;
2021-03-22 04:18:09 +01:00
Ok(())
2020-09-27 16:15:35 +02:00
}
/// Return `true` if two timestamps are equal.
#[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
}
/// Return `true` if two timestamps are not equal.
#[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
}
/// Return `true` if the first timestamp is earlier than the second.
#[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
}
/// Return `true` if the first timestamp is earlier than or equals to the second.
#[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
}
/// Return `true` if the first timestamp is later than the second.
#[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
}
/// Return `true` if the first timestamp is later than or equals to the second.
#[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
}
}