0

I'm trying to build functions that help me execute frequent sparse tree queries, using org-match-sparse-tree nil "<some_query>" in my code.

Some of these queries need to contain constraints such as SCHEDULED<=<end_of_this_month>, where <end_of_this_month> just means the end of the current month. The same could be true for this week, next week, this year, and so on.

I know about the possibility of writing SCHEDULED<=<1+y>, but that just means 1 year from now, meaning, as of the time of this writing, from Aug 28 2019 to Aug 27 2020, which is not what I want.

Is there a way to, programmatically, obtain in a string the end of the current or next month/year/week without having to manually calculate those (meaning using a preexisting date-handling function)?

  • 1
    The calendar.el library has a function: (calendar-last-day-of-month MONTH YEAR). There is probably a buit-in function to obtain the current month / year, but if not, you can extract it form (current-time) and/or format-time-string that defaults to the current time ..... See also org-time-today, parse-time-string and org-parse-time-string ... – lawlist Aug 28 at 21:32
  • See also the elmo.el library that has a function that is easily extracted/separated called elmo-time-to-datevec (which relies on the built-in timezone.el library) -- see github.com/wanderlust/wanderlust/blob/master/elmo/elmo-date.el E.g., (elmo-time-to-datevec (current-time)) at this precise moment in time returns [2019 8 28 14 50 56 (-25200 "PDT")] and we can then extract the nth element from the vector ... – lawlist Aug 28 at 21:50
  • 1
    (calendar-last-day-of-month (string-to-number (format-time-string "%m")) (string-to-number (format-time-string "%y"))) – Ivan Perez Aug 28 at 21:52
  • I think that would work. Hmm, I wonder how to get the last day of the current week, now? – Ivan Perez Aug 28 at 21:53
  • 1
    Here is a link that contains a function last-day-of-week ... andersen.berlin/emacs.d I have not studied to see who wrote it .... The result is in time and needs to be converted ... – lawlist Aug 28 at 22:01
1

Arthur Andersen has published his emacs configuration, and contains exactly these functions, here. (Credit to @lawlist for pointing out where this was, in the comments to the original question.)

The functions to calculate end of the day/month/year are:

(defun last-day-of-year ()
  "Return the last day of the year as time."
  (encode-time 0 0 0 31 12 (nth 5 (decode-time
                                    (current-time)))))

(defun last-day-of-month ()
  "Return the last day of month as time."
  (let* ((now (decode-time (current-time)))
         (month (nth 4 now))
         (year (nth 5 now))
         (last-day-of-month (calendar-last-day-of-month month year)))
        (encode-time 0 0 0 last-day-of-month month year)))

(defun last-day-of-week ()
  "Return the last day of the week as time."
  (let* ((now (current-time))
         (datetime (decode-time now))
         (dow (nth 6 datetime)))
        (time-add now (days-to-time (- 7 dow)))))

These functions return a time, not a date, so you need to convert them appropriately, such as:

(format-time-string "%Y-%m-%d" (last-day-of-year))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.