]> git.eshelyaron.com Git - emacs.git/commitdiff
Remove some assumptions about timestamp format
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 25 Sep 2018 02:13:34 +0000 (19:13 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 25 Sep 2018 02:15:23 +0000 (19:15 -0700)
These changes remove some assumptions of Lisp code on timestamp
format.  Although we’re not going to change the default format any
time soon, I went looking for code that was too intimate about
details of timestamp format and removed assumptions where this was
easy to do with current Emacs primitives.
* lisp/ido.el (ido-wash-history):
Fix test for zero timestamp.
* lisp/time.el (display-time-event-handler):
Use time-less-p rather than doing it by hand.
(display-time-update): Simplify by using float-time
instead of doing the equivalent by hand.
* lisp/url/url-auth.el (url-digest-auth-make-cnonce):
* test/lisp/calendar/parse-time-tests.el (parse-time-tests):
* test/lisp/emacs-lisp/timer-tests.el (timer-test-multiple-of-time):
* test/lisp/net/tramp-tests.el:
(tramp-test19-directory-files-and-attributes)
(tramp-test22-file-times, tramp-test23-visited-file-modtime):
Don’t assume detailed format of returned Lisp timestamps.

lisp/ido.el
lisp/time.el
lisp/url/url-auth.el
test/lisp/calendar/parse-time-tests.el
test/lisp/emacs-lisp/timer-tests.el
test/lisp/net/tramp-tests.el

index 64d820333f4afdb57d9c675e4f09da5b1a2b4309..7bf4a92b229edbc7d4ce1711e2ecd02c903e4466 100644 (file)
@@ -1518,9 +1518,7 @@ Removes badly formatted data and ignored directories."
                             (consp time)
                             (cond
                              ((integerp (car time))
-                              (and (/= (car time) 0)
-                                   (integerp (car (cdr time)))
-                                   (/= (car (cdr time)) 0)
+                              (and (not (zerop (float-time time)))
                                    (ido-may-cache-directory dir)))
                              ((eq (car time) 'ftp)
                               (and (numberp (cdr time))
index f8d933d48aa02fe3c7970eedb8cea2ba1e7dfe49..bfecba9f9ddbd8e92b5f95f1d33aae365a5bf6ae 100644 (file)
@@ -336,15 +336,10 @@ would give mode line times like `94/12/30 21:07:48 (UTC)'."
         (next-time (timer-relative-time
                     (list (aref timer 1) (aref timer 2) (aref timer 3))
                     (* 5 (aref timer 4)) 0)))
-    ;; If the activation time is far in the past,
+    ;; If the activation time is not in the future,
     ;; skip executions until we reach a time in the future.
     ;; This avoids a long pause if Emacs has been suspended for hours.
-    (or (> (nth 0 next-time) (nth 0 current))
-       (and (= (nth 0 next-time) (nth 0 current))
-            (> (nth 1 next-time) (nth 1 current)))
-       (and (= (nth 0 next-time) (nth 0 current))
-            (= (nth 1 next-time) (nth 1 current))
-            (> (nth 2 next-time) (nth 2 current)))
+    (or (time-less-p current next-time)
        (progn
          (timer-set-time timer (timer-next-integral-multiple-of-time
                                 current display-time-interval)
@@ -439,23 +434,16 @@ update which can wait for the next redisplay."
                ((and (stringp mail-spool-file)
                      (or (null display-time-server-down-time)
                          ;; If have been down for 20 min, try again.
-                         (> (- (nth 1 now) display-time-server-down-time)
-                            1200)
-                         (and (< (nth 1 now) display-time-server-down-time)
-                              (> (- (nth 1 now)
-                                    display-time-server-down-time)
-                                 -64336))))
-                (let ((start-time (current-time)))
+                         (< 1200 (- (float-time now)
+                                    display-time-server-down-time))))
+                (let ((start-time (float-time)))
                   (prog1
                       (display-time-file-nonempty-p mail-spool-file)
-                    (if (> (- (nth 1 (current-time))
-                              (nth 1 start-time))
-                           20)
-                        ;; Record that mail file is not accessible.
-                        (setq display-time-server-down-time
-                              (nth 1 (current-time)))
-                      ;; Record that mail file is accessible.
-                      (setq display-time-server-down-time nil)))))))
+                    ;; Record whether mail file is accessible.
+                    (setq display-time-server-down-time
+                          (let ((end-time (float-time)))
+                            (and (< 20 (- end-time start-time))
+                                 end-time))))))))
          (24-hours (substring time 11 13))
          (hour (string-to-number 24-hours))
          (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
index 67e701ecb1661f65ceee1dc769ca03133eaea205..401baece8384c08ee8450c15b90bf53334c8bbff 100644 (file)
@@ -192,7 +192,9 @@ key cache `url-digest-auth-storage'."
 (defun url-digest-auth-make-cnonce ()
   "Compute a new unique client nonce value."
   (base64-encode-string
-   (apply 'format "%016x%04x%04x%05x%05x" (random) (current-time)) t))
+   (apply #'format "%016x%08x%08x" (random)
+         (read (format-time-string "(%s %N)")))
+   t))
 
 (defun url-digest-auth-nonce-count (_nonce)
   "The number requests sent to server with the given NONCE.
index 9689997f793dc3fef3eee0124ef3c42a8e13948b..ca71ff71b7ad737841572758f0c7b4ef0ed664b5 100644 (file)
                  '(42 35 19 22 2 2016 1 nil -28800)))
   (should (equal (parse-time-string "Friday, 21 Sep 2018 13:47:58 PDT")
                  '(58 47 13 21 9 2018 5 t -25200)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54-0200")
-                 '(13818 33666)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54-0230")
-                 '(13818 35466)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54-02:00")
-                 '(13818 33666)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54-02")
-                 '(13818 33666)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54+0230")
-                 '(13818 17466)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54+02")
-                 '(13818 19266)))
-  (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54Z")
-                 '(13818 26466)))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54-0200") t)
+                "1998-09-12 14:21:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54-0230") t)
+                "1998-09-12 14:51:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54-02:00") t)
+                "1998-09-12 14:21:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54-02") t)
+                "1998-09-12 14:21:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54+0230") t)
+                "1998-09-12 09:51:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54+02") t)
+                "1998-09-12 10:21:54"))
+  (should (equal (format-time-string
+                 "%Y-%m-%d %H:%M:%S"
+                 (parse-iso8601-time-string "1998-09-12T12:21:54Z") t)
+                "1998-09-12 12:21:54"))
   (should (equal (parse-iso8601-time-string "1998-09-12T12:21:54")
                  (encode-time 54 21 12 12 9 1998))))
 
index fa92c1b64aa16982b4680f41f5a424c61fa1d0d5..1d3ba757f63853e5624bc030dff1faf5ee8a8837 100644 (file)
       (should (debug-timer-check)) t))
 
 (ert-deftest timer-test-multiple-of-time ()
-  (should (equal
-           (timer-next-integral-multiple-of-time '(0 0 0 1) (1+ (ash 1 53)))
-           (list (ash 1 (- 53 16)) 1 0 0))))
+  (should (zerop
+          (float-time
+           (time-subtract
+            (timer-next-integral-multiple-of-time '(0 0 0 1) (1+ (ash 1 53)))
+            (list (ash 1 (- 53 16)) 1))))))
 
 ;;; timer-tests.el ends here
index 55884f30a7e671c15531d7653d21e8969ca3122e..79013558fdbb1449b955b66617a16efc57264175 100644 (file)
@@ -2882,16 +2882,16 @@ This tests also `file-readable-p', `file-regular-p' and
            ;; able to return the date correctly.  They say "don't know".
            (dolist (elt attr)
              (unless
-                 (equal
-                  (nth
-                   5 (file-attributes (expand-file-name (car elt) tmp-name2)))
-                  '(0 0))
+                 (zerop
+                  (float-time
+                   (nth 5 (file-attributes
+                           (expand-file-name (car elt) tmp-name2)))))
                (should
                 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
                        (cdr elt)))))
            (setq attr (directory-files-and-attributes tmp-name2 'full))
            (dolist (elt attr)
-             (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
+             (unless (zerop (float-time (nth 5 (file-attributes (car elt)))))
                (should
                 (equal (file-attributes (car elt)) (cdr elt)))))
            (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
@@ -3215,14 +3215,14 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
            (write-region "foo" nil tmp-name1)
            (should (file-exists-p tmp-name1))
            (should (consp (nth 5 (file-attributes tmp-name1))))
-           ;; '(0 0) means don't know, and will be replaced by
-           ;; `current-time'.  Therefore, we use '(0 1).  We skip the
+           ;; A zero timestamp means don't know, and will be replaced by
+           ;; `current-time'.  Therefore, use timestamp 1.  Skip the
            ;; test, if the remote handler is not able to set the
            ;; correct time.
            (skip-unless (set-file-times tmp-name1 (seconds-to-time 1)))
            ;; Dumb remote shells without perl(1) or stat(1) are not
            ;; able to return the date correctly.  They say "don't know".
-           (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
+           (unless (zerop (float-time (nth 5 (file-attributes tmp-name1))))
              (should
               (equal (nth 5 (file-attributes tmp-name1)) (seconds-to-time 1)))
              (write-region "bla" nil tmp-name2)
@@ -3250,9 +3250,9 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
            (with-temp-buffer
              (insert-file-contents tmp-name)
              (should (verify-visited-file-modtime))
-             (set-visited-file-modtime '(0 1))
+              (set-visited-file-modtime (seconds-to-time 1))
              (should (verify-visited-file-modtime))
-             (should (equal (visited-file-modtime) '(0 1 0 0)))))
+             (should (= 1 (float-time (visited-file-modtime))))))
 
        ;; Cleanup.
        (ignore-errors (delete-file tmp-name))))))