Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Common Lisp

🎈 🎈 🎈
+1

    (defun unix-timestamp-to-date (unix-timestamp)
    (let* ((epoch (encode-universal-time 0 0 0 1 1 1970))
            (time (float unix-timestamp)))
        (multiple-value-bind (second minute hour day month year)
            (decode-universal-time (+ (coerce time 'integer) epoch))
        (format nil "~4d-~2,'0d-~2,'0d ~2,'0d:~2,'0d:~2,'0d" year month day hour minute second))))
    
    ;; Convert Unix timestamp 1093730212 to date
    (format t "Converted Date: ~a~%" (unix-timestamp-to-date 1093730212))          
        

Output:
Converted Date: 2004-08-28 21:56:52
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Common Lisp example the encode-universal-time function creates a time object representing the Unix epoch (1970-01-01 00:00:00 UTC).
The Unix timestamp is then added to this epoch. The decode-universal-time function decodes the combined timestamp into its individual time components (year, month, day, hour, minute, second).
Finally, format is used to print the result in a human-readable format, such as '2004-08-28 21:56:52'.


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