ts
是 Emacs 的日期和时间库。它的目标是通过提供简单的访问器(如(ts-year (ts-now))
来比(string-to-number (format-time-string "%Y"))
等模式更方便。
为了(显着)提高性能,格式化的日期部分是延迟计算的,而不是在实例化时间戳对象时计算的,然后缓存计算出的部分以供以后访问而无需重新计算。在幕后,这避免了不必要的(string-to-number (format-time-string...
调用,这些调用非常昂贵。
获取当前日期的部分内容:
; ; When the current date is 2018-12-08 23:09:14 -0600:
(ts-year (ts-now)) ; => 2018
(ts-month (ts-now)) ; => 12
(ts-day (ts-now)) ; => 8
(ts-hour (ts-now)) ; => 23
(ts-minute (ts-now)) ; => 9
(ts-second (ts-now)) ; => 14
(ts-tz-offset (ts-now)) ; => "-0600"
(ts-dow (ts-now)) ; => 6
(ts-day-abbr (ts-now)) ; => "Sat"
(ts-day-name (ts-now)) ; => "Saturday"
(ts-month-abbr (ts-now)) ; => "Dec"
(ts-month-name (ts-now)) ; => "December"
(ts-tz-abbr (ts-now)) ; => "CST"
增加当前日期:
; ; By 10 years:
( list :now (ts-format)
:future (ts-format (ts-adjust 'year 10 (ts-now))))
; ;=> ( :now "2018-12-15 22:00:34 -0600"
; ; :future "2028-12-15 22:00:34 -0600")
; ; By 10 years, 2 months, 3 days, 5 hours, and 4 seconds:
( list :now (ts-format)
:future (ts-format
(ts-adjust 'year 10 'month 2 'day 3
'hour 5 'second 4
(ts-now))))
; ;=> ( :now "2018-12-15 22:02:31 -0600"
; ; :future "2029-02-19 03:02:35 -0600")
两天前是星期几?
(ts-day-name (ts-dec 'day 2 (ts-now))) ; => "Thursday"
; ; Or, with threading macros:
( thread-last (ts-now) (ts-dec 'day 2 ) ts-day-name) ; => "Thursday"
(-> > (ts-now) (ts-dec 'day 2 ) ts-day-name) ; => "Thursday"
获取上周这个时间的时间戳:
(ts-unix (ts-adjust 'day -7 (ts-now)))
; ;=> 1543728398.0
; ; To confirm that the difference really is 7 days:
( / ( - (ts-unix (ts-now))
(ts-unix (ts-adjust 'day -7 (ts-now))))
86400 )
; ;=> 7.000000567521762
; ; Or human-friendly as a list:
(ts-human-duration
(ts-difference (ts-now)
(ts-dec 'day 7 (ts-now))))
; ;=> (:years 0 :days 7 :hours 0 :minutes 0 :seconds 0)
; ; Or as a string:
(ts-human-format-duration
(ts-difference (ts-now)
(ts-dec 'day 7 (ts-now))))
; ;=> "7 days"
; ; Or confirm by formatting:
( list :now (ts-format)
:last-week (ts-format (ts-dec 'day 7 (ts-now))))
; ;=> ( :now "2018-12-08 23:31:37 -0600"
; ; :last-week "2018-12-01 23:31:37 -0600")
一些访问器具有类似于format-time-string
构造函数的别名:
(ts-hour (ts-now)) ; => 0
(ts-H (ts-now)) ; => 0
(ts-minute (ts-now)) ; => 56
(ts-min (ts-now)) ; => 56
(ts-M (ts-now)) ; => 56
(ts-second (ts-now)) ; => 38
(ts-sec (ts-now)) ; => 38
(ts-S (ts-now)) ; => 38
(ts-year (ts-now)) ; => 2018
(ts-Y (ts-now)) ; => 2018
(ts-month (ts-now)) ; => 12
(ts-m (ts-now)) ; => 12
(ts-day (ts-now)) ; => 9
(ts-d (ts-now)) ; => 9
将字符串解析为时间戳对象并重新格式化它:
(ts-format (ts-parse " sat dec 8 2018 12:12:12 " )) ; => "2018-12-08 12:12:12 -0600"
; ; With a threading macro:
(-> > " sat dec 8 2018 12:12:12 "
ts-parse
ts-format) ; ;=> "2018-12-08 12:12:12 -0600"
设置两个时间戳之间的差异的格式:
(ts-human-format-duration
(ts-difference (ts-now)
(ts-adjust 'day -400
'hour -2 'minute -1 'second -5
(ts-now))))
; ; => "1 years, 35 days, 2 hours, 1 minutes, 5 seconds"
; ; Abbreviated:
(ts-human-format-duration
(ts-difference (ts-now)
(ts-adjust 'day -400
'hour -2 'minute -1 'second -5
(ts-now)))
'abbr )
; ; => "1y35d2h1m5s"
直接从org-element-context
解析 Org 时间戳元素并找出它与现在之间的差异:
( with-temp-buffer
( org-mode )
( save-excursion
( insert " <2015-09-24 Thu .+1d> " ))
(ts-human-format-duration
(ts-difference (ts-now)
(ts-parse-org-element ( org-element-context )))))
; ;=> "3 years, 308 days, 2 hours, 24 minutes, 21 seconds"
解析 Org 时间戳字符串(具有中继器)并格式化年份和月份:
; ; Note the use of `format' rather than `concat' , because `ts-year'
; ; returns the year as a number rather than a string.
( let* ((ts (ts-parse-org " <2015-09-24 Thu .+1d> " )))
( format " %s , %s " (ts-month-name ts) (ts-year ts)))
; ;=> "September, 2015"
; ; Or, using dash.el:
(-- > (ts-parse-org " <2015-09-24 Thu .+1d> " )
( format " %s , %s " (ts-month-name it) (ts-year it)))
; ;=> "September, 2015"
; ; Or, if you remember the format specifiers:
(ts-format " %B, %Y " (ts-parse-org " <2015-09-24 Thu .+1d> " ))
; ;=> "September, 2015"
1970 年这个日期是多久以前的事?
( let* ((now (ts-now))
(then (ts-apply :year 1970 now)))
( list (ts-format then)
(ts-human-format-duration
(ts-difference now then))))
; ;=> ("1970-08-04 07:07:10 -0500"
; ; "49 years, 12 days")
这个纪元是多久以前开始的?
(ts-human-format-duration
(ts-diff (ts-now) (make-ts :unix 0 )))
; ;=> "49 years, 227 days, 12 hours, 12 minutes, 30 seconds"
过去 100 年中哪一个圣诞节是在星期六?
( let ((ts (ts-parse " 2019-12-25 " ))
(limit ( - (ts-year (ts-now)) 100 )))
( cl-loop while ( >= (ts-year ts) limit)
when ( string= " Saturday " (ts-day-name ts))
collect (ts-year ts)
do (ts-decf (ts-year ts))))
; ;=> (2010 2004 1999 1993 1982 1976 1971 1965 1954 1948 1943 1937 1926 1920)
举一个更有趣的例子,时间戳是否落在上一个日历周内?
; ; First, define a function to return the range of the previous calendar week.
( defun last-week-range ()
" Return timestamps (BEG . END) spanning the previous calendar week. "
( let* ( ; ; Bind `now' to the current timestamp to ensure all calculations
; ; begin from the same timestamp. (In the unlikely event that
; ; the execution of this code spanned from one day into the next,
; ; that would cause a wrong result.)
(now (ts-now))
; ; We start by calculating the offsets for the beginning and
; ; ending timestamps using the current day of the week. Note
; ; that the `ts-dow' slot uses the "%w" format specifier, which
; ; counts from Sunday to Saturday as a number from 0 to 6.
(adjust-beg-day ( - ( + 7 (ts-dow now))))
(adjust-end-day ( - ( - 7 ( - 6 (ts-dow now)))))
; ; Make beginning/end timestamps based on `now' , with adjusted
; ; day and hour/minute/second values. These functions return
; ; new timestamps, so `now' is unchanged.
(beg ( thread-last now
; ; `ts-adjust' makes relative adjustments to timestamps.
(ts-adjust 'day adjust-beg-day)
; ; `ts-apply' applies absolute values to timestamps.
(ts-apply :hour 0 :minute 0 :second 0 )))
(end ( thread-last now
(ts-adjust 'day adjust-end-day)
(ts-apply :hour 23 :minute 59 :second 59 ))))
( cons beg end)))
(-let* ( ; ; Bind the default format string for `ts-format' , so the
; ; results are easy to understand.
(ts-default-format " %a, %Y-%m-%d %H:%M:%S %z " )
; ; Get the timestamp for 3 days before now.
(check-ts (ts-adjust 'day -3 (ts-now)))
; ; Get the range for the previous week from the function we defined.
((beg . end) (last-week-range)))
( list :last-week-beg (ts-format beg)
:check-ts (ts-format check-ts)
:last-week-end (ts-format end)
:in-range-p (ts-in beg end check-ts)))
; ;=> (:last-week-beg "Sun, 2019-08-04 00:00:00 -0500"
; ; :check-ts "Fri, 2019-08-09 10:00:34 -0500"
; ; :last-week-end "Sat, 2019-08-10 23:59:59 -0500"
; ; :in-range-p t)
ts-B (STRUCT)
ts
struct STRUCT
的槽“month-name”。ts-H (STRUCT)
ts
struct STRUCT
的槽“hour”。ts-M (STRUCT)
ts
struct STRUCT
的槽“分钟”。ts-S (STRUCT)
ts
struct STRUCT
的“第二个”槽。ts-Y (STRUCT)
ts
struct STRUCT
的槽“year”。ts-b (STRUCT)
ts
struct STRUCT
的槽“month-abbr”。ts-d (STRUCT)
ts
struct STRUCT
的槽“day”。ts-day (STRUCT)
ts
struct STRUCT
的槽“day”。ts-day-abbr (STRUCT)
ts
struct STRUCT
的槽“day-abbr”。ts-day-name (STRUCT)
ts
struct STRUCT
的槽“day-name”。ts-day-of-month-num (STRUCT)
ts
struct STRUCT
的槽“day”。ts-day-of-week-abbr (STRUCT)
ts
struct STRUCT
的槽“day-abbr”。ts-day-of-week-name (STRUCT)
ts
struct STRUCT
的槽“day-name”。ts-day-of-week-num (STRUCT)
ts
struct STRUCT
的槽“dow”。ts-day-of-year (STRUCT)
ts
struct STRUCT
的槽“doy”。ts-dom (STRUCT)
ts
struct STRUCT
的槽“day”。ts-dow (STRUCT)
ts
struct STRUCT
的槽“dow”。ts-doy (STRUCT)
ts
struct STRUCT
的槽“doy”。ts-hour (STRUCT)
ts
struct STRUCT
的槽“hour”。ts-internal (STRUCT)
ts
struct STRUCT
的“内部”槽。 Slot 表示 Emacs 内部时间值(例如,由current-time
返回)。ts-m (STRUCT)
ts
struct STRUCT
的槽“month”。ts-min (STRUCT)
ts
struct STRUCT
的槽“分钟”。ts-minute (STRUCT)
ts
struct STRUCT
的槽“分钟”。ts-month (STRUCT)
ts
struct STRUCT
的槽“month”。ts-month-abbr (STRUCT)
ts
struct STRUCT
的槽“month-abbr”。ts-month-name (STRUCT)
ts
struct STRUCT
的槽“month-name”。ts-month-num (STRUCT)
ts
struct STRUCT
的槽“month”。ts-moy (STRUCT)
ts
struct STRUCT
的槽“month”。ts-sec (STRUCT)
ts
struct STRUCT
的“第二个”槽。ts-second (STRUCT)
ts
struct STRUCT
的“第二个”槽。ts-tz-abbr (STRUCT)
ts
struct STRUCT
的槽“tz-abbr”。ts-tz-offset (STRUCT)
ts
struct STRUCT
的槽“tz-offset”。ts-unix (STRUCT)
ts
struct STRUCT
的槽“unix”。ts-week (STRUCT)
ts
struct STRUCT
的槽“woy”。ts-week-of-year (STRUCT)
ts
struct STRUCT
的槽“woy”。ts-woy (STRUCT)
ts
struct STRUCT
的槽“woy”。ts-year (STRUCT)
ts
struct STRUCT
的槽“year”。 ts-apply (&rest SLOTS TS)
TS
返回新时间戳。填充时间戳槽,覆盖给定的槽值,并使用从新槽值派生的 Unix 时间戳值返回新时间戳。 SLOTS
是一个交替键值对的列表,就像传递给make-ts
那样。ts-adjust (&rest ADJUSTMENTS)
ADJUSTMENTS
应用于TS
新时间戳。 ADJUSTMENTS
应该是一系列交替的SLOTS
和VALUES
用于调整它们。例如,此表单返回 47 小时后的新时间戳: (ts-adjust 'hour -1 'day +2 (ts-now))
由于时间戳参数位于最后,因此它适合在线程宏中使用。
ts-dec (SLOT VALUE TS)
TS
新时间戳,其SLOT
递减VALUE
。 SLOT
应指定为普通符号,而不是关键字。ts-inc (SLOT VALUE TS)
TS
新时间戳,其SLOT
增加VALUE
。 SLOT
应指定为普通符号,而不是关键字。ts-update (TS)
TS
。非破坏性。在设置插槽后使用,例如ts-fill
。破坏性的
ts-adjustf (TS &rest ADJUSTMENTS)
ADJUSTMENTS
时间戳TS
。该函数具有破坏性,因为它在TS
上调用setf
。 ADJUSTMENTS
应该是一系列交替的SLOTS
和VALUES
用于调整它们。例如,此表单将时间戳调整为未来 47 小时:
(let ((ts (ts-now))) (ts-adjustf ts 'hour -1 'day +2))
ts-decf (PLACE &optional (VALUE 1))
PLACE
递减VALUE
(默认为 1),更新其 Unix 时间戳,并返回PLACE
的新值。ts-incf (PLACE &optional (VALUE 1))
PLACE
添加VALUE
(默认为 1),更新其 Unix 时间戳,并返回PLACE
的新值。 ts-in (BEG END TS)
TS
在BEG
到END
范围内(含),则返回非零。所有参数都应该是ts
结构。ts< (AB)
A
小于时间戳B
则返回非零。ts<= (AB)
A
<= 时间戳B
则返回非零。ts= (AB)
A
与时间戳B
相同,则返回非零。仅比较时间戳的unix
插槽。请注意,时间戳的 Unix 时隙是浮点数,可能相差不到一秒,导致即使时间戳的所有格式化部分都相同,它们也会不相等。ts> (AB)
A
大于时间戳B
则返回非零。ts>= (AB)
A
>= 时间戳B
则返回非零。 ts-human-duration (SECONDS)
SECONDS
plist,以年、日、小时、分钟和秒为单位。这是一个简单的计算,没有考虑闰年、闰秒等。ts-human-format-duration (SECONDS &optional ABBREVIATE)
SECONDS
的人类格式的字符串。如果SECONDS
小于 1,则返回"0 seconds"
。如果ABBREVIATE
不为零,则返回一个较短的版本,不带空格。这是一个简单的计算,没有考虑闰年、闰秒等。 ts-format (&optional TS-OR-FORMAT-STRING TS)
format-time-string
设置时间戳格式。如果TS-OR-FORMAT-STRING
是时间戳或 nil,则使用ts-default-format
的值。如果TS-OR-FORMAT-STRING
和TS
都为零,则使用当前时间。 ts-parse (STRING)
ts
结构,使用parse-time-string
解析STRING
。ts-parse-fill (FILL STRING)
ts
结构,使用parse-time-string
解析STRING
。空的小时/分钟/秒值根据FILL
填充:如果begin
,则为 0;如果end
,则小时填充为 23,分/秒填充为 59;如果为零,则当时间值为空时可能会发出错误信号。请注意,当FILL
为end
时,像“12:12”这样的时间值将填充为“12:12:00”,而不是“12:12:59”。ts-parse-org (ORG-TS-STRING)
ORG-TS-STRING
的时间戳对象。请注意,调用了org-parse-time-string
函数,应在调用该函数之前加载该函数。ts-parse-org-fill (FILL ORG-TS-STRING)
ORG-TS-STRING
的时间戳对象。请注意,调用了org-parse-time-string
函数,应在调用该函数之前加载该函数。时/分/秒值根据FILL
填充:如果begin
,则为 0;如果end
,小时填充为 23,分钟/秒填充为 59。请注意, org-parse-time-string
不支持包含秒的时间戳。ts-parse-org-element (ELEMENT)
ELEMENT
的时间戳对象。 Element 应该像org-element
解析的那样,第一个元素是timestamp
。假设时间戳不是一个范围。 copy-ts (TS)
TS
的副本。ts-difference (AB)
A
和B
之间的秒差。ts-diff
ts-difference
的别名。ts-fill (TS &optional ZONE)
TS
已填充其 Unix 时间戳的所有槽。这是非破坏性的。 ZONE
被传递给format-time-string
,如下所示。ts-now
ts
结构集返回到现在。ts-p (STRUCT)
ts-reset (TS)
TS
,清除除unix
之外的所有插槽。非破坏性。等同于: (make-ts :unix (ts-unix ts))
ts-defstruct (&rest ARGS)
cl-defstruct
类似,但具有额外的槽选项。其他插槽选项和值:
:accessor-init
:如果槽为 nil,则初始化访问器中的槽。符号struct
将绑定到当前结构。访问器是在结构体完全定义之后定义的,因此它可以引用结构体定义(例如,通过使用cl-struct
pcase
宏)。
:aliases
:将作为插槽访问器的别名的符号A
,前缀为结构名称(例如,带有插槽year
和别名y
结构ts
将创建别名ts-y
)。
ts-human-format-duration
与format-seconds
Emacs 有一个内置函数format-seconds
,它生成类似于ts-human-format-duration
的输出。其输出也可以通过格式字符串进行控制。但是,如果ts-human-format-duration
的输出足够,它的性能比format-seconds
好得多。这个简单的基准测试运行了 100,000 次,表明它运行得更快并且产生的垃圾更少:
(bench-multi-lexical :times 100000
:forms (( " ts-human-format-duration " (ts-human-format-duration 15780.910933971405 t ))
( " format-seconds " ( format-seconds " %yy%dd%hh%mm%ss%z " 15780.910933971405 ))))
形式 | x 比下一个快 | 总运行时间 | GC 数量 | GC 总运行时间 |
---|---|---|---|---|
ts-人类格式持续时间 | 5.82 | 0.832945 | 3 | 0.574929 |
格式秒 | 最慢的 | 4.848253 | 17 号 | 3.288799 |
(有关bench-multi-lexical
宏,请参阅 Emacs 包开发人员手册。)
额外
ts-fill
接受一个ZONE
参数,就像传递给format-time-string
参数一样,参见。 固定的
ts-human-format-duration
返回持续时间小于 1 秒的空字符串(现在返回"0 seconds"
)。 额外
ts-parse-fill
和ts-parse-org-fill
。ts-in
。改变了
ts-now
不再内联。这允许在运行时使用cl-letf
进行更改,这对测试很有帮助。固定的
ts-fill
中。 (其中调用的函数split-string
会修改匹配数据。)ts-parse-org
中。 (其中调用的函数org-parse-time-string
会修改匹配数据。)文档
第一个标记发布。发布到 MELPA。
GPLv3