Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Common Lisp

How to convert Datetime to Unix timestamp in Common Lisp

🎈 🎈 🎈
+1

    (defun date-to-unix-timestamp (year month day hour minute second)
    (let* ((epoch (encode-universal-time 0 0 0 1 1 1970))
            (date (encode-universal-time second minute hour day month year)))
        (- date epoch)))
    
    ;; Convert date (2022-05-15 08:10:45) to Unix timestamp
    (format t "Unix Timestamp: ~a~%" (date-to-unix-timestamp 2022 5 15 8 30 45))          
        

Output:
Unix Timestamp: 1652603445
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Common Lisp example, the encode-universal-time function is used to encode both the Unix epoch and the specific date provided (2022-05-15 08:10:45).
The Unix timestamp is then calculated by subtracting the Unix epoch from the encoded date, which gives the number of seconds since January 1, 1970. The result is the Unix timestamp '1652602245'.


Other useful Common Lisp date functions

Function Description Example
encode-universal-time Encodes date and time information into a universal time (seconds since the Common Lisp epoch). (encode-universal-time 45 30 8 15 5 2022) returns the encoded time for 2022-05-15 08:10:45.
decode-universal-time Decodes a universal time (in seconds) into a human-readable date (year, month, day, hour, minute, second). (decode-universal-time (get-universal-time)) returns the current date and time.
get-universal-time Returns the current universal time, the number of seconds since the Common Lisp epoch (January 1, 1900). (get-universal-time) returns the current timestamp.
get-decoded-time Returns the decoded time for the current universal time, providing components like year, month, day, etc. (get-decoded-time) returns the current date and time as multiple values.
multiple-value-bind Allows extracting multiple values returned by functions such as decode-universal-time. (multiple-value-bind (sec min hr day mon yr) (decode-universal-time (get-universal-time)) ...)
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder