Commit a00144ab authored by Lars Ingebrigtsen's avatar Lars Ingebrigtsen

Change the interface a bit -- leave unspecified as nil

parent 53f7e58a
......@@ -148,7 +148,7 @@ well as variants like \"2008W32\" (week number) and
(string-to-number (match-string 4 string))))
(jan-start (decoded-time-weekday
(decode-time
(encode-time
(iso8601--encode-time
(iso8601--decoded-time :year year
:month 1
:day 4)))))
......@@ -291,14 +291,14 @@ Return the number of minutes."
duration))))
(list start end
(or duration
(decode-time (time-subtract (encode-time end)
(encode-time start))
(decode-time (time-subtract (iso8601--encode-time end)
(iso8601--encode-time start))
(decoded-time-zone end))))))
(defun iso8601--match (regexp string)
(string-match (concat "\\`" regexp "\\'") string))
(defun iso8601--value (elem default)
(defun iso8601--value (elem &optional default)
(if (stringp elem)
(string-to-number elem)
(or elem default)))
......@@ -306,16 +306,34 @@ Return the number of minutes."
(cl-defun iso8601--decoded-time (&key second minute hour
day month year
dst zone)
(list (iso8601--value second 0)
(iso8601--value minute 0)
(iso8601--value hour 0)
(iso8601--value day 1)
(iso8601--value month 1)
(iso8601--value year 0)
(list (iso8601--value second)
(iso8601--value minute)
(iso8601--value hour)
(iso8601--value day)
(iso8601--value month)
(iso8601--value year)
nil
dst
zone))
(defun iso8601--encode-time (time)
"Like `encode-time', but fill in nil values in TIME."
(setq time (copy-sequence time))
(unless (decoded-time-second time)
(setf (decoded-time-second time) 0))
(unless (decoded-time-minute time)
(setf (decoded-time-minute time) 0))
(unless (decoded-time-hour time)
(setf (decoded-time-hour time) 0))
(unless (decoded-time-day time)
(setf (decoded-time-day time) 1))
(unless (decoded-time-month time)
(setf (decoded-time-month time) 1))
(unless (decoded-time-year time)
(setf (decoded-time-year time) 0))
(encode-time time))
(provide 'iso8601)
;;; iso8601.el ends here
......@@ -24,45 +24,45 @@
(ert-deftest test-iso8601-date-years ()
(should (equal (iso8601-parse-date "1985")
'(0 0 0 1 1 1985 nil nil nil)))
'(nil nil nil nil nil 1985 nil nil nil)))
(should (equal (iso8601-parse-date "-0003")
'(0 0 0 1 1 -4 nil nil nil)))
'(nil nil nil nil nil -4 nil nil nil)))
(should (equal (iso8601-parse-date "+1985")
'(0 0 0 1 1 1985 nil nil nil))))
'(nil nil nil nil nil 1985 nil nil nil))))
(ert-deftest test-iso8601-date-dates ()
(should (equal (iso8601-parse-date "1985-03-14")
'(0 0 0 14 3 1985 nil nil nil)))
'(nil nil nil 14 3 1985 nil nil nil)))
(should (equal (iso8601-parse-date "19850314")
'(0 0 0 14 3 1985 nil nil nil)))
'(nil nil nil 14 3 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-02")
'(0 0 0 1 2 1985 nil nil nil))))
'(nil nil nil nil 2 1985 nil nil nil))))
(ert-deftest test-iso8601-date-obsolete ()
(should (equal (iso8601-parse-date "--02-01")
'(0 0 0 1 2 0 nil nil nil)))
'(nil nil nil 1 2 nil nil nil nil)))
(should (equal (iso8601-parse-date "--0201")
'(0 0 0 1 2 0 nil nil nil))))
'(nil nil nil 1 2 nil nil nil nil))))
(ert-deftest test-iso8601-date-weeks ()
(should (equal (iso8601-parse-date "2008W39-6")
'(0 0 0 27 9 2008 nil nil nil)))
'(nil nil nil 27 9 2008 nil nil nil)))
(should (equal (iso8601-parse-date "2009W01-1")
'(0 0 0 29 12 2008 nil nil nil)))
'(nil nil nil 29 12 2008 nil nil nil)))
(should (equal (iso8601-parse-date "2009W53-7")
'(0 0 0 3 1 2010 nil nil nil))))
'(nil nil nil 3 1 2010 nil nil nil))))
(ert-deftest test-iso8601-date-ordinals ()
(should (equal (iso8601-parse-date "1981-095")
'(0 0 0 5 4 1981 nil nil nil))))
'(nil nil nil 5 4 1981 nil nil nil))))
(ert-deftest test-iso8601-time ()
(should (equal (iso8601-parse-time "13:47:30")
'(30 47 13 1 1 0 nil nil nil)))
'(30 47 13 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "134730")
'(30 47 13 1 1 0 nil nil nil)))
'(30 47 13 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "1347")
'(0 47 13 1 1 0 nil nil nil))))
'(0 47 13 nil nil nil nil nil nil))))
(ert-deftest test-iso8601-combined ()
(should (equal (iso8601-parse "2008-03-02T13:47:30")
......@@ -109,37 +109,48 @@
(ert-deftest standard-test-dates ()
(should (equal (iso8601-parse-date "19850412")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-04-12")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985102")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-102")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985W155")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-W15-5")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985W15")
'(0 0 0 7 4 1985 nil nil nil)))
'(nil nil nil 7 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-W15")
'(0 0 0 7 4 1985 nil nil nil)))
'(nil nil nil 7 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985-04")
'(0 0 0 1 4 1985 nil nil nil)))
'(nil nil nil nil 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "1985")
'(0 0 0 1 1 1985 nil nil nil)))
'(nil nil nil nil nil 1985 nil nil nil)))
(should (equal (iso8601-parse-date "+1985-04-12")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil)))
(should (equal (iso8601-parse-date "+19850412")
'(0 0 0 12 4 1985 nil nil nil)))
'(nil nil nil 12 4 1985 nil nil nil))))
)
(ert-deftest standard-test-time-of-day ()
(should (equal (iso8601-parse-time "152746")
'(46 27 15 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "15:27:46")
'(46 27 15 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "1528")
'(0 28 15 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "15:28")
'(0 28 15 nil nil nil nil nil nil)))
(should (equal (iso8601-parse-time "15")
'(0 0 15 nil nil nil nil nil nil))))
;;; iso8601-tests.el ends here
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment