rhai/doc/src/language/timestamps.md

41 lines
1.5 KiB
Markdown
Raw Normal View History

2020-09-19 06:14:02 +02:00
`timestamp`
===========
2020-06-20 06:06:17 +02:00
{{#include ../links.md}}
2020-06-27 04:43:57 +02:00
Timestamps are provided by the [`BasicTimePackage`][packages] (excluded if using a [raw `Engine`])
2020-06-20 06:06:17 +02:00
via the `timestamp` function.
Timestamps are not available under [`no_std`].
2020-06-27 04:43:57 +02:00
The Rust type of a timestamp is `std::time::Instant` ([`instant::Instant`] in [WASM] builds).
2020-06-20 06:06:17 +02:00
[`type_of()`] a timestamp returns `"timestamp"`.
Built-in Functions
-----------------
2020-06-27 04:43:57 +02:00
The following methods (defined in the [`BasicTimePackage`][packages] but excluded if using a [raw `Engine`]) operate on timestamps:
2020-06-20 06:06:17 +02:00
2020-09-26 13:45:33 +02:00
| Function | Parameter(s) | Description |
| ----------------------------- | ------------------------------------------- | -------------------------------------------------------- |
| `elapsed` method and property | _none_ | returns the number of seconds since the timestamp |
| `-` operator | 1) later timestamp<br/>2) earlier timestamp | returns the number of seconds between the two timestamps |
| `+` operator | number of seconds to add | returns a new timestamp |
| `-` operator | number of seconds to subtract | returns a new timestamp |
2020-06-20 06:06:17 +02:00
Examples
--------
```rust
let now = timestamp();
// Do some lengthy operation...
if now.elapsed > 30.0 {
print("takes too long (over 30 seconds)!")
}
```