all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: calculating relative time until a day of week
Date: Mon, 08 Jul 2024 20:00:16 +0200	[thread overview]
Message-ID: <87ed83n4r3.fsf@dataswamp.org> (raw)
In-Reply-To: 877cdwbsn5.fsf@web.de

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




      reply	other threads:[~2024-07-08 18:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-06 20:38 calculating relative time until a day of week Christopher Howard
2024-07-07  5:10 ` tomas
2024-07-08  1:05 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-07-08 18:00   ` Emanuel Berg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ed83n4r3.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.