rune/
timefns.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! Time analysis
use crate::core::{
    env::{sym, Env},
    gc::{Context, Rt},
    object::Object,
};
use rune_core::macros::list;
use rune_macros::defun;
use std::time::SystemTime;

defvar!(CURRENT_TIME_LIST, true);

#[defun]
fn current_time<'ob>(cx: &'ob Context, env: &Rt<Env>) -> Object<'ob> {
    assert!(
        env.vars.get(sym::CURRENT_TIME_LIST).unwrap() == &sym::TRUE,
        "current-time-list is nil"
    );
    let duration = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .expect("System time is before the epoch");

    let secs = duration.as_secs();
    let micros = duration.subsec_micros();
    let low = secs & 0xffff;
    let high = secs >> 16;

    list![high, low, micros, 0; cx]
}