branch: elpa/datetime commit 93a6b2058600d46ca940d43bc81cac3c7bd13e8b Author: Paul Pogonyshev <pogonys...@gmail.com> Commit: Paul Pogonyshev <pogonys...@gmail.com>
Fix parsing of eras when those are specified multiple times in the pattern for whatever reason. --- datetime.el | 13 +++++++++---- test/parse.el | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/datetime.el b/datetime.el index 71bb8028ad..d87e97cae6 100644 --- a/datetime.el +++ b/datetime.el @@ -1315,8 +1315,9 @@ unless specified otherwise. (constant-year (unless year-computation (or (plist-get 'year defaults) 1970))) (era-correction (when (and year-computation era-part-indices) - (datetime--parser-computation pattern "era" validating nil nil - (era-part-indices (datetime--parser-era-correction locale nil downcased) t)))) + (let ((only-one-part (null (cdr era-part-indices)))) + (datetime--parser-computation pattern "era" validating nil nil + (era-part-indices (datetime--parser-era-correction locale nil downcased (not only-one-part)) t))))) (month-computation (or (datetime--parser-computation pattern "month" validating 0 11 (month-number-part-indices (datetime--parser-int-computation t)) (month-name-part-indices (datetime--parser-string-index-computation locale nil downcased) t)) @@ -1408,8 +1409,12 @@ unless specified otherwise. (`always-two-digits `(+ ,(datetime--parser-int-computation (car argument)) 2000)) (_ (datetime--parser-int-computation (car argument))))) -(defun datetime--parser-era-correction (argument locale field downcased) - (datetime--parser-string-if-computation argument locale (or field (cdr argument)) downcased `(setq year (- 1 year)) nil)) +(defun datetime--parser-era-correction (argument locale field downcased can-be-called-multiple-times) + (datetime--parser-string-if-computation argument locale (or field (cdr argument)) downcased + (if can-be-called-multiple-times + `(if (>= year 0) (setq year (- 1 year)) year) + `(setq year (- 1 year))) + nil)) (defun datetime--parser-hour-1-24-computation (argument) `(let ((hour-1-24 ,(datetime--parser-int-computation argument))) diff --git a/test/parse.el b/test/parse.el index 86522af344..f8d1825da5 100644 --- a/test/parse.el +++ b/test/parse.el @@ -181,4 +181,15 @@ (funcall datetime--test-formatter -3000000000))))))))) +(ert-deftest datetime-parsing-era-specified-several-times () + (datetime--test-set-up-parser 'UTC 'en "yyyy G+G" + (datetime--test-parser (list "2000 AD+AD")) + ;; Used to die with "inconsistent era", even though it is consistent. Used to invert + ;; year two times, resulting in wrong return value. + (datetime--test-parser (list "2000 BC+BC"))) + (datetime--test-set-up-parser 'UTC 'en "yyyy G / GGGG / GGGGG" + (datetime--test-parser (list "2000 AD / Anno Domini / A")) + (datetime--test-parser (list "2000 BC / Before Christ / B")))) + + (provide 'test/parse)