Title here
Summary here
(defpackage :time-example
(:use :cl :local-time))
(in-package :time-example)
(defun main ()
(let ((p #'print))
;; We'll start by getting the current time.
(let ((now (now)))
(funcall p now)
;; You can build a timestamp by providing the
;; year, month, day, etc. Times are always associated
;; with a timezone.
(let ((then (encode-timestamp 651387237 58 34 20 17 11 2009 :timezone +utc-zone+)))
(funcall p then)
;; You can extract the various components of the time
;; value as expected.
(funcall p (timestamp-year then))
(funcall p (timestamp-month then))
(funcall p (timestamp-day then))
(funcall p (timestamp-hour then))
(funcall p (timestamp-minute then))
(funcall p (timestamp-second then))
(funcall p (timestamp-microsecond then))
(funcall p (timestamp-timezone then))
;; The day of the week is also available.
(funcall p (timestamp-day-of-week then))
;; These functions compare two times, testing if the
;; first occurs before, after, or at the same time
;; as the second, respectively.
(funcall p (timestamp< then now))
(funcall p (timestamp> then now))
(funcall p (timestamp= then now))
;; The time-difference function returns a duration representing
;; the interval between two times.
(let ((diff (timestamp-difference now then)))
(funcall p diff)
;; We can compute the length of the duration in
;; various units.
(funcall p (duration-as diff :hour))
(funcall p (duration-as diff :minute))
(funcall p (duration-as diff :sec))
(funcall p (duration-as diff :nsec))
;; You can use timestamp+ to advance a time by a given
;; duration, or with a negative duration to move backwards.
(funcall p (timestamp+ then diff))
(funcall p (timestamp+ then (duration- diff))))))))
(main)
This Lisp code provides similar functionality to the original Go example, using the local-time
library which is commonly used for time operations in Common Lisp.
Here’s a brief explanation of the changes:
time-example
and use the local-time
library.main
function encapsulates all the operations.now
to get the current time, similar to time.Now()
in Go.encode-timestamp
is used to create a specific timestamp, similar to time.Date
in Go.timestamp-year
, timestamp-month
, etc., are used to extract components of the timestamp.timestamp<
, timestamp>
, and timestamp=
are used for time comparisons.timestamp-difference
is used to calculate the duration between two times.duration-as
is used to convert the duration to different units.timestamp+
is used to add or subtract durations from a timestamp.Note that Lisp doesn’t have a direct equivalent to Go’s nanosecond precision, so we use microseconds instead. Also, the day of the week is represented as a number (0-6) rather than a named constant.
To run this program, you would need to have a Common Lisp implementation installed along with the local-time
library. You can then load and run this file in your Lisp environment.