]> git.eshelyaron.com Git - emacs.git/commitdiff
Minor fixes/simplifications to time functions
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 19 Dec 2018 20:57:25 +0000 (12:57 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 19 Dec 2018 21:01:42 +0000 (13:01 -0800)
* doc/lispintro/emacs-lisp-intro.texi (Files List): Simplify.
* doc/lispref/os.texi (Time of Day): Mention format-time-string
as an alternative to current-time-string.
* lisp/arc-mode.el (archive-unixdate, archive-unixtime):
Port better to future versions of Emacs where (COUNT . HZ)
will take precedence to (HI . LO).
* lisp/arc-mode.el (archive-unixtime):
* lisp/calendar/todo-mode.el (todo-insert-item--basic)
(todo-item-done, todo-read-time):
Prefer format-time-string to substringing current-time-string.
* lisp/calc/calc-forms.el (calc-time, calcFunc-now):
Prefer decode-time to parsing the output of current-time-string.
* lisp/emacs-lisp/cl-extra.el (cl--random-time):
Prefer encode-time to hashing the output of current-time-string.
* lisp/gnus/gnus-score.el (gnus-score-headers)
(gnus-score-adaptive):
Avoid stringifying and then reparsing timestamp.
* src/timefns.c (Fencode_time): Omit redundant assignment.

doc/lispintro/emacs-lisp-intro.texi
doc/lispref/os.texi
lisp/arc-mode.el
lisp/calc/calc-forms.el
lisp/calendar/todo-mode.el
lisp/emacs-lisp/cl-extra.el
lisp/gnus/gnus-score.el
src/timefns.c

index 0b0c0a167d983cc95e0f14cc819d9d3e4867ed3e..1a1518b2c5185b3e8c716551fffe3214139b69ef 100644 (file)
@@ -15599,7 +15599,7 @@ like this:
    (recursive-lengths-list-many-files
     (files-in-below-directory "/usr/local/src/emacs/lisp/"))
    '<)
-  (insert (format "%s" (current-time-string))))
+  (insert (current-time-string)))
 @end ignore
 
 @node Counting function definitions
index 41753859e503cf55b09ed98e846581556887d3c1..60697f2e29f16638029bb7687571ae6e178744a1 100644 (file)
@@ -1313,9 +1313,10 @@ This function returns the current time and date as a human-readable
 string.  The format does not vary for the initial part of the string,
 which contains the day of week, month, day of month, and time of day
 in that order: the number of characters used for these fields is
-always the same, so you can reliably
-use @code{substring} to extract them.  You should count
-characters from the beginning of the string rather than from the end,
+always the same, although (unless you require English weekday or
+month abbreviations regardless of locale) it is typically more
+convenient to use @code{format-time-string} than to extract
+fields from the output of @code{current-time-string},
 as the year might not have exactly four digits, and additional
 information may some day be added at the end.
 
index 068702bc71b37eaf9329415db967265a69c7f554..9d63a2a579a0dc9839b2be860fbc73959003b459 100644 (file)
@@ -637,7 +637,7 @@ the mode is invalid.  If ERROR is nil then nil will be returned."
 
 (defun archive-unixdate (low high)
   "Stringify Unix (LOW HIGH) date."
-  (let* ((time (cons high low))
+  (let* ((time (list high low))
         (str (current-time-string time)))
     (format "%s-%s-%s"
            (substring str 8 10)
@@ -646,8 +646,7 @@ the mode is invalid.  If ERROR is nil then nil will be returned."
 
 (defun archive-unixtime (low high)
   "Stringify Unix (LOW HIGH) time."
-  (let ((str (current-time-string (cons high low))))
-    (substring str 11 19)))
+  (format-time-string "%H:%M:%S" (list high low)))
 
 (defun archive-get-lineno ()
   (if (>= (point) archive-file-list-start)
index f7586288ca20c792007f7f0181f046ffd17a6100..ccd52d370d19b3d6f315c05206b3537f34ce1afc 100644 (file)
 (defun calc-time ()
   (interactive)
   (calc-wrapper
-   (let ((time (current-time-string)))
+   (let ((time (decode-time)))
      (calc-enter-result 0 "time"
                        (list 'mod
                              (list 'hms
-                                   (string-to-number (substring time 11 13))
-                                   (string-to-number (substring time 14 16))
-                                   (string-to-number (substring time 17 19)))
+                                   (nth 2 time) (nth 1 time) (nth 0 time))
                              (list 'hms 24 0 0))))))
 
 (defun calc-to-hms (arg)
@@ -1341,16 +1339,15 @@ as measured in the integer number of days before December 31, 1 BC (Gregorian)."
           (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second)))))
 
 (defun calcFunc-now (&optional zone)
-  (let ((date (let ((calc-date-format nil))
-               (math-parse-date (current-time-string)))))
-    (if (consp date)
-       (if zone
-           (math-add date (math-div (math-sub (calcFunc-tzone nil date)
-                                              (calcFunc-tzone zone date))
-                                    '(float 864 2)))
-         date)
-      (calc-record-why "*Unable to interpret current date from system")
-      (append (list 'calcFunc-now) (and zone (list zone))))))
+  (let ((date (let ((now (decode-time)))
+               (list 'date (math-dt-to-date
+                            (list (nth 5 now) (nth 4 now) (nth 3 now)
+                                  (nth 2 now) (nth 1 now) (nth 0 now)))))))
+    (if zone
+       (math-add date (math-div (math-sub (calcFunc-tzone nil date)
+                                          (calcFunc-tzone zone date))
+                                '(float 864 2)))
+      date)))
 
 (defun calcFunc-year (date)
   (car (math-date-to-dt date)))
index 41fe57e60ce896701e0a54f5deb128fc8e7d81c2..145cf78e6de4df65e8988d8d4137f4294ed2053b 100644 (file)
@@ -1931,7 +1931,7 @@ their associated keys and their effects."
                             (calendar-current-date) t t))))
             (time-string (or (and time (todo-read-time))
                              (and todo-always-add-time-string
-                                  (substring (current-time-string) 11 16)))))
+                                  (format-time-string "%H:%M")))))
        (setq todo-date-from-calendar nil)
        (find-file-noselect file 'nowarn)
        (set-window-buffer (selected-window)
@@ -2881,8 +2881,7 @@ visible."
              (not marked))
       (let* ((date-string (calendar-date-string (calendar-current-date) t t))
             (time-string (if todo-always-add-time-string
-                             (concat " " (substring (current-time-string)
-                                                    11 16))
+                             (format-time-string " %H:%M")
                            ""))
             (done-prefix (concat "[" todo-done-string date-string time-string
                                  "] "))
@@ -6091,7 +6090,7 @@ the empty string (i.e., no time string)."
     (while (not valid)
       (setq answer (read-string "Enter a clock time: " nil nil
                                (when todo-always-add-time-string
-                                 (substring (current-time-string) 11 16))))
+                                 (format-time-string "%H:%M"))))
       (when (or (string= "" answer)
                (string-match diary-time-regexp answer))
        (setq valid t)))
index 13988db9a8624fb0f2e73eb37b392ee3fed95d52..78484fcfa3f8c5be965c91c2d5ec1d2f75252abe 100644 (file)
@@ -438,9 +438,7 @@ as an integer unless JUNK-ALLOWED is non-nil."
 ;; Random numbers.
 
 (defun cl--random-time ()
-  (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
-    (while (>= (cl-decf i) 0) (setq v (+ (* v 3) (aref time i))))
-    v))
+  (car (encode-time nil t)))
 
 ;;;###autoload (autoload 'cl-random-state-p "cl-extra")
 (cl-defstruct (cl--random-state
index 327cc69392dea88fbdce1934b04f90a8b23d58e7..f777163cc3137ce5157056620ac787b561a64efe 100644 (file)
@@ -1501,7 +1501,7 @@ If FORMAT, also format the current score file."
       (when (and gnus-summary-default-score
                 scores)
        (let* ((entries gnus-header-index)
-              (now (date-to-day (current-time-string)))
+              (now (time-to-days (current-time)))
               (expire (and gnus-score-expiry-days
                            (- now gnus-score-expiry-days)))
               (headers gnus-newsgroup-headers)
@@ -2380,7 +2380,7 @@ score in `gnus-newsgroup-scored' by SCORE."
               (memq 'word gnus-newsgroup-adaptive))
       (with-temp-buffer
        (let* ((hashtb (gnus-make-hashtable 1000))
-              (date (date-to-day (current-time-string)))
+              (date (time-to-days (current-time)))
               (data gnus-newsgroup-data)
               word d score val)
          (with-syntax-table gnus-adaptive-word-syntax-table
index f527d5ed7fd2e7831336e8658edd0a54c78dd6da..58dda1c70619fbe5737c661e7f2bec1db2d73a62 100644 (file)
@@ -1475,7 +1475,6 @@ usage: (encode-time &optional TIME FORM &rest OBSOLESCENT-ARGUMENTS)  */)
     {
       if (6 < nargs)
        zone = args[nargs - 1];
-      form = Qnil;
       tm.tm_sec  = check_tm_member (a, 0);
       tm.tm_min  = check_tm_member (args[1], 0);
       tm.tm_hour = check_tm_member (args[2], 0);