emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 1f3e2ac 1/2: Add new function decoded-time-period


From: Lars Ingebrigtsen
Subject: master 1f3e2ac 1/2: Add new function decoded-time-period
Date: Wed, 5 Aug 2020 04:27:57 -0400 (EDT)

branch: master
commit 1f3e2ac4b62e38af2d9424f2a4fcc1515a4c0b30
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add new function decoded-time-period
    
    * lisp/calendar/time-date.el (decoded-time-period): New function.
---
 etc/NEWS                              |  5 +++++
 lisp/calendar/time-date.el            | 15 +++++++++++++++
 test/lisp/calendar/time-date-tests.el | 14 ++++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index f135b3f..c187916 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -731,6 +731,11 @@ optional argument specifying whether to follow symbolic 
links.
 ** 'parse-time-string' can now parse ISO 8601 format strings,
 such as "2020-01-15T16:12:21-08:00".
 
+---
+** The new function 'decoded-time-period' has been added.
+It interprets a decoded time structure as a period and returns the
+equivalent period in seconds.
+
 +++
 ** The new function 'dom-remove-attribute' has been added.
 
diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el
index eeb0992..125f9ac 100644
--- a/lisp/calendar/time-date.el
+++ b/lisp/calendar/time-date.el
@@ -527,6 +527,21 @@ TIME is modified and returned."
 
   time)
 
+(defun decoded-time-period (time)
+  "Interpret DECODED as a period and return its length in seconds.
+For computational purposes, years are 365 days long and months
+are 30 days long."
+  (+ (if (consp (decoded-time-second time))
+         ;; Fractional second.
+         (/ (float (car (decoded-time-second time)))
+            (cdr (decoded-time-second time)))
+       (or (decoded-time-second time) 0))
+     (* (or (decoded-time-minute time) 0) 60)
+     (* (or (decoded-time-hour time) 0) 60 60)
+     (* (or (decoded-time-day time) 0) 60 60 24)
+     (* (or (decoded-time-month time) 0) 60 60 24 30)
+     (* (or (decoded-time-year time) 0) 60 60 24 365)))
+
 (provide 'time-date)
 
 ;;; time-date.el ends here
diff --git a/test/lisp/calendar/time-date-tests.el 
b/test/lisp/calendar/time-date-tests.el
index 3eecc67..fe1460c 100644
--- a/test/lisp/calendar/time-date-tests.el
+++ b/test/lisp/calendar/time-date-tests.el
@@ -109,4 +109,18 @@
 (ert-deftest test-time-since ()
   (should (time-equal-p 0 (time-since nil))))
 
+(ert-deftest test-time-decoded-period ()
+  (should (equal (decoded-time-period '(nil nil 1 nil nil nil nil nil nil))
+                 3600))
+
+  (should (equal (decoded-time-period '(1 0 0 0 0 0 nil nil nil)) 1))
+  (should (equal (decoded-time-period '(0 1 0 0 0 0 nil nil nil)) 60))
+  (should (equal (decoded-time-period '(0 0 1 0 0 0 nil nil nil)) 3600))
+  (should (equal (decoded-time-period '(0 0 0 1 0 0 nil nil nil)) 86400))
+  (should (equal (decoded-time-period '(0 0 0 0 1 0 nil nil nil)) 2592000))
+  (should (equal (decoded-time-period '(0 0 0 0 0 1 nil nil nil)) 31536000))
+
+  (should (equal (decoded-time-period '((135 . 10) 0 0 0 0 0 nil nil nil))
+                 13.5)))
+
 ;;; time-date-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]