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

Rud*_*lah 8 emacs datetime elisp

我想使用Emacs Lisp来执行数学运算,例如日期和时间的添加和差异.

mch*_*mch 18

简短的回答是:阅读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
Run Code Online (Sandbox Code Playgroud)

让我们进行一些转换:

(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
Run Code Online (Sandbox Code Playgroud)

最后,回答你的问题:

(time-add (current-time) (seconds-to-time 23))
(19689 60954 497526)

(time-subtract (current-time) (seconds-to-time 45))
(19689 61001 736330)
Run Code Online (Sandbox Code Playgroud)