[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v4] org-encode-time compatibility and convenience helper

From: Max Nikulin
Subject: Re: [PATCH v4] org-encode-time compatibility and convenience helper
Date: Fri, 13 May 2022 22:14:32 +0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1

On 11/05/2022 20:20, Ihor Radchenko wrote:
Max Nikulin writes:

+              ;; In Emacs-27 and Emacs-28 `encode-time' does not support 6 
+              ;; list argument so `org-encode-time' can not be outside of 
+              (pcase-let
+                  ((`(,_ ,_ ,_ ,d ,m ,y ,dow . ,_) (decode-time start)))
+                (pcase step
+                  (`day (org-encode-time 0 0 org-extend-today-until (1+ d) m 
+                  (`week
+                   (let ((offset (if (= dow week-start) 7
+                                   (mod (- week-start dow) 7))))
+                     (org-encode-time 0 0 org-extend-today-until (+ d offset) 
m y)))
+                  (`semimonth (org-encode-time 0 0 0
+                                               (if (< d 16) 16 1)
+                                               (if (< d 16) m (1+ m)) y))
+                  (`month (org-encode-time 0 0 0 month-start (1+ m) y))
+                  (`year (org-encode-time 0 0 org-extend-today-until 1 1 (1+ 

I do not like repeating of `org-encode-time' but do not see another way
till Emacs-29 will become the lowest supported version.

This is fine. AFAIK, other parts of time handling code is full of conds
and pcases.

I mean that before my patch there was single `encode-time' outside of `pcase', I replace `list' by `org-encode-time' inside each pattern.

+           (org-encode-time
+             (apply #'list
+                    (or (car time0) 0)
+                    (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
+                    (+ (if (eq timestamp? 'hour) n 0)   (nth 2 time0))
+                    (+ (if (eq timestamp? 'day) n 0)    (nth 3 time0))
+                    (+ (if (eq timestamp? 'month) n 0)  (nth 4 time0))
+                    (+ (if (eq timestamp? 'year) n 0)   (nth 5 time0))
+                    (nthcdr 6 time0))))
        (when (and (memq timestamp? '(hour minute))
                 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))

I am tempting to write something like

    (let* ((ts (copy-sequence time0))
         (ord (memq timestamp? '(year month day hour minute)))
         (field (and ord (nthcdr (length ord) ts))))
      (when field
        (setcar field (+ (car field) n)))
      (org-encode-time ts))

but I am afraid it will make the code rather obscure.

Yes, the second version is rather hard to understand. The proper
solution would be writing (or using) some high-level time handling
library and then using it in Org. Then, we would not need to deal with
low-level time representations so frequently.

From my point of view

   (lambda (value part)
     (if (and part (eq part timestamp?))
         (+ n value)
   time0 '(second minute hour day month year nil nil nil))

is better than the original code, but...

Nicolas Goaziou to emacs-orgmode. [Patch] to correctly sort the items with emphasis marks in a list. Mon, 19 Apr 2021 18:08:17 +0200.
I stay away from CL as much as possible, otherwise newcomers will have
to learn two languages to start contributing, Elisp and CL (cl-loop,
ewww). CL is still necessary however, as we cannot use `seq' yet.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]