about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-10-02T17·15+0300
committertazjin <tazjin@tvl.su>2022-10-04T08·03+0000
commit89dc26cece0595009c34a41a5574107833750f03 (patch)
tree02600ee0bfabe40ac1139e275cc6a124c85d61d6 /tvix/eval/src
parentf0179c92d3248667b7f037de91c6b9eb737a40d2 (diff)
feat(tvix/eval): implement `builtins.currentTime` r/5027
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 <grfn@gws.fyi>
Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/builtins/impure.rs14
1 files changed, 12 insertions, 2 deletions
diff --git a/tvix/eval/src/builtins/impure.rs b/tvix/eval/src/builtins/impure.rs
index 41e3805b2d0e..7073deaaa7ac 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<NixString, Value> {
         .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
 }