emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] scratch/iso8601 186b469 3/3: Add interval tests


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] scratch/iso8601 186b469 3/3: Add interval tests
Date: Mon, 8 Jul 2019 09:49:40 -0400 (EDT)

branch: scratch/iso8601
commit 186b4695c6fe7d93e0af88259d9b8e450c8d8bf0
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    Add interval tests
---
 lisp/calendar/iso8601.el            | 20 +++++++++++++++++---
 test/lisp/calendar/iso8601-tests.el | 20 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/lisp/calendar/iso8601.el b/lisp/calendar/iso8601.el
index c8a1579..79e1403 100644
--- a/lisp/calendar/iso8601.el
+++ b/lisp/calendar/iso8601.el
@@ -243,6 +243,8 @@ Return the number of minutes."
         start end duration)
     (if (not (= (length bits) 2))
         (signal 'wrong-type-argument string)
+      ;; The intervals may be an explicit start/end times, or either a
+      ;; start or an end, and an accompanying duration.
       (cond
        ((and (string-match "\\`P" (car bits))
              (iso8601-valid-p (cadr bits)))
@@ -254,11 +256,23 @@ Return the number of minutes."
               start (iso8601-parse (car bits))))
        ((and (iso8601-valid-p (car bits))
              (iso8601-valid-p (cadr bits)))
-        (setq start (encode-time (iso8601-parse (car bits)))
-              end (encode-time (iso8601-parse (cadr bits)))))
+        (setq start (iso8601-parse (car bits))
+              end (iso8601-parse (cadr bits))))
        (t
         (signal 'wrong-type-argument string))))
-    (list start end)))
+    (unless end
+      (setq end (decode-time (time-add (encode-time start)
+                                       (encode-time duration))
+                             (decoded-time-zone start))))
+    (unless start
+      (setq start (decode-time (time-subtract (encode-time end)
+                                              (encode-time duration))
+                               (decoded-time-zone end))))
+    (list start end
+          (or duration
+              (decode-time (time-subtract (encode-time end)
+                                          (encode-time start))
+                           (decoded-time-zone end))))))
 
 (defun iso8601--match (regexp string)
   (string-match (concat "\\`" regexp "\\'") string))
diff --git a/test/lisp/calendar/iso8601-tests.el 
b/test/lisp/calendar/iso8601-tests.el
index f9ea6e8..b33f38f 100644
--- a/test/lisp/calendar/iso8601-tests.el
+++ b/test/lisp/calendar/iso8601-tests.el
@@ -91,4 +91,24 @@
   (should-not (iso8601-valid-p "2008-03-02 T 13:47:30-01 "))
   (should-not (iso8601-valid-p "20008-03-02T13:47:30-01")))
 
+(ert-deftest test-iso8601-intervals ()
+  (should (equal
+           (iso8601-parse-interval "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z")
+           '((0 0 13 1 3 2007 nil nil 0)
+             (0 30 15 11 5 2008 nil nil 0)
+             ;; Hm...  can't really use decode-time for time differences...
+             (0 30 2 14 3 1971 0 nil 0))))
+  (should
+   (equal (iso8601-parse-interval "2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
+          '((0 0 13 1 3 2007 nil nil 0)
+            ;; Well, that's wrong...
+            (0 47 14 10 4 38 6 nil 0)
+            (0 30 2 10 2 1 nil nil nil))))
+  (should (equal (iso8601-parse-interval "P1Y2M10DT2H30M/2008-05-11T15:30:00Z")
+                 ;; I think I have to add decoded-time math functions,
+                 ;; really.
+                 '((0 43 13 1 4 3977 5 nil 0)
+                   (0 30 15 11 5 2008 nil nil 0)
+                   (0 30 2 10 2 1 nil nil nil)))))
+
 ;;; iso8601-tests.el ends here



reply via email to

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