From 3d02a8e1192a782a16ffdee4940612f69a12629f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 18 Apr 2022 13:08:26 -0700 Subject: [PATCH 1/6] Support (encode-time (list s m h D M Y)) * src/timefns.c (Fencode_time): Add support for a 6-elt list arg. Requested by Max Nikulin for Org (bug#54764). * test/src/timefns-tests.el (encode-time-alternate-apis): New test. --- doc/lispref/os.texi | 5 +++++ etc/NEWS | 5 +++++ src/timefns.c | 21 +++++++++++++++------ test/src/timefns-tests.el | 9 +++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index cabae08970..bfcd51318e 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi @@ -1660,6 +1660,11 @@ Time Conversion handle situations like this you can use a numeric @var{zone} to disambiguate instead. +The first argument can also be a list @code{(@var{second} @var{minute} +@var{hour} @var{day} @var{month} @var{year})}, which is treated like +the list @code{(@var{second} @var{minute} @var{hour} @var{day} +@var{month} @var{year} nil -1 nil)}. + As an obsolescent calling convention, this function can be given six or more arguments. The first six arguments @var{second}, @var{minute}, @var{hour}, @var{day}, @var{month}, and @var{year} diff --git a/etc/NEWS b/etc/NEWS index 3e7788277d..c5a136ea68 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1950,6 +1950,11 @@ For example, '(time-add nil '(1 . 1000))' no longer warns that the '(1 . 1000)' acts like '(1000 . 1000000)'. This warning, which was a temporary transition aid for Emacs 27, has served its purpose. ++++ +** 'encode-time' now also accepts a 6-element list with just time and date. +(encode-time (list SECOND MINUTE HOUR DAY MONTH YEAR)) is now short for +(encode-time (list SECOND MINUTE HOUR DAY MONTH YEAR nil -1 nil)). + +++ ** 'date-to-time' now assumes earliest values if its argument lacks month, day, or time. For example, (date-to-time "2021-12-04") now diff --git a/src/timefns.c b/src/timefns.c index 7a4a7075ed..b0b84a438c 100644 --- a/src/timefns.c +++ b/src/timefns.c @@ -1620,6 +1620,9 @@ DEFUN ("encode-time", Fencode_time, Sencode_time, 1, MANY, 0, saving time, nil for standard time, and -1 to cause the daylight saving flag to be guessed. +TIME can also be a list (SECOND MINUTE HOUR DAY MONTH YEAR), which is +equivalent to (SECOND MINUTE HOUR DAY MONTH YEAR nil -1 nil). + As an obsolescent calling convention, if this function is called with 6 or more arguments, the first 6 arguments are SECOND, MINUTE, HOUR, DAY, MONTH, and YEAR, and specify the components of a decoded time. @@ -1645,7 +1648,7 @@ DEFUN ("encode-time", Fencode_time, Sencode_time, 1, MANY, 0, if (nargs == 1) { Lisp_Object tail = a; - for (int i = 0; i < 9; i++, tail = XCDR (tail)) + for (int i = 0; i < 6; i++, tail = XCDR (tail)) CHECK_CONS (tail); secarg = XCAR (a); a = XCDR (a); minarg = XCAR (a); a = XCDR (a); @@ -1653,11 +1656,17 @@ DEFUN ("encode-time", Fencode_time, Sencode_time, 1, MANY, 0, mdayarg = XCAR (a); a = XCDR (a); monarg = XCAR (a); a = XCDR (a); yeararg = XCAR (a); a = XCDR (a); - a = XCDR (a); - Lisp_Object dstflag = XCAR (a); a = XCDR (a); - zone = XCAR (a); - if (SYMBOLP (dstflag) && !FIXNUMP (zone) && !CONSP (zone)) - tm.tm_isdst = !NILP (dstflag); + if (! NILP (a)) + { + CHECK_CONS (a); + a = XCDR (a); + CHECK_CONS (a); + Lisp_Object dstflag = XCAR (a); a = XCDR (a); + CHECK_CONS (a); + zone = XCAR (a); + if (SYMBOLP (dstflag) && !FIXNUMP (zone) && !CONSP (zone)) + tm.tm_isdst = !NILP (dstflag); + } } else if (nargs < 6) xsignal2 (Qwrong_number_of_arguments, Qencode_time, make_fixnum (nargs)); diff --git a/test/src/timefns-tests.el b/test/src/timefns-tests.el index e7c464472d..08d06f27d9 100644 --- a/test/src/timefns-tests.el +++ b/test/src/timefns-tests.el @@ -225,6 +225,15 @@ encode-time-dst-numeric-zone (encode-time '(29 31 17 30 4 2019 2 t 7200)) '(23752 27217)))) +(ert-deftest encode-time-alternate-apis () + (let* ((time '(30 30 12 15 6 1970)) + (time-1 (append time '(nil -1 nil))) + (etime (encode-time time))) + (should (time-equal-p etime (encode-time time-1))) + (should (time-equal-p etime (apply #'encode-time time))) + (should (time-equal-p etime (apply #'encode-time time-1))) + (should (time-equal-p etime (apply #'encode-time (append time '(nil))))))) + (ert-deftest float-time-precision () (should (= (float-time '(0 1 0 4025)) 1.000000004025)) (should (= (float-time '(1000000004025 . 1000000000000)) 1.000000004025)) -- 2.35.1