summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2019-07-08 15:49:29 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2019-07-08 15:49:29 +0200
commit186b4695c6fe7d93e0af88259d9b8e450c8d8bf0 (patch)
tree9a90cf6f5e3d65d4f3f2d8ac25a88f1e2eb6bc50
parentf0da4f03be7ced01aa80102192e12ed5153214ee (diff)
downloademacs-186b4695c6fe7d93e0af88259d9b8e450c8d8bf0.tar.gz
Add interval tests
-rw-r--r--lisp/calendar/iso8601.el20
-rw-r--r--test/lisp/calendar/iso8601-tests.el20
2 files changed, 37 insertions, 3 deletions
diff --git a/lisp/calendar/iso8601.el b/lisp/calendar/iso8601.el
index c8a15794f6b..79e1403802a 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 f9ea6e886b1..b33f38f03fd 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