From 89dc26cece0595009c34a41a5574107833750f03 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 2 Oct 2022 20:15:51 +0300 Subject: feat(tvix/eval): implement `builtins.currentTime` Returns time since epoch in seconds. This has a slight behaviour difference from Nix, in that we don't pin the time between REPL entries (Nix pins it for the program lifetime), but this is probably inconsequential as long as it is pinned during an evaluation. Change-Id: I010c02e93097a209d8ad69e278397c7e30e54c86 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6846 Tested-by: BuildkiteCI Reviewed-by: grfn Reviewed-by: wpcarro --- tvix/eval/src/builtins/impure.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'tvix/eval/src/builtins/impure.rs') diff --git a/tvix/eval/src/builtins/impure.rs b/tvix/eval/src/builtins/impure.rs index 41e3805b2d..7073deaaa7 100644 --- a/tvix/eval/src/builtins/impure.rs +++ b/tvix/eval/src/builtins/impure.rs @@ -3,8 +3,6 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -use smol_str::SmolStr; - use crate::{ value::{Builtin, NixString}, Value, @@ -22,5 +20,17 @@ pub(super) fn builtins() -> BTreeMap { .map(|b| (b.name().into(), Value::Builtin(b))) .collect(); + // currentTime pins the time at which evaluation was started + { + let seconds = match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(dur) => dur.as_secs() as i64, + + // This case is hit if the system time is *before* epoch. + Err(err) => -(err.duration().as_secs() as i64), + }; + + map.insert(NixString::from("currentTime"), Value::Integer(seconds)); + } + map } -- cgit 1.4.1