emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ken Mankoff <mankoff@gmail.com>
To: David Rogers <davidandrewrogers@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Diary sexp and comparing dates, agenda
Date: Fri, 12 Jan 2024 10:11:56 -0800	[thread overview]
Message-ID: <87bk9ql8ur.fsf@gmail.com> (raw)
In-Reply-To: <rzhwmsfhz4i.fsf@gmail.com>

Hi David,

I'm not sure if this will help, but I have to file time sheets on the last weekday less than or equal to the 15th and last-day-of-month.

I wrote two diary functions to achieve this (I don't handle holiday edge cases). I use these functions with:

** INFO [#A] Time Sheet
<%%(kdm/diary-last-weekday-of-month date)>
<%%(kdm/diary-last-weekday-15th date)>

Functions are:

(defun kdm/diary-last-weekday-15th (date)
  (let* (
	 (dow (calendar-day-of-week date))
	 (dom (calendar-extract-day date))
	 (weekday (and (>= dow 1) (<= dow 5))))
    (or 
     (and weekday (= dom 15))
     (and (= dow 5) (= dom 14))
     (and (= dow 5) (= dom 13)))))

(defun kdm/diary-last-weekday-of-month (date)
  (let* (
	 (dow (calendar-day-of-week date))
	 (dom (calendar-extract-day date))
         (month (calendar-extract-month date))
         (year (calendar-extract-year date))
         (ldom-num (calendar-last-day-of-month month year))
	 (weekday (and (>= dow 1) (<= dow 5))))
    (or 
     (and weekday (= dom ldom-num))
     (and (= dow 5) (= (+ 1 dom) ldom-num))
     (and (= dow 5) (= (+ 2 dom) ldom-num)))))

  -k.

On 2024-01-11 at 21:58 -08, David Rogers <davidandrewrogers@gmail.com>
wrote...
> Hello all
>
> I'm using the Org agenda to show when certain church occasions will
> happen. Mostly I've got them working correctly, after "stealing" the
> method used in holidays.el for finding the date of Easter, along with
> someone else's function that then uses that to find the difference
> between Easter and today. (I'm pretty sure that whole idea was on
> emacswiki, and I think Paul Sexton put it together.)
>
> So now I have a much smaller problem to solve, but I don't understand
> how to get it to work; I suspect it has to do with how dates get
> formatted within different functions. Using what I already have, I can
> do this:
>
> * Example 1 <%%(= 245 (mf-days-from-easter))>
>
> because "mf-days-from-easter" is defined in my init file, along with
> the definition of Easter itself. This does what it looks like it
> should do; this year, Example 1 is shown on December 1.
>
> And I can do
>
> * Example 2 <%%(and (diary-float 1 0 5 7) (<= (mf-days-from-easter)
> -56))>
>
> to say "five Sundays after January 6th, but only if Easter is still 8
> weeks away or more".
>
> But Example 1 isn't quite finished, because that date is too close to
> Christmas. I can easily show a diary sexp defining when "too close to
> Christmas" is:
>
> <%%(diary-float 12 0 -4 24)>
>
> So I want to combine these ideas, to say "Show Example 1 in the agenda
> 245 days after Easter each year, but only if it's earlier than the
> fourth Sunday before Christmas".
>
> I could do a long string of (and (not this day, not this day, etc etc
> [insert long list of days] . . . , but that seems like a last resort.
>
> Everything in this question fits into "the current year according to
> the agenda view" - there's no need to consider dates that cross a year
> boundary.
>
> So: Is there a fairly simple way to define a (mf-days-from-advent)
> that will do a similar job to what (mf-days-from-easter) is already
> doing (i.e. it works when used in a diary sexp)?
>
>
> Here are the relevant definitions I've been using:
>
>
>    (defun mf-easter (displayed-year)
>        (let* ((century (1+ (/ displayed-year 100)))
>               (shifted-epact ; age of moon for April 5...
>                (% (+ 14 (* 11 (% displayed-year 19)) ; ...by Nicaean
>                rule
>                      (- ; ...corrected for the Gregorian century rule
>                       (/ (* 3 century) 4)) (/ ; ...corrected for
>                      Metonic cycle inaccuracy
>                       (+ 5 (* 8 century)) 25) (* 30 century)) ; keeps
>                      value positive
>                   30)) (adjusted-epact ; adjust for 29.5 day month
>                (if (or (zerop shifted-epact)
>                        (and (= shifted-epact 1) (< 10 (%
>                       displayed-year 19))))
>                    (1+ shifted-epact) shifted-epact))
>               (paschal-moon ; day after the full moon on or after
>               March 21
>                (- (calendar-absolute-from-gregorian (list 4 19
>                displayed-year))
>                   adjusted-epact))) (calendar-dayname-on-or-before 0
>               (+ paschal-moon 7))))
>
>
>
>    (defun mf-days-from-easter ()
>      "When used in a diary sexp, this function will calculate how many
> days are between the current date (DATE) and Easter Sunday."
>      (- (calendar-absolute-from-gregorian date)
>         (mf-easter (calendar-extract-year date))))



  parent reply	other threads:[~2024-01-12 17:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-12  5:58 Diary sexp and comparing dates, agenda David Rogers
2024-01-12 17:01 ` Tory S. Anderson
2024-01-12 18:11 ` Ken Mankoff [this message]
2024-01-12 23:00 ` Ihor Radchenko
2024-01-13  5:41   ` David Rogers
2024-01-13 20:05     ` Ihor Radchenko
2024-01-13 23:44       ` David Rogers

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

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=87bk9ql8ur.fsf@gmail.com \
    --to=mankoff@gmail.com \
    --cc=davidandrewrogers@gmail.com \
    --cc=emacs-orgmode@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).