From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id EKL/G3K4e2KtsgAAbAwnHQ (envelope-from ) for ; Wed, 11 May 2022 15:21:54 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id TefkG3K4e2JMLwEA9RJhRA (envelope-from ) for ; Wed, 11 May 2022 15:21:54 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 142BD28E4E for ; Wed, 11 May 2022 15:21:53 +0200 (CEST) Received: from localhost ([::1]:40408 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nomHo-0004Ua-RP for larch@yhetil.org; Wed, 11 May 2022 09:21:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nomGI-0004TY-GK for emacs-orgmode@gnu.org; Wed, 11 May 2022 09:20:18 -0400 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]:52058) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nomGA-0002o5-FF for emacs-orgmode@gnu.org; Wed, 11 May 2022 09:20:11 -0400 Received: by mail-pj1-x1034.google.com with SMTP id x88so2230217pjj.1 for ; Wed, 11 May 2022 06:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=gna11rn/mXT7bb5vwuuuH2+X29MIYaFVs1RPC4WI63E=; b=PEB4UCVqbj0IHFQnl7jIrIBp5OuQ7fwJUZ4IoTuFqHpIo2gYOnh1DxH+Nq0S0vgKrc os0c4ZhrXblm2x5jFvyGEyFuGnt4vfD1M9iMBEPN33pInKXCmLAocnP8UsK2sAeDl3O0 p1HPkbtQgiPxwDn9zpT6hvToirpPw8GhdwC5DKsiCo5/68tYueYVuY/CgzbLmjjBYmIV DyEUXnQ1ETh7Ya57feqsNm9ok2QYTJNInPNVIRzi0hwdpJm0rZx3EjAPZblqToZUHap6 TtGNr0sKMWi3r2QTOJ368WvpCywqhznhOIB7CSmgILUH6umH62RVA3xzgAaoYImvNVpP SZcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=gna11rn/mXT7bb5vwuuuH2+X29MIYaFVs1RPC4WI63E=; b=nfk8Tgq9Oz0p8z613Wk/BVoItRgSXGo8adrdaR/Iu5jtpiBLUrKbnPILqnvr8XctyQ iyroo3YTHtQhI4FTU6mdjBTdh+Gp6ugcJwjxVwbUe/5oP2wDk6cmzjZ3jCr94AtvLw/U DzSExnDFECGF7cOgI3j8htQVP0YED2OrH6grJkAojrGbKQHITYQNpz6i0pNB/76u8D/n 3SKcFrJETiu5jsVHw3Gk02YqQaw0kUMIF9ix1QcJBwUvWhP2BWP2kFmg1sb7sYL4kfT7 Hv0DCRuZclQ61SJCm+sDgVlcSg8piK7rtFqeVsOa58s4sXxweshQ+vNtNzncKzh/UHXk Ijxw== X-Gm-Message-State: AOAM533JHpaCcq3+UsHo5VjoQz0N7wqS7dMC47esLE0ZS49UbKGljMFw RPIlfjgYEQj+lxX/8Nmbaj6f1CmFXn4tXLZn X-Google-Smtp-Source: ABdhPJwAAsgkDo0Z+6ywzW1fSBPUySnab6qwG+Qep8IXT+PLh9acyiodc4HyaLxMwkQO70h/4WdyWg== X-Received: by 2002:a17:90b:4a4e:b0:1dc:55ca:6f33 with SMTP id lb14-20020a17090b4a4e00b001dc55ca6f33mr5401271pjb.4.1652275209093; Wed, 11 May 2022 06:20:09 -0700 (PDT) Received: from localhost ([66.154.104.4]) by smtp.gmail.com with ESMTPSA id w124-20020a626282000000b0050dc7628143sm1710854pfb.29.2022.05.11.06.20.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 06:20:08 -0700 (PDT) From: Ihor Radchenko To: Max Nikulin Cc: emacs-orgmode@gnu.org, Paul Eggert Subject: Re: [PATCH v4] org-encode-time compatibility and convenience helper In-Reply-To: References: <7f4ea652-7d22-fb61-f873-5e92f078c9e6@gmail.com> <87mtgcmckj.fsf@localhost> <875ymwus9p.fsf@localhost> <8735hpk4ef.fsf@localhost> <2661a0c6-a6a3-6934-16fe-7c987a6a1684@gmail.com> Date: Wed, 11 May 2022 21:20:51 +0800 Message-ID: <874k1wjjd8.fsf@localhost> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=yantar92@gmail.com; helo=mail-pj1-x1034.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1652275314; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=gna11rn/mXT7bb5vwuuuH2+X29MIYaFVs1RPC4WI63E=; b=rs8msMiF5dDqa8UvLHrWqGAcxHRYXPjxNg4EE23+CWJrqXdlj4jM+o3D7ZMd1EtYd2oyEe CxMcwHapbWBXx4HhCAmpj4GLre7VE7W8ox0UQTUn7lojFgJqq2v6sShsD0IJRJSiECKvth Wuu2paJNGyy5X05yHL0dpt18q0V1XgZMx5WszpqR4IsXRlTTRFko2yquwN7ROfV2NnLE+D QtUXr37RaQBWji/RTcTf0yQ07Cu634aWIpHI3BZiRrwHfNveVNDHR2+ALAaG4IjBHSLv7W pLQnTAzzdEJ5CRgOt49/9Oencohp+zcMsEUKgJfXM/2DATP6pgqoru6XQykMQw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1652275314; a=rsa-sha256; cv=none; b=MklNdS1FxTFtmSK8IIQTi97/f3bVUvuhhvGVZeh+x6lS5fmJSIsGrP3y/PfGYa2DSWzfO6 kYfmLs/y2gzruvLIo6i74Aq3qkDxen536mtC7A7jDF08N2d64MlFulM7JQGPBtp36o3ibQ OBM0sqafZLkA2ff8+nJKvjUlXmBA7vqc0fOKwWLqH00NouxToNRIHM6svjrlBr/F6oE8WL sRleBgbEV43fhrByKIho9IEGFXDYVphyB/GAmfC7FjJJEVMUN2zM0t1VhqEaOFMkW74udI yzLHnzVJkO3gWPyEp+8u3iD4ALLvsY9O9/GPXTPF3OH+Ri31LBE7FvRI3QWnLg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PEB4UCVq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -5.11 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PEB4UCVq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 142BD28E4E X-Spam-Score: -5.11 X-Migadu-Scanner: scn0.migadu.com X-TUID: SQgJOyX+AKKs Max Nikulin writes: >> + (defmacro org-encode-time (&rest time) >> + (pcase (length time) ; Emacs-29 since d75e2c12eb >> + (1 `(encode-time ,@time)) >> + ((or 6 9) `(encode-time (list ,@time))) >> + (_ (error "`org-encode-time' may be called with 1, 6, or 9 arguments but %d given" >> + (length time))))) > > Should it be something like the following? > > (signal 'wrong-type-argument (list '(1 6 9) (length time))) > > or even > > (signal 'wrong-type-argument > (list '(lambda (n-args) (memq n-args) '(1 6 9)) (length time))) > > Usually "wrong type argument" errors give no clue even related to called > function til enabling enter debugger on error and realizing how to > reproduce the problem. The current error is fine. I'd rather propose Emacs to change the "wrong type argument" message to mention the function name. >> + ;; In Emacs-27 and Emacs-28 `encode-time' does not support 6 elements >> + ;; list argument so `org-encode-time' can not be outside of `pcase'. >> + (pcase-let >> + ((`(,_ ,_ ,_ ,d ,m ,y ,dow . ,_) (decode-time start))) >> + (pcase step >> + (`day (org-encode-time 0 0 org-extend-today-until (1+ d) m y)) >> + (`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+ y)))))) > > 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. >> + (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)) >> extra >> (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. >> + (org-encode-time >> + (append '(0) >> + (mapcar >> + (lambda (prop) (or (org-element-property prop timestamp) 0)) >> + (if end '(:minute-end :hour-end :day-end :month-end :year-end) >> + '(:minute-start :hour-start :day-start :month-start >> + :year-start))) >> + '(nil -1 nil)))) >> >> (defun org-timestamp-has-time-p (timestamp) >> "Non-nil when TIMESTAMP has a time specified." > > Hardly may be considered as an example of elegant code. It is ok. You also could do it as `(0 ,@(mapcar (lambda (prop) ...) (if ...)) nil -1 nil) AFAIK, there is nothing much you can improve further without using function composition from dash.el. Best, Ihor