You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Common Lisp local-time包格式化带GMT偏移时间戳报错排查

Local-Time包:gmt-offset格式参数报错问题解决

问题原因

你遇到的CASE-FAILURE错误,本质是当前使用的local-time版本过旧,不支持:gmt-offset这个格式参数。新版本的local-time源码中,%construct-timestring函数的ECASE分支已经加入:gmt-offset选项,但旧版本没有,所以触发了“参数未匹配”的错误。

解决方案

1. 升级local-time到最新版本

通过Quicklisp执行以下命令强制更新:

(ql:update-dist "quicklisp")
(ql:quickload "local-time" :force t)

2. 正确使用:gmt-offset参数

升级完成后,:gmt-offset可以直接作为格式列表元素,也可以带参数指定输出格式:

  • (:gmt-offset):默认输出完整格式(如+08:00)
  • (:gmt-offset :short):简化格式(如+0800)
  • (:gmt-offset :long):带时区名称的格式(如GMT+08:00)

示例代码:

(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 :short)))

3. 旧版本兼容方案(无法升级时)

如果暂时不能升级包,可以手动获取并格式化GMT偏移:

(ql:quickload "local-time")
(let* ((now (local-time:now))
       (offset-secs (local-time:timestamp-offset now))
       (offset-hours (floor offset-secs 3600))
       (offset-mins (mod (abs offset-secs) 60)))
  (concatenate 'string
               (local-time:format-timestring nil now :format
                   '((:year 4)  #\.
                     (:month 2) #\.
                     (:day 2)   #\.
                     (:hour 2)  #\.
                     (:min 2)   #\.
                     (:sec 2)   #\.
                     (:msec 3)))
               (format nil "~@c~2,'0d:~2,'0d" 
                       (if (plusp offset-secs) #\+ #\-) 
                       (abs offset-hours) 
                       offset-mins)))

内容的提问来源于stack exchange,提问作者jsparkes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:58:38