Lisp - Handling Date & Time



In Lisp, we can handle date and time using builtin functions and using external libraries for more advanced operations on date and time.

Built-in Functions

Following are some of the important built-in functions in Common Lisp to work with date and time.

  • get-universal-time function is used to get the current date time as a UTC time, number of seconds elapsed since January 1, 1900, 00:00:00 GMT.

  • decode-universal-timefunction is used to converts a universal time into a decoded time, which is in form of a tuple of nine values. Each value represents seconds, minutes, hours, day, month, year, day of the week (0=Monday to 6=Sunday), daylight saving time flag (T or NIL), and time zone (hours west of GMT).

  • encode-universal-time function does reverse of decode-universal-time and is used to convert a decoded time back into a universal time.

  • get-decoded-timefunction is equivalent to (decode-universal-time (get-universal-time))) and returns the current time as a decoded time.

  • get-internal-real-time function is used for internal purpose and returns the current real time in implementation-dependent units.

  • get-internal-run-time function is used to get the current run time of the Lisp process in implementation-dependent units.

  • internal-time-units-per-second holds the number of internal time units in one second for the current Lisp implementation.

Usage of Universal Time

get-universal-time returns the time in number of seconds.

main.lisp

(print (get-universal-time))

Output

When you execute the code, it returns the following result −

3955268890

Decode Universal Time

decode-universal-time can be used to get parse universal time as shown below −

main.lisp

(multiple-value-bind (second minute hour day month year day-of-week dst-flag time-zone)
   (decode-universal-time (get-universal-time))
      (format t "Time ~2,'0d:~2,'0d:~2,'0d on ~A, ~d/~d/~d (GMT~@d)"
         hour minute second
         (case day-of-week
            (0 "Mon") (1 "Tue") (2 "Wed") (3 "Thu")
            (4 "Fri") (5 "Sat") (6 "Sun"))
            day month year time-zone))

Output

When you execute the code, it returns the following result −

Time 19:20:34 on Sat, 3/5/2025 (GMT-11/2)

Where −

  • Seconds − Represents second of the minute (0-59). In the example above, it's 34.

  • Minutes − Represents minute of the hour (0-59). In the example, it's 20.

  • Hours − Represents hour of the day (0-23). In the example, it's 19.

  • Day − Represents day of the month (1-31). In the example, it's 3.

  • Month − Represents month of the year (1-12). In the example, it's 5 (May).

  • Year − Represents the year. In the example, it's 2025.

  • Day of the Week − represented by an integer as the day of the week, where 0 is Monday and 6 is Sunday. In the example, 3 corresponds to Saturday.

  • Daylight Saving Time Flag − is a boolean value. It is t where day light savings is applicable. In the example, it's NIL.

  • Time Zone − It represents the difference in hours between the local time zone and Greenwich Mean Time, GMT. For Delhi, which is typically GMT+5:30. In the example, it is 11/2.

External Libraries

We can import external libraries for comprehensive coverage on date time operations. Following are some of the popular libraries for Common Lisp.

  • local-time − It is the most popular external library for date time manipulation. It provide a rich set of functionalities including timezone handling, date and time arithmetic, conversion between universal time, unix time and timestamps and lots more.

  • cl-dates − It is mainly used in business and financial applications for its execellent date manipulation abilities. It supports date formatting, date arithmetic, calculating date differences and so on.

  • simple-date-time − It is a simple date time library providing basic date and time handling.

  • cl-date-time-parser − It is specific to convert date in various formats to universal time.

Advertisements