4

I set the diary file as

(setq org-agenda-include-diary t)
(setq org-agenda-diary-file "~/Documents/OrgMode/ORG/src/standard-diary") ;;2020-03-02 10:47:06
(setq diary-file "~/Documents/OrgMode/ORG/src/standard-diary")

and the Day info in the diary

;;Day info
%%(diary-sunrise-sunset)
%%(diary-lunar-phases)
%%(diary-iso-date)

Then check the day view from agenda

Tuesday     3 March 2020
               3:00...... -----------------------------------------------------
  Diary:       3:59...... First Quarter Moon (CST)
               6:00...... -----------------------------------------------------
  Diary:       6:46...... Sunrise (CST), sunset 6:07pm (CST) at 40.0N, 116.3E (11:21 hrs daylight)
               9:00...... -----------------------------------------------------
               9:02...... now - - - - - - - - - - - - - - - - - - - - - - - - -
              12:00...... -----------------------------------------------------
              15:00...... -----------------------------------------------------
              18:00...... -----------------------------------------------------
              21:00...... -----------------------------------------------------
              24:00...... -----------------------------------------------------

In the day view the Sunrise time and the sunset time displayed on the same line,
How could place them appropriate the right clock time as

Tuesday     3 March 2020
               3:00...... -----------------------------------------------------
  Diary:       3:59...... First Quarter Moon (CST)
               6:00...... -----------------------------------------------------
  Diary:       6:46...... Sunrise (CST) - - - - - - - - - - - - - - - - - - - - 
               9:00...... -----------------------------------------------------
               9:02...... now - - - - - - - - - - - - - - - - - - - - - - - - -
              12:00...... -----------------------------------------------------
              15:00...... -----------------------------------------------------
              18:00...... -----------------------------------------------------
              18:07...... Sunset 6:07pm (CST) at 40.0N, 116.3E (11:21 hrs daylight)
              21:00...... -----------------------------------------------------
              24:00...... -----------------------------------------------------

1 Answer 1

6

diary-sunrise-sunset returns a string with both times:

"Sunrise 6:17am (EST), sunset 5:36pm (EST) at 40.3N, 71.0W (11:19 hrs daylight)"

So use diary-sunrise and diary-sunset separately:

%%(diary-sunrise)
%%(diary-sunset)

where these two functions are defined as follows (somewhere in your init file):

(defun diary-sunrise ()
  (let ((dss (diary-sunrise-sunset)))
    (with-temp-buffer
      (insert dss)
      (goto-char (point-min))
      (while (re-search-forward " ([^)]*)" nil t)
        (replace-match "" nil nil))
      (goto-char (point-min))
      (search-forward ",")
      (buffer-substring (point-min) (match-beginning 0)))))

(defun diary-sunset ()
  (let ((dss (diary-sunrise-sunset))
        start end)
    (with-temp-buffer
      (insert dss)
      (goto-char (point-min))
      (while (re-search-forward " ([^)]*)" nil t)
        (replace-match "" nil nil))
      (goto-char (point-min))
      (search-forward ", ")
      (setq start (match-end 0))
      (search-forward " at")
      (setq end (match-beginning 0))
      (goto-char start)
      (capitalize-word 1)
      (buffer-substring start end))))

Each of these calls diary-sunrise-sunset and parses the return value to get one of the two times.

In order for the times to be correct you have to supply your location, e.g.:

(setq calendar-latitude 40.3)
(setq calendar-longitude -71)

That's somewhere in the Atlantic off of New York City: adjust to taste.

Note that diary-sunset does capitalize sunset but it also strips the location off, which is fine for me: you'll have to change the parsing a bit in the second function if you want to keep it.

EDIT in response to comment: It's actually not a very good code example to learn elisp from. It's only doing regexp matching and string operations for one. But the biggest problem is that the diary functions in general (and diary-sunrise-sunset in particular) require that a variable named date be set before you can call them. So to really run them e.g. in the *scratch* buffer in lisp-interaction-mode, you have to say something like this:

(setq date '(3 14 2020))
(diary-sunrise-sunset)

The date is a list of three numbers: the month, the day and the year.

5
  • amazing and excellent code example to learn elisp for the next days , thank you.
    – Wizard
    Commented Mar 3, 2020 at 4:59
  • Thanks so much! I tried this but I get only the label: "Sunset" instead of "Sunset 6:07pm (CST) at 40.0N, 116.3E (11:21 hrs daylight)". Not sure what I am doing wrong. I followed the guidelines to the mm :) (and same for sunrise, of course. I would be nice to get the longer string with the data on location and day length). Commented May 9, 2020 at 9:06
  • Alternative implementation suggestion: (defun diary-sunrise () (let ((dss (diary-sunrise-sunset))) (save-match-data (string-match "^Sunrise \\([^ ]*\\) " dss) (concat "🌅 " (match-string 1 dss)) ))) (defun diary-sunset () (let ((dss (diary-sunrise-sunset))) (save-match-data (string-match ", sunset \\([^ ]*\\) " dss) (concat "🌇 " (match-string 1 dss)) )))
    – hvr
    Commented Feb 29 at 8:08
  • Please make your comment into an answer: comments are not meant to contain large amounts of code and they are meant to ask/provide clarifications/corrections, not alternative implementations. As an answer, your code will also be much more visible (and you may earn some reputation points too - which is not the case with comments).
    – NickD
    Commented Feb 29 at 12:33
  • @EmmanuelGoldstein: sorry I missed your question. You have to set calendar-latitude and calendar-longitude for your location as described in the question. Otherwise, you don't get any information out of the function. See the debugging info in the EDIT as well.
    – NickD
    Commented Feb 29 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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