如何使用Emacs Lisp在Emacs中添加日期?

11

我希望使用Emacs Lisp在日期和时间上执行像加法和减法这样的数学运算。

1个回答

23
简短的回答是:阅读elisp信息手册的系统接口部分。 更具体地说,时间部分: 更长的回答是:
Emacs可以将时间值作为字符串、浮点数或两个或三个整数的元组来处理。 例如,在*scratch*缓冲区中调用一些函数:
(current-time)
(19689 59702 984160)
(current-time-string)
"Sun Nov 21 20:54:22 2010"
(current-time-zone)
(-25200 "MST")
(float-time)
1290398079.965001

让我们进行一些转换:

(decode-time (current-time))
(33 7 21 21 11 2010 0 nil -25200)
(decode-time) ; (current-time) by default
(51 7 21 21 11 2010 0 nil -25200)

(let ((seconds 36)
      (minutes 10)
      (hour 21)
      (day 21)
      (month 11)
      (year 2010))
  (encode-time seconds minutes hour day month year))
(19689 60732)

(format-time-string "%A %e %B" (current-time))
"Sunday 21 November"

(seconds-to-time 23)
(0 23 0)

(time-to-seconds (current-time))
1290399309.598342

(time-to-days (current-time))
734097

最后,回答你的问题:
(time-add (current-time) (seconds-to-time 23))
(19689 60954 497526)

(time-subtract (current-time) (seconds-to-time 45))
(19689 61001 736330)

我正在阅读它,但我没有注意到时间计算部分,谢谢! - user9903
我认为在你的最终示例中不需要 seconds-to-time - Michaël

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接