From: Lars Ingebrigtsen Date: Wed, 5 Aug 2020 08:07:13 +0000 (+0200) Subject: Add new function decoded-time-period X-Git-Tag: emacs-28.0.90~6815 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1f3e2ac4b62e38af2d9424f2a4fcc1515a4c0b30;p=emacs.git Add new function decoded-time-period * lisp/calendar/time-date.el (decoded-time-period): New function. --- diff --git a/etc/NEWS b/etc/NEWS index f135b3f6b3c..c1879169dd6 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 eeb09926a6e..125f9acc705 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 3eecc67eb53..fe1460cf29e 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