From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: calculating relative time until a day of week Date: Mon, 08 Jul 2024 20:00:16 +0200 Message-ID: <87ed83n4r3.fsf@dataswamp.org> References: <87v81itfvs.fsf@librehacker.com> <877cdwbsn5.fsf@web.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28683"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:4rWhEbXiTkMxs2REiSAWue6jjO0= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jul 09 15:49:59 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sRBEF-0007BY-0i for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 09 Jul 2024 15:49:59 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sRBDX-0007SW-NR; Tue, 09 Jul 2024 09:49:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sQsf9-000874-7r for help-gnu-emacs@gnu.org; Mon, 08 Jul 2024 14:00:31 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sQsf6-0004HN-CZ for help-gnu-emacs@gnu.org; Mon, 08 Jul 2024 14:00:30 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1sQsf2-0006r1-53 for help-gnu-emacs@gnu.org; Mon, 08 Jul 2024 20:00:24 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io 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, HEADER_FROM_DIFFERENT_DOMAINS=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 09 Jul 2024 09:49:14 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:147130 Archived-At: Michael Heerdegen via Users list for the GNU Emacs text editor wrote: >> Hi, I want to make an Emacs timer that runs a command once >> a week, every Tuesday at 4am. I could do this with >> run-at-time and a relative time, but I would need to >> calculate how many seconds until Tuesday 4am. Is there an >> easy way to calcuate that? > > Depending on what that command is for, an answer might be > "Use diary and appointments" Or org-mode. Here is a bunch of time stuff, maybe some of it can be modified for that purpose. ;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/time-cmp.el (require 'cl-lib) (require 'pcase) (defun wall-clock-time (h1 m1 s1 h2 m2 s2) (let*((y 1978) (m 05) (d 08) ; arbitrary date, 1978-05-08 (total-seconds-1 (float-time (encode-time s1 m1 h1 d m y))) (total-seconds-2 (float-time (encode-time s2 m2 h2 d m y))) (s-diff (- total-seconds-2 total-seconds-1)) ) (format-seconds "%.2h:%.2m:%.2s" s-diff) )) (defalias 'wct #'wall-clock-time) ;; (wct 09 35 10 23 00 00) ; 13:24:50 (defun days (y1 m1 d1 y2 m2 d2) (let*((then (float-time (encode-time 0 0 0 d1 m1 y1))) (now (float-time (encode-time 0 0 0 d2 m2 y2))) (diff (- now then)) ) (string-to-number (format-seconds "%d" diff)) )) ;; (days 1958 04 13 1958 08 30) ; 139 days between Tahiti Nui 2 & 3 (defun days-date (date1 date2) (pcase-let*( (sep "-") (`(,y1 ,m1 ,d1) (cl-map 'list #'string-to-number (split-string date1 sep))) (`(,y2 ,m2 ,d2) (cl-map 'list #'string-to-number (split-string date2 sep))) ) (days y1 m1 d1 y2 m2 d2) )) ;; (days-date "2021-03-19" "2021-04-20") ; 31 ;; (days-date "1964-07-26" "2021-03-22") ; 20 693 (defun get-time-since (y m d) (interactive "nyear: \nnmonth: \nnday: ") (message "%s" (format-seconds "%yy %dd" (float-time (time-since (encode-time 0 0 0 d m y)) )))) ;; (get-time-since 2011 09 27) ; 10y 172d [2022-03-15] (defun days-since (y m d) (string-to-number (format-seconds "%d" (float-time (time-since (encode-time 0 0 0 d m y))) ))) ;; (days-since 1 1 1) ; 738 228 days from 0001-01-01 [2022-03-15] (provide 'time-cmp) -- underground experts united https://dataswamp.org/~incal