43.10 时间计算

下列函数使用时间值进行历法相关计算(see 时刻)。与所有时间值一致,其任意时间值参数为 nil 时代表当前系统时间,为有限数值时代表从纪元开始经过的秒数。

Function: time-less-p t1 t2

若时间值 t1 小于时间值 t2,返回 t

Function: time-equal-p t1 t2

若两个时间值 t1t2 相等,返回 t。若任一参数为非数值 NaN,则结果为 nil。 出于比较目的,参数为 nil 代表无限精度的当前时间,因此当一个参数为 nil 而另一个不为 nil 时,该函数返回 nil。调用者可因此使用 nil 表示与任意时间戳均不相等的未知时间值。

Function: time-subtract t1 t2

该函数返回两个时间值的差值 t1t2,以 Lisp 时间戳形式表示。结果为精确值,其时钟精度不低于两个参数中精度较低者。若需要以流逝秒数为单位的差值,可使用 time-convertfloat-time 进行转换。See 时间转换

Function: time-add t1 t2

该函数返回两个时间值之和,转换规则与 time-subtract 相同。 其中一个参数应代表时间差而非时间点,通常为仅表示流逝秒数的单个数值。 以下为向时间值增加若干秒的用法:

(time-add time seconds)
Function: time-to-days time-value

该函数返回公元 1 年年初至 time-value 之间的天数,采用默认时区。 操作系统会限制时间与时区值的有效范围。

Function: days-to-time days

该函数并非 time-to-days 的严格逆运算,出于历史原因,它使用 Emacs 纪元(而非公元 1 年)作为基准。若要得到逆运算结果,需从 days 中减去 (time-to-days 0);此时若 days 为负数,days-to-time 可能返回 nil

Function: time-to-day-in-year time-value

返回 time-value 对应年份内的日期序号,采用默认时区。 操作系统会限制时间与时区值的有效范围。

Function: date-leap-year-p year

year 为闰年,该函数返回 t

Function: date-days-in-month year month

返回 yearmonth 月的天数。例如,2020 年 2 月有 29 天。

Function: date-ordinal-to-time year ordinal

以解码时间结构返回 year 年第 ordinal 天的日期。例如,2004 年第 120 天为 4 月 29 日。