阅读encode-time
和decode-time
的文档后,我不了解它们的工作原理。
我的麻烦来自encode-time
返回两个值的事实,而我期望只有一个(自UNIX时代以来的秒数)。这两个值到底是什么意思?
ELISP> (encode-time 0 0 0 1 1 1970 t)
(0 0)
ELISP> (encode-time 0 0 0 1 1 1971 t)
(481 13184)
由于这三个调用起作用,我变得更加困惑,但是在前两种情况下我不理解如何解释该论点:
;; Ok this is what I expect from the documentation
ELISP> (decode-time
(encode-time 0 0 0 1 1 1971 t))
(0 0 0 1 1 1971 5 nil 0)
;; This is also what I expected, given it's the same than above, but I don't understand the meaning of the arguments
ELISP> (decode-time '(481 13184))
(0 0 0 1 1 1971 5 nil 0)
;; I kind of understand the following: here 481 is considered the seconds since UNIX epoch,
;; and 13184 is the difference in seconds from UTC.
;; The returned value, is the date (UNIX epoch + 481 seconds)
;; expressed in a timezone 13184 seconds ahead of UTC (45 47 3 1 1 1970 = 13665 seconds
;; and 13665−13184 = 481)
ELISP> (decode-time 481 13184)
(45 47 3 1 1 1970 4 nil 13184)
请注意,对于以上所有示例,我都设置了(set-time-zone-rule t)
,以避免必须处理时区。
答案 0 :(得分:2)
(481 131184)
用(HIGH LOW)
表示秒数,其中HIGH
是高位,而LOW
是低16位。即真正的秒数是
(+ (* 481 65536) 13184) == 31536000
答案 1 :(得分:1)
查看文档:
(decode-time &optional TIME ZONE)
将时间值解码为(SEC MINUTE HOUR DAY MONTH YEAR DOW DST UTCOFF)。 可选的TIME应该是(HIGH LOW。IGNORED)的列表, 从“当前时间” 和“文件属性”开始,或者从零开始使用 当前时间。也可以是一个整数秒 时代。过时的格式(HIGH。LOW)仍然可以接受。
因此,我们可以看到期望的格式是带有值“ HIGH”和“ LOW”的列表(可能还有一些其他信息)。遵循指向current-time
的指示为我们提供了进一步的解释:
(current-time)
返回当前时间,以自1970-01-01 00:00:00以来的秒数为单位。 时间以整数列表形式返回(HIGH LOW USEC PSEC)。 HIGH具有秒的最高有效位,而LOW具有秒 最低有效16位。 USEC和PSEC是微秒, 皮秒计数。
Emacs为什么使用这种看似复杂的格式?毫无疑问,答案很简单,那就是Emacs很旧,它需要与只有16位整数可用的系统一起使用。
查看手册,elisp-index-search
的 M-x current-time
将我们带到(elisp)Time of Day
:
大多数这些函数将时间表示为四个整数的列表 ‘((SEC-HIGH SEC-LOW MICROSEC PICOSEC)’)。这代表 从“纪元”(1970年1月1日,世界标准时间00:00)开始的秒数, 公式:HIGH * 2 ** 16 + LOW + MICRO * 10 ** − 6 + PICO * 10 ** − 12。的 “ current-time”的返回值代表使用此表单的时间, 其他函数的返回值中的时间戳,例如 “文件属性”(*请注意文件属性的定义::)。在一些 情况下,函数可能会返回包含两个或三个元素的列表,并且省略 MICROSEC和PICOSEC组件默认为零。
函数参数,例如“ current-time-string”的TIME参数, 接受更通用的“时间值”格式,该格式可以是 如上所示的整数,或者从纪元开始数秒的单个数字,或者 当前时间为“无”。您可以将时间值转换为 使用“当前时间字符串”和 将“格式时间字符串”转换为使用“秒数”的整数列表, 以及使用“解码时间”和“浮动时间”的其他形式。 功能将在以下各节中介绍。
因此,您可以例如将单个32位Unix时间戳整数传递给decode-time
(decode-time 1554670400)
=> (20 53 8 8 4 2019 1 nil 43200)
您还可以使用format-time-string
获得一个整数值:
(string-to-number (format-time-string "%s" (current-time)))