From 6670478ef5807d39b3e4bc286b58b79679b8cb4d Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Mon, 8 Jul 2019 03:30:08 +0200 Subject: Added tests fos is8601 --- lisp/calendar/iso8601.el | 119 +++++------------------------------- lisp/calendar/time-date.el | 2 +- test/lisp/calendar/iso8601-tests.el | 81 ++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 106 deletions(-) create mode 100644 test/lisp/calendar/iso8601-tests.el diff --git a/lisp/calendar/iso8601.el b/lisp/calendar/iso8601.el index 45004c46b4e..dd2fdf83806 100644 --- a/lisp/calendar/iso8601.el +++ b/lisp/calendar/iso8601.el @@ -25,6 +25,8 @@ ;;; Code: +(require 'time-date) + (cl-defmethod make-decoded-time (&key second minute hour day month year dst zone) @@ -87,24 +89,27 @@ ;; Monday 29 December 2008 is written "2009-W01-1". ((< ordinal 1) (setq year (1- year) - ordinal (+ ordinal (time-days-in-year year)))) + ordinal (+ ordinal (if (date-leap-year-p year) + 366 365)))) ;; Sunday 3 January 2010 is written "2009-W53-7". - ((> ordinal (iso8601-days-in-year year)) - (setq ordinal (- ordinal (time-days-in-year year)) + ((> ordinal (if (date-leap-year-p year) + 366 365)) + (setq ordinal (- ordinal (if (date-leap-year-p year) + 366 365)) year (1+ year)))) - (let ((month-day (time-ordinal-to-date year ordinal))) + (let ((month-day (date-ordinal-to-time year ordinal))) (make-decoded-time :year year - :month (car month-day) - :day (cdr month-day))))) + :month (decoded-time-month month-day) + :day (decoded-time-day month-day))))) ;; Ordinal dates: YYYY-DDD ((string-match "\\`\\([0-9][0-9][0-9][0-9]\\)-?\\([0-9][0-9][0-9]\\)\\'" string) (let* ((year (string-to-number (match-string 1 string))) (ordinal (string-to-number (match-string 2 string))) - (month-day (iso8601-ordinal-to-date year ordinal))) + (month-day (date-ordinal-to-time year ordinal))) (make-decoded-time :year year - :month (car month-day) - :day (cdr month-day)))))) + :month (decoded-time-month month-day) + :day (decoded-time-day month-day)))))) (defun iso8601-parse-time (string) "Parse STRING, which should be an ISO 8601 time string, and return a time value." @@ -204,102 +209,6 @@ Return the number of minutes." end (encode-time (iso8601-parse (cadr bits))))))) (list start end))) - -;; (format-time-string "%FT%T" (iso8601-parse-date "1985")) -;; "1985-01-01T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "-0003")) -;; "0002-01-01T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "+1985")) -;; "1985-01-01T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "1985-03-14")) -;; "1985-03-14T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "19850314")) -;; "1985-03-14T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "1985-02")) -;; "1985-02-01T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "--02-01")) -;; "0000-02-01T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "--0201")) -;; "0000-02-01T00:00:00" - -;; (time-days-in-year 1999) -;; 365 - -;; (time-days-in-year 1900) -;; 366 - -;; (time-days-in-year 1996) -;; 366 - -;; (time-days-in-year 2000) -;; 365 - -;; (time-days-in-month 2001 5) -;; 31 - -;; (time-days-in-month 2004 2) -;; 29 - -;; (time-days-in-month 2001 11) -;; 30 - -;; (time-ordinal-to-date 2008 271) -;; (0 0 0 27 9 2008 nil nil nil) - -;; (time-ordinal-to-date 2008 1) -;; (0 0 0 1 1 2008 nil nil nil) - -;; (time-ordinal-to-date 2008 32) -;; (0 0 0 1 2 2008 nil nil nil) - -;; (time-ordinal-to-date 1981 095) -;; (0 0 0 5 4 1981 nil nil nil) - -;; (format-time-string "%FT%T" (iso8601-parse-date "2008W39-6")) -;; "2008-09-27T01:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "2009W01-1")) -;; "2008-12-29T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "2009W53-7")) -;; "2010-01-03T00:00:00" - -;; (format-time-string "%FT%T" (iso8601-parse-date "1981-095")) -;; "1981-04-05T01:00:00" - -;; (format-time-string "%G-W%V-%u" (encode-time '(0 0 0 29 12 2008 nil nil nil))) -;; "2009-W01-1" - -;; (format-time-string "%FT%T" (iso8601-parse-date "2009W01-1")) -;; "2009-01-05T00:00:00" - -;; (format-time-string "%FT%T" (encode-time (iso8601-parse-time "13:47:30"))) -;; "0000-01-01T13:47:30" - -;; (format-time-string "%FT%T" (encode-time (iso8601-parse "2008-03-02T13:47:30"))) -;; "2008-03-02T13:47:30" - - -;; (iso8601-parse-duration "P3Y6M4DT12H30M5S") -;; (5 30 12 4 6 3 nil nil nil) - -;; (iso8601-parse-duration "P1M") -;; (0 0 0 0 1 0 nil nil nil) - -;; (iso8601-parse-duration "PT1M") -;; (0 1 0 0 0 0 nil nil nil) - -;; (iso8601-parse-duration "P0003-06-04T12:30:05") -(5 30 12 4 6 3 nil nil nil) - - (provide 'iso8601) ;;; iso8601.el ends here diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el index 2535e972473..6e268baecc6 100644 --- a/lisp/calendar/time-date.el +++ b/lisp/calendar/time-date.el @@ -359,7 +359,7 @@ is output until the first non-zero unit is encountered." 31 30))) -(defun time-ordinal-to-date (year ordinal) +(defun date-ordinal-to-time (year ordinal) "Convert a YEAR/ORDINAL to the equivalent `decoded-time' structure. ORDINAL is the number of days since the start of the year, with January 1st being 1." diff --git a/test/lisp/calendar/iso8601-tests.el b/test/lisp/calendar/iso8601-tests.el new file mode 100644 index 00000000000..20a6f9c9cf2 --- /dev/null +++ b/test/lisp/calendar/iso8601-tests.el @@ -0,0 +1,81 @@ +;;; iso8601-tests.el --- tests for calendar/iso8601.el -*- lexical-binding:t -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'ert) +(require 'iso8601) + +(ert-deftest test-iso8601-date-years () + (should (equal (iso8601-parse-date "1985") + '(0 0 0 1 1 1985 nil nil nil))) + (should (equal (iso8601-parse-date "-0003") + '(0 0 0 1 1 2 nil nil nil))) + (should (equal (iso8601-parse-date "+1985") + '(0 0 0 1 1 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))) + (should (equal (iso8601-parse-date "19850314") + '(0 0 0 14 3 1985 nil nil nil))) + (should (equal (iso8601-parse-date "1985-02") + '(0 0 0 1 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))) + (should (equal (iso8601-parse-date "--0201") + '(0 0 0 1 2 0 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))) + (should (equal (iso8601-parse-date "2009W01-1") + '(0 0 0 29 12 2008 nil nil nil))) + (should (equal (iso8601-parse-date "2009W53-7") + '(0 0 0 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)))) + +(ert-deftest test-iso8601-time () + (should (equal (iso8601-parse-time "13:47:30") + '(30 47 13 1 1 0 nil nil nil))) + (should (equal (iso8601-parse-time "134730") + '(30 47 13 1 1 0 nil nil nil))) + (should (equal (iso8601-parse-time "1347") + '(0 47 13 1 1 0 nil nil nil)))) + +(ert-deftest test-iso8601-combined () + (should (equal (iso8601-parse "2008-03-02T13:47:30") + '(30 47 13 2 3 2008 nil nil nil)))) + +(ert-deftest test-iso8601-duration () + (should (equal (iso8601-parse-duration "P3Y6M4DT12H30M5S") + '(5 30 12 4 6 3 nil nil nil))) + (should (equal (iso8601-parse-duration "P1M") + '(0 0 0 0 1 0 nil nil nil))) + (should (equal (iso8601-parse-duration "PT1M") + '(0 1 0 0 0 0 nil nil nil))) + (should (equal (iso8601-parse-duration "P0003-06-04T12:30:05") + '(5 30 12 4 6 3 nil nil nil)))) + +;;; iso8601-tests.el ends here -- cgit v1.2.1