Skip to content

Commit

Permalink
add built-in function time
Browse files Browse the repository at this point in the history
  • Loading branch information
binoyjayan committed Sep 17, 2023
1 parent c3e4ba9 commit 46f3928
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/common/builtins.rs
@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::common::object::*;
use lazy_static::lazy_static;
Expand All @@ -13,6 +14,7 @@ lazy_static! {
BuiltinFunction::new("rest".into(), builtin_rest),
BuiltinFunction::new("push".into(), builtin_push),
BuiltinFunction::new("str".into(), builtin_str),
BuiltinFunction::new("time".into(), builtin_time),
]
};
}
Expand Down Expand Up @@ -135,3 +137,15 @@ fn builtin_str(args: Vec<Rc<Object>>) -> Result<Rc<Object>, String> {
}
Ok(Rc::new(Object::Str(obj.to_string())))
}

fn builtin_time(args: Vec<Rc<Object>>) -> Result<Rc<Object>, String> {
if args.len() != 0 {
return Err(String::from("'time' takes no argument(s)"));
}
let current_time = SystemTime::now();
let duration = current_time
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let seconds = duration.as_secs();
Ok(Rc::new(Object::Number(seconds as f64)))
}

0 comments on commit 46f3928

Please sign in to comment.