Login
7 branches 0 tags
Ben (X13/Void) Removed :tree convenience function 15ebef1 3 years ago 428 Commits
nujel / stdlib / datetime / time.nuj
; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
; This project uses the MIT license, a copy should be included under /LICENSE

;; Time related λs

[defun time/seconds [timestamp]
       "Return the seconds part of TIMESTAMP, defaults to current time"
       [% [default timestamp [time]] 60]]

[defun time/minutes [timestamp]
       "Return the minutes part of TIMESTAMP, defaults to current time"
       [% [/ [default timestamp [time]] 60] 60]]

[defun time/hours [timestamp]
       "Return the hours part of TIMESTAMP, defaults to current time"
       [% [/ [default timestamp [time]] 3600] 24]]

[defun profile-form [raw]
       [def start-time [time/milliseconds]]
       [def val [eval raw]]
       [def end-time [time/milliseconds]]
       [display [cat "Evaluating " [ansi-yellow [str/write raw]] " to " [ansi-green [str/write val]] " took " [ansi-red [cat [- end-time start-time] "ms"] "\n"]]]]

[defmacro profile body
          "Measure and display how much time and ressources it takes for BODY to be evaluated"
          `[profile-form '~[if [last? body]
                               [car body]
                               [cons 'do body]]]]