There are many reasons why we may want Nyquist to know the current date or time. We may want to add the date to an exported file name, or we may want to measure how long it takes for a plug-in to run. In Nyquist 3.16 we can do this with (get-real-time).
For timing a long running Nyquist script, we can simply store the time when we start, then subtract that from the time at the end. As an example, we can count the number of samples have a value greater than 0 dB (a slow running process) and time how long it takes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(defun count-clipped (sig &aux (count 0)) (do ((val (snd-fetch sig) (snd-fetch sig))) ((not val) count) (when (> (abs val) 1) (incf count)))) (setf start (get-real-time)) (setf num (multichan-expand #'count-clipped *track*)) (setf seconds (- (get-real-time) start)) (format nil "Found ~a samples > 0 dB in ~a seconds." num seconds) ; Example output: ; "Found 51 samples > 0 dB in 0.425084 seconds." |
But what if we want the actual time now?
Given that “Epoch Time” began at 00:00:00 UTC, we can calculate the real UTC time now as the remainder after dividing Epoch Time by one day in seconds.
1 2 3 4 5 6 7 8 9 10 |
(setf day (* 60 60 24)) ;one day in seconds (setf epoch-time (get-real-time)) ;; Nyquist "remainder" function requires integers (setf epoch-int (truncate epoch-time)) (setf fraction (- epoch-time epoch-int)) (setf now (+ (rem epoch-int day) fraction)) |
Nearly there, but we still only have the time in seconds. Fortunately, we can easily convert the time in seconds to hours, minutes and seconds, using a handy function: SS-TO-HHMMSS.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
(setf local 1) ;local timezone = UTC +1 hours. (let ((day (* 60 60 24)) (epoch-time (get-real-time))) ;adjust for local time zone (setf local-time (+ epoch-time (* local 60 60))) (setf time-int (round local-time)) ;; Time now = remainder after dividing by 1 day in seconds (setf now (rem time-int day)) ;; Finally, we can print the local time now, ;; using the SS-TO-HHMMSS function: (format nil "The time now is: ~a" (ss-to-hhmmss now :places 0))) |
Example output:
1 2 |
The time now is: 17:31:02 |