}
DEFUN ("time-add", Ftime_add, Stime_add, 2, 2, 0,
- doc: /* Return the sum of two time values A and B, as a timestamp.
-See Info node `(elisp)Time of Day' for time value formats.
+ doc: /* Return the sum of two time values A and B, as a time value.
+See `format-time-string' for the various forms of a time value.
For example, nil stands for the current time. */)
(Lisp_Object a, Lisp_Object b)
{
}
DEFUN ("time-subtract", Ftime_subtract, Stime_subtract, 2, 2, 0,
- doc: /* Return the difference between two time values A and B, as a timestamp.
+ doc: /* Return the difference between two time values A and B, as a time value.
You can use `float-time' to convert the difference into elapsed seconds.
-See Info node `(elisp)Time of Day' for time value formats.
+See `format-time-string' for the various forms of a time value.
For example, nil stands for the current time. */)
(Lisp_Object a, Lisp_Object b)
{
DEFUN ("time-less-p", Ftime_less_p, Stime_less_p, 2, 2, 0,
doc: /* Return non-nil if time value A is less than time value B.
-See Info node `(elisp)Time of Day' for time value formats.
+See `format-time-string' for the various forms of a time value.
For example, nil stands for the current time. */)
(Lisp_Object a, Lisp_Object b)
{
DEFUN ("time-equal-p", Ftime_equal_p, Stime_equal_p, 2, 2, 0,
doc: /* Return non-nil if A and B are equal time values.
-See Info node `(elisp)Time of Day' for time value formats. */)
+See `format-time-string' for the various forms of a time value. */)
(Lisp_Object a, Lisp_Object b)
{
return time_cmp (a, b) == 0 ? Qt : Qnil;
DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0,
doc: /* Return the current time, as a float number of seconds since the epoch.
-If SPECIFIED-TIME is given, it is a Lisp time value to convert to
-float instead of the current time. See Info node `(elisp)Time of Day'
-for time value formats.
+If SPECIFIED-TIME is given, it is a time value to convert to float
+instead of the current time. See `format-time-string' for the various
+forms of a time value.
WARNING: Since the result is floating point, it may not be exact.
-If precise time stamps are required, use either `current-time',
+If precise time stamps are required, use either `encode-time',
or (if you need time as a string) `format-time-string'. */)
(Lisp_Object specified_time)
{
}
DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0,
- doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted or nil.
-TIME is a Lisp time value; see Info node `(elisp)Time of Day'.
+ doc: /* Use FORMAT-STRING to format the time value TIME.
+A time value that is omitted or nil stands for the current time,
+a number stands for that many seconds, an integer pair (TICKS . HZ)
+stands for TICKS/HZ seconds, and an integer list (HI LO US PS) stands
+for HI*2**16 + LO + US/10**6 + PS/10**12 seconds. This function
+treats seconds as time since the epoch of 1970-01-01 00:00:00 UTC.
The optional ZONE is omitted or nil for Emacs local time, t for
Universal Time, `wall' for system wall clock time, or a string as in
DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 2, 0,
doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST UTCOFF).
-The optional TIME is the Lisp time value to convert. See Info node
-`(elisp)Time of Day' for time value formats.
+The optional TIME is the time value to convert. See
+`format-time-string' for the various forms of a time value.
The optional ZONE is omitted or nil for Emacs local time, t for
Universal Time, `wall' for system wall clock time, or a string as in
}
DEFUN ("encode-time", Fencode_time, Sencode_time, 1, MANY, 0,
- doc: /* Convert TIME to a timestamp.
+ doc: /* Convert optional TIME to a timestamp.
Optional FORM specifies how the returned value should be encoded.
This can act as the reverse operation of `decode-time', which see.
If TIME is a list (SECOND MINUTE HOUR DAY MONTH YEAR IGNORED DST ZONE)
-it a decoded time in the style of `decode-time', so that (encode-time
-(decode-time ...)) works. TIME can also be a Lisp time value; see
-Info node `(elisp)Time of Day'.
+it is a decoded time in the style of `decode-time', so that (encode-time
+(decode-time ...)) works. TIME can also be a time value.
+See `format-time-string' for the various forms of a time value.
+For example, an omitted TIME stands for the current time.
If FORM is a positive integer, the time is returned as a pair of
integers (TICKS . FORM), where TICKS is the number of clock ticks and FORM
is the clock frequency in ticks per second. (Currently the positive
integer should be at least 65536 if the returned value is expected to
be given to standard functions expecting Lisp timestamps.) If FORM is
-t, the time is returned as (TICKS . PHZ), where PHZ is a
-platform-dependent clock frequency. If FORM is `integer', the time is
+t, the time is returned as (TICKS . PHZ), where PHZ is a platform dependent
+clock frequency in ticks per second. If FORM is `integer', the time is
returned as an integer count of seconds. If FORM is `list', the time is
returned as an integer list (HIGH LOW USEC PSEC), where HIGH has the
most significant bits of the seconds, LOW has the least significant 16
omitted or nil FORM currently acts like `list', this is planned to
change, so callers requiring list timestamps should specify `list'.
-As an obsolescent calling convention, the first 6 arguments SECOND,
-MINUTE, HOUR, DAY, MONTH, and YEAR specify the components of a decoded
-time, where DST assumed to be -1 and FORM is omitted. If there are more
+As an obsolescent calling convention, if this function is called with
+6 or more arguments, the first 6 arguments are SECOND, MINUTE, HOUR,
+DAY, MONTH, and YEAR, and specify the components of a decoded time,
+where DST assumed to be -1 and FORM is omitted. If there are more
than 6 arguments the *last* argument is used as ZONE and any other
-extra arguments are ignored, so that (apply \\='encode-time
+extra arguments are ignored, so that (apply #\\='encode-time
(decode-time ...)) works; otherwise ZONE is assumed to be nil.
If the input is a decoded time, ZONE is nil for Emacs local time, t
Years before 1970 are not guaranteed to work. On some systems,
year values as low as 1901 do work.
-usage: (encode-time TIME &optional FORM) */)
+usage: (encode-time &optional TIME FORM &rest OBSOLESCENT-ARGUMENTS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
time_t value;
}
DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
- doc: /* Return the current time, counting the number of seconds since the epoch.
-
-See Info node `(elisp)Time of Day' for the format of the returned
-timestamp. Although this is currently list format, it may change in
-future versions of Emacs. Use `encode-time' if you need a particular
-form; for example, (encode-time nil \\='list) returns the current time
-in list form. */)
+ doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
+The time is returned as a list of integers (HIGH LOW USEC PSEC).
+HIGH has the most significant bits of the seconds, while LOW has the
+least significant 16 bits. USEC and PSEC are the microsecond and
+picosecond counts. Use `encode-time' if you need a particular
+timestamp form; for example, (encode-time nil \\='integer) returns the
+current time in seconds. */)
(void)
{
return make_lisp_time (current_timespec ());
However, see also the functions `decode-time' and `format-time-string'
which provide a much more powerful and general facility.
-If SPECIFIED-TIME is given, it is the Lisp time value to format
-instead of the current time. See Info node `(elisp)Time of Day' for
-time value formats.
+If SPECIFIED-TIME is given, it is the time value to format instead of
+the current time. See `format-time-string' for the various forms of a
+time value.
The optional ZONE is omitted or nil for Emacs local time, t for
Universal Time, `wall' for system wall clock time, or a string as in
NAME is a string giving the name of the time zone.
If SPECIFIED-TIME is given, the time zone offset is determined from it
instead of using the current time. The argument should be a Lisp
-time value; see Info node `(elisp)Time of Day'.
+time value; see `format-time-string' for the various forms of a time
+value.
The optional ZONE is omitted or nil for Emacs local time, t for
Universal Time, `wall' for system wall clock time, or a string as in
(require 'ert)
-;;; Check format-time-string with various TZ settings.
+;;; Check format-time-string and decode-time with various TZ settings.
;;; Use only POSIX-compatible TZ values, since the tests should work
;;; even if tzdb is not in use.
(ert-deftest format-time-string-with-zone ()
;; Similarly, stick to the limited set of time zones that are
;; supported by both POSIX and MS-Windows: exactly 3 ASCII letters
;; in the abbreviation, and no DST.
- (let ((look '(1202 22527 999999 999999))
- (format "%Y-%m-%d %H:%M:%S.%3N %z (%Z)"))
- ;; UTC.
- (should (string-equal
- (format-time-string "%Y-%m-%d %H:%M:%S.%3N %z" look t)
- "1972-06-30 23:59:59.999 +0000"))
- ;; "UTC0".
- (should (string-equal
- (format-time-string format look "UTC0")
- "1972-06-30 23:59:59.999 +0000 (UTC)"))
- ;; Negative UTC offset, as a Lisp list.
- (should (string-equal
- (format-time-string format look '(-28800 "PST"))
- "1972-06-30 15:59:59.999 -0800 (PST)"))
- ;; Negative UTC offset, as a Lisp integer.
- (should (string-equal
- (format-time-string format look -28800)
- ;; MS-Windows build replaces unrecognizable TZ values,
- ;; such as "-08", with "ZZZ".
- (if (eq system-type 'windows-nt)
- "1972-06-30 15:59:59.999 -0800 (ZZZ)"
- "1972-06-30 15:59:59.999 -0800 (-08)")))
- ;; Positive UTC offset that is not an hour multiple, as a string.
- (should (string-equal
- (format-time-string format look "IST-5:30")
- "1972-07-01 05:29:59.999 +0530 (IST)"))))
+ (let ((format "%Y-%m-%d %H:%M:%S.%3N %z (%Z)"))
+ (dolist (look '((1202 22527 999999 999999)
+ (7879679999900 . 100000)
+ (78796799999999999999 . 1000000000000)))
+ ;; UTC.
+ (should (string-equal
+ (format-time-string "%Y-%m-%d %H:%M:%S.%3N %z" look t)
+ "1972-06-30 23:59:59.999 +0000"))
+ (should (equal (decode-time look t)
+ '(59 59 23 30 6 1972 5 nil 0)))
+ ;; "UTC0".
+ (should (string-equal
+ (format-time-string format look "UTC0")
+ "1972-06-30 23:59:59.999 +0000 (UTC)"))
+ (should (equal (decode-time look "UTC0")
+ '(59 59 23 30 6 1972 5 nil 0)))
+ ;; Negative UTC offset, as a Lisp list.
+ (should (string-equal
+ (format-time-string format look '(-28800 "PST"))
+ "1972-06-30 15:59:59.999 -0800 (PST)"))
+ (should (equal (decode-time look '(-28800 "PST"))
+ '(59 59 15 30 6 1972 5 nil -28800)))
+ ;; Negative UTC offset, as a Lisp integer.
+ (should (string-equal
+ (format-time-string format look -28800)
+ ;; MS-Windows build replaces unrecognizable TZ values,
+ ;; such as "-08", with "ZZZ".
+ (if (eq system-type 'windows-nt)
+ "1972-06-30 15:59:59.999 -0800 (ZZZ)"
+ "1972-06-30 15:59:59.999 -0800 (-08)")))
+ (should (equal (decode-time look -28800)
+ '(59 59 15 30 6 1972 5 nil -28800)))
+ ;; Positive UTC offset that is not an hour multiple, as a string.
+ (should (string-equal
+ (format-time-string format look "IST-5:30")
+ "1972-07-01 05:29:59.999 +0530 (IST)"))
+ (should (equal (decode-time look "IST-5:30")
+ '(59 29 5 1 7 1972 6 nil 19800))))))
+
+(ert-deftest decode-then-encode-time ()
+ (let ((time-values (list 0 -2 1 0.0 -0.0 -2.0 1.0
+ most-negative-fixnum most-positive-fixnum
+ (1- most-negative-fixnum)
+ (1+ most-positive-fixnum)
+ 1e+INF -1e+INF 1e+NaN -1e+NaN
+ '(0 1 0 0) '(1 0 0 0) '(-1 0 0 0)
+ '(123456789000000 . 1000000)
+ (cons (1+ most-positive-fixnum) 1000000000000)
+ (cons 1000000000000 (1+ most-positive-fixnum)))))
+ (dolist (a time-values)
+ (let* ((d (ignore-errors (decode-time a t)))
+ (e (encode-time d))
+ (diff (float-time (time-subtract a e))))
+ (should (or (not d)
+ (and (<= 0 diff) (< diff 1))))))))
;;; This should not dump core.
(ert-deftest format-time-string-with-outlandish-zone ()
(ert-deftest time-equal-p-nil-nil ()
(should (time-equal-p nil nil)))
+
+(ert-deftest time-arith-tests ()
+ (let ((time-values (list 0 -1 1 0.0 -0.0 -1.0 1.0
+ most-negative-fixnum most-positive-fixnum
+ (1- most-negative-fixnum)
+ (1+ most-positive-fixnum)
+ 1e+INF -1e+INF 1e+NaN -1e+NaN
+ '(0 0 0 1) '(0 0 1 0) '(0 1 0 0) '(1 0 0 0)
+ '(-1 0 0 0) '(1 2 3 4) '(-1 2 3 4)
+ '(-123456789 . 100000) '(123456789 . 1000000)
+ (cons (1+ most-positive-fixnum) 1000000000000)
+ (cons 1000000000000 (1+ most-positive-fixnum)))))
+ (dolist (a time-values)
+ (dolist (b time-values)
+ (let ((aa (time-subtract (time-add a b) b)))
+ (should (or (time-equal-p a aa) (and (floatp aa) (isnan aa)))))
+ (should (= 1 (+ (if (time-less-p a b) 1 0)
+ (if (time-equal-p a b) 1 0)
+ (if (time-less-p b a) 1 0)
+ (if (or (and (floatp a) (isnan a))
+ (and (floatp b) (isnan b)))
+ 1 0))))
+ (should (or (not (time-less-p 0 b))
+ (time-less-p a (time-add a b))
+ (time-equal-p a (time-add a b))
+ (and (floatp (time-add a b)) (isnan (time-add a b)))))
+ (let ((x (float-time (time-add a b)))
+ (y (+ (float-time a) (float-time b))))
+ (should (or (and (isnan x) (isnan y))
+ (= x y)
+ (< 0.99 (/ x y) 1.01)
+ (< 0.99 (/ (- (float-time a)) (float-time b))
+ 1.01))))))))