reddit / emacs

How to insert today's date, but of last year, as an expansion of a template?

eis3nheim · 2022-10-29 16:29
For example, if today is 2022-10-29, I want to insert the same day and month as last year (2021-10-29). I have only found today's date, and I don't know how to perform an asthmatic operation like subtracting one from the year's parameter. Currently, this template expansion `%<%Y-%m-%d>` expands to today's date, how can I modify it?
eis3nheim · 2022-10-29 16:55
So after a bit of searching through the Emacs native functions, I came up with this function: `(format-time-string "%Y-%m-%d" (time-subtract (current-time) (* 365 86400)))` I think I could be able to use it with the [‘%(EXP)’](https://orgmode.org/manual/Template-expansion.html#:~:text=by%20FILE.-,%E2%80%98%25(EXP)%E2%80%99,-Evaluate%20Elisp%20expression) I am still open to a more professional opinion.
eis3nheim · 2022-10-29 16:57
Update: I am getting an error, Invalid function.
HM0880 · 2022-10-29 19:52
Try using `time-convert`: (format-time-string "%Y" (time-convert (time-subtract (current-time) (* 365 86400))))
gusbrs · 2022-10-29 21:59
There's also `decoded-time-add`: (format-time-string "%Y-%m-%d" (encode-time (decoded-time-add (decode-time) (make-decoded-time :year -1)))) It's a little more roundabout for the immediate purpose, as you have to decode an re-encode, but seems cleaner in that you don't have to handle the year conversion. It is also more semantic, you are really doing what you mean (would the conversion of year to seconds you used grant expected results for a skip year?).
akirakom · 2022-10-30 03:13
I would recommend this solution due to leap years. You could use `thread-last` to format the nested code: (thread-last (make-decoded-time :year -1) (decoded-time-add (decode-time)) (encode-time) (format-time-string "%F")))
gusbrs · 2022-10-30 03:40
Leap, leap!, not "skip year". :-)
eis3nheim · 2022-10-30 07:06
Thank you for your reply. How can I use it in expanding a template?
eis3nheim · 2022-10-30 07:07
Thank you for your reply. How can I use it in expanding a template?
gusbrs · 2022-10-30 10:31
I'm not sure I get what you are trying to do. I just suggested an alternative to the `time-subtract` you had, and supposed you meant `format-time-string`. A "template" of what? `%<%Y-%m-%d>` is not a self-standing elisp object, you must be trying this in some specific context, of which we know nothing about.
akirakom · 2022-10-30 11:59
If you are inside Org mode, you can insert the timestamp by `M-x org-time-stamp` (`C-c .`) and then type `-1y`. You can use the same command for editing an existing timestamp.