Format a timestamp including GMT offset in common lisp?

59 Views Asked by At

Using common lisp with the "local-time" package, I'm trying to format a timestamp including the GMT offset. The documentation includes a ":gmt-offset" format parameter, but using it generates an error. I looked at the source code on github and the case statement seems to include :gmt-offset. Am I doing something wrong? Am I getting an older version of local-time which doesn't have gmt-offset?

(ql:quickload "local-time")
(local-time:format-timestring nil (local-time:now) :format
    '((:year 4)  #\.
      (:month 2) #\.
      (:day 2)   #\.
      (:hour 2)  #\.
      (:min 2)   #\.
      (:sec 2)   #\.
      (:msec 3)  #\-
      (:gmt-offset)))

Loading it in SBCL 2.3.4 gives me:

debugger invoked on a SB-KERNEL:CASE-FAILURE @225F39AB in thread #<THREAD "main thread" RUNNING {10044C00A3}>: :GMT-OFFSET fell through ECASE expression. Wanted one of (:NSEC :USEC :MSEC :SEC :MIN :HOUR :HOUR12 :DAY :WEEKDAY :MONTH :YEAR :ISO-WEEK-YEAR ...).

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(LOCAL-TIME::%CONSTRUCT-TIMESTRING #<unavailable argument> #<unavailable argument> #<unavailable argument>)
   source: (ECASE (IF (CONSP FMT) (CAR FMT) FMT) (:NSEC NSEC) (:USEC (FLOOR NSEC 1000)) (:MSEC (FLOOR NSEC 1000000)) (:SEC SEC) (:MIN MINUTE) (:HOUR HOUR) (:HOUR12 (1+ (MOD (1- HOUR) 12))) (:DAY DAY) (:WEEKDAY WEEKDAY) (:MONTH MONTH) ...)
1

There are 1 best solutions below

1
jsparkes On

I found the answer. I just had to remove the parentheses around :gmt-offset. I guess that it doesn't support the padding parameters there. This is a consistency issue, along with a documentation issue, perhaps there should be an example.

I'll leave this here in case someone else runs into this.

The correct format is:

(ql:quickload "local-time")
(local-time:format-timestring nil (local-time:now) :format
                  '((:year 4) #\.
                (:month 2) #\.
                (:day 2) #\.
                (:hour 2) #\.
                (:min 2) #\.
                (:sec 2) #\.
                (:msec 3) #\-
                :gmt-offset
                ))