Simplify use of current-time and friends
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 29 Oct 2014 02:19:38 +0000 (02:19 +0000)
committerKatsumi Yamaoka <yamaoka@jpl.org>
Wed, 29 Oct 2014 02:19:38 +0000 (02:19 +0000)
* gnus-delay.el (gnus-delay-article):
* gnus-sum.el (gnus-summary-read-document):
* gnus-util.el (gnus-seconds-today, gnus-seconds-month):
* message.el (message-make-expires-date):
Omit unnecessary call to current-time.
* gnus-util.el (gnus-float-time): Simplify to an alias because
time-to-seconds now behaves like float-time with respect to nil arg.
(gnus-seconds-year): Don't call current-time twice to get the current
time stamp, as this can lead to inconsistent results.
* time-date.el (time-to-seconds) [!float-time]:
Use current time if arg is nil, to be compatible with float-time.
(time-date--day-in-year): New function, with most of the guts of
the old time-to-day-in-year.
(time-to-day-in-year): Use it.
(time-to-days): Use it, to avoid decoding the same time stamp twice.

lisp/ChangeLog
lisp/gnus-delay.el
lisp/gnus-sum.el
lisp/gnus-util.el
lisp/message.el
lisp/time-date.el

index ad98a15..250d2e8 100644 (file)
@@ -1,3 +1,22 @@
+2014-10-29  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Simplify use of current-time and friends.
+       * gnus-delay.el (gnus-delay-article):
+       * gnus-sum.el (gnus-summary-read-document):
+       * gnus-util.el (gnus-seconds-today, gnus-seconds-month):
+       * message.el (message-make-expires-date):
+       Omit unnecessary call to current-time.
+       * gnus-util.el (gnus-float-time): Simplify to an alias because
+       time-to-seconds now behaves like float-time with respect to nil arg.
+       (gnus-seconds-year): Don't call current-time twice to get the current
+       time stamp, as this can lead to inconsistent results.
+       * time-date.el (time-to-seconds) [!float-time]:
+       Use current time if arg is nil, to be compatible with float-time.
+       (time-date--day-in-year): New function, with most of the guts of
+       the old time-to-day-in-year.
+       (time-to-day-in-year): Use it.
+       (time-to-days): Use it, to avoid decoding the same time stamp twice.
+
 2014-10-27  Katsumi Yamaoka  <yamaoka@jpl.org>
 
        * gnus.el (gnus-mode-line-buffer-identification):
index 75b967e..2a286da 100644 (file)
@@ -98,7 +98,7 @@ DELAY is a string, giving the length of the time.  Possible values are:
           (setq hour   (string-to-number (match-string 1 delay))
                 minute (string-to-number (match-string 2 delay)))
           ;; Use current time, except...
-          (setq deadline (apply 'vector (decode-time (current-time))))
+          (setq deadline (apply 'vector (decode-time)))
           ;; ... for minute and hour.
           (aset deadline 1 minute)
           (aset deadline 2 hour)
index db0242e..55a263d 100644 (file)
@@ -9333,7 +9333,7 @@ Obeys the standard process/prefix convention."
      ((gnus-group-read-ephemeral-group
        (setq vgroup (format
                     "nnvirtual:%s-%s" gnus-newsgroup-name
-                    (format-time-string "%Y%m%dT%H%M%S" (current-time))))
+                    (format-time-string "%Y%m%dT%H%M%S")))
        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
        t
        (cons (current-buffer) 'summary)))
index fe4d707..15f3aed 100644 (file)
@@ -313,14 +313,12 @@ Symbols are also allowed; their print names are used instead."
 
 ;; Every version of Emacs Gnus supports has built-in float-time.
 ;; The featurep test silences an irritating compiler warning.
-(eval-and-compile
+(defalias 'gnus-float-time
   (if (or (featurep 'emacs)
          (fboundp 'float-time))
-      (defalias 'gnus-float-time 'float-time)
-    (defun gnus-float-time (&optional time)
-      "Convert time value TIME to a floating point number.
-TIME defaults to the current time."
-      (time-to-seconds (or time (current-time))))))
+      'float-time 'time-to-seconds)
+  "Convert time value TIME to a floating point number.
+TIME defaults to the current time.")
 
 ;;; Keymap macros.
 
@@ -389,19 +387,20 @@ TIME defaults to the current time."
 
 (defun gnus-seconds-today ()
   "Return the number of seconds passed today."
-  (let ((now (decode-time (current-time))))
+  (let ((now (decode-time)))
     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600))))
 
 (defun gnus-seconds-month ()
   "Return the number of seconds passed this month."
-  (let ((now (decode-time (current-time))))
+  (let ((now (decode-time)))
     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
        (* (- (car (nthcdr 3 now)) 1) 3600 24))))
 
 (defun gnus-seconds-year ()
   "Return the number of seconds passed this year."
-  (let ((now (decode-time (current-time)))
-       (days (format-time-string "%j" (current-time))))
+  (let* ((current (current-time))
+        (now (decode-time current))
+        (days (format-time-string "%j" current)))
     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
        (* (- (string-to-number days) 1) 3600 24))))
 
index 5ba42b1..59d22ef 100644 (file)
@@ -5594,7 +5594,7 @@ If NOW, use that time instead."
   "Make date string for the Expires header.  Expiry in DAYS days.
 
 In posting styles use `(\"Expires\" (make-expires-date 30))'."
-  (let* ((cur (decode-time (current-time)))
+  (let* ((cur (decode-time))
         (nday (+ days (nth 3 cur))))
     (setf (nth 3 cur) nday)
     (message-make-date (apply 'encode-time cur))))
index 48fe229..82bc05f 100644 (file)
@@ -44,7 +44,7 @@ The value of the last form in BODY is returned.
 
 Each element of the list VARLIST is a list of the form
 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [PICO-SYMBOL [TYPE-SYMBOL]] TIME-VALUE).
-The time value TIME-VALUE is decoded and the result it bound to
+The time value TIME-VALUE is decoded and the result is bound to
 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
 The optional PICO-SYMBOL is bound to the picoseconds part.
 
@@ -147,10 +147,12 @@ If DATE lacks timezone information, GMT is assumed."
   (or (featurep 'emacs)
       (and (fboundp 'float-time)
            (subrp (symbol-function 'float-time)))
-      (defun time-to-seconds (time)
-        "Convert time value TIME to a floating point number."
-        (with-decoded-time-value ((high low micro pico type time))
-          (+ (* 1.0 high 65536)
+      (defun time-to-seconds (&optional time)
+        "Convert optional value TIME to a floating point number.
+TIME defaults to the current time."
+        (with-decoded-time-value ((high low micro pico type
+                                  (or time (current-time))))
+          (+ (* high 65536.0)
              low
             (/ (+ (* micro 1e6) pico) 1e12))))))
 
@@ -272,11 +274,9 @@ DATE1 and DATE2 should be date-time strings."
           (not (zerop (% year 100))))
       (zerop (% year 400))))
 
-;;;###autoload
-(defun time-to-day-in-year (time)
-  "Return the day number within the year corresponding to TIME."
-  (let* ((tim (decode-time time))
-        (month (nth 4 tim))
+(defun time-date--day-in-year (tim)
+  "Return the day number within the year corresponding to the decoded time TIM."
+  (let* ((month (nth 4 tim))
         (day (nth 3 tim))
         (year (nth 5 tim))
         (day-of-year (+ day (* 31 (1- month)))))
@@ -286,6 +286,11 @@ DATE1 and DATE2 should be date-time strings."
        (setq day-of-year (1+ day-of-year))))
     day-of-year))
 
+;;;###autoload
+(defun time-to-day-in-year (time)
+  "Return the day number within the year corresponding to TIME."
+  (time-date--day-in-year (decode-time time)))
+
 ;;;###autoload
 (defun time-to-days (time)
   "The number of days between the Gregorian date 0001-12-31bce and TIME.
@@ -293,7 +298,7 @@ TIME should be a time value.
 The Gregorian date Sunday, December 31, 1bce is imaginary."
   (let* ((tim (decode-time time))
         (year (nth 5 tim)))
-    (+ (time-to-day-in-year time)      ;       Days this year
+    (+ (time-date--day-in-year tim)    ;       Days this year
        (* 365 (1- year))               ;       + Days in prior years
        (/ (1- year) 4)                 ;       + Julian leap years
        (- (/ (1- year) 100))           ;       - century years