Emacs: Timers for Weekly Events (publ. 2024-07-23)

A great feature of Emacs is the timer library. This includes `run-at-time' for starting a timer at a particular time (relative or absolute) and it can receive an argument for repeating the timer every 𝑥 number of seconds after that. You can list all your active timers with the `list-timers' command, which interface also includes a handy shortcut key for cancelling a timer.

It is not obvious, however, how to use run-at-time to set up a weekly timer event. It is possible to pass a time of day to run-at-time, but that forces the user to check what day of the week it is; also there is a quirk in the design of run-at-time, such that if you specify a time of day, the timer will immediately run if the time of day is already past that point. Now, in Elisp it is not too hard to calculate the number of seconds until some future date. But that is not very helpful since, if your run-at-time is part of your init.el, you would need to first calculate what that next future date would be, which is not trival.

To deal with this problem, I came up with my own function that calculates the number of seconds until a specified day, hour, and minute of the week:

I'm currently testing this function with a once a week call to a function which stamps the time and date in the *Messages* log.

One caveat is that secs-until-weekly does not make any adjustments for leap seconds. So to prevent any sizeable drift (over the centuries!) you'll want to make the run-at-time call non-repeating, and instead have your timer function grab the current time again and start a new timer using secs-until-weekly.

This work © 2024 by Christopher Howard is licensed under Attribution-ShareAlike 4.0 International.

CC BY-SA 4.0 Deed

Proxied content from gemini://gem.librehacker.com/gemlog/starlog/20240723-0.gmi

Gemini request details:

Original URL
gemini://gem.librehacker.com/gemlog/starlog/20240723-0.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.