artichoke_backend/extn/core/time/
mod.rs

1//! Time is an abstraction of dates and times.
2//!
3//! This module implements the [`Time`] class from Ruby Core.
4//!
5//! In Artichoke, Time is represented as a 64-bit signed integer of seconds
6//! since January 1, 1970 UTC and an unsigned 32-bit integer of subsecond
7//! nanoseconds. This allows representing roughly 584 billion years.
8//!
9//! You can use this class in your application by accessing it directly. As a
10//! Core class, it is globally available:
11//!
12//! ```ruby
13//! Time.now
14//! ```
15//!
16//! This implementation of `Time` supports the system clock via the
17//! [`spinoso-time`] crate.
18//!
19//! [`Time`]: https://ruby-doc.org/core-3.1.2/Time.html
20
21use crate::convert::HeapAllocatedData;
22use crate::extn::prelude::*;
23
24pub mod args;
25pub(in crate::extn) mod mruby;
26pub mod offset;
27pub mod subsec;
28pub(super) mod trampoline;
29
30#[doc(inline)]
31pub use spinoso_time::tzrs::*;
32
33impl HeapAllocatedData for Time {
34    const RUBY_TYPE: &'static str = "Time";
35}
36
37impl From<TimeError> for Error {
38    fn from(error: TimeError) -> Error {
39        ArgumentError::from(format!("{error}")).into()
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::test::prelude::*;
46
47    const SUBJECT: &str = "Time";
48    const FUNCTIONAL_TEST: &[u8] = include_bytes!("time_functional_test.rb");
49
50    #[test]
51    fn functional() {
52        let mut interp = interpreter();
53        let result = interp.eval(FUNCTIONAL_TEST);
54        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
55        let result = interp.eval(b"spec");
56        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
57    }
58}