luby/crates/lb/src/time.rs
2025-06-30 17:22:12 +10:00

50 lines
1.2 KiB
Rust

//! Time library.
//!
//! The `lb:time` library provides utilities for working with the date and time and its related
//! constructs.
//!
//! ## Exports
//!
//! See [`lb_timelib`] for items exported by this library.
use luaffi::{cdef, metatype};
/// Items exported by the `lb:time` library.
///
/// This library can be acquired by calling
/// [`require("lb:time")`](https://www.lua.org/manual/5.1/manual.html#pdf-require).
///
/// ```lua
/// local time = require("lb:time");
/// ```
#[cdef(module = "lb:time")]
pub struct lb_timelib;
#[metatype]
impl lb_timelib {
#[new]
extern "Lua-C" fn new() -> Self {
Self
}
/// Returns an instant object that represents the current time at the time of calling.
pub extern "Lua-C" fn instant() -> lb_instant {
lb_instant::new(std::time::Instant::now())
}
}
/// Represents the measurement of a monotonically nondecreasing clock.
#[cdef]
pub struct lb_instant(#[opaque] std::time::Instant);
#[metatype]
impl lb_instant {
fn new(instant: std::time::Instant) -> Self {
Self(instant)
}
/// Returns the number of seconds elapsed since this instant was measured.
pub extern "Lua-C" fn elapsed_secs(&self) -> f64 {
self.0.elapsed().as_secs_f64()
}
}