0

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...... -----------------------------------------------------
2

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.

2
  • amazing and excellent code example to learn elisp for the next days , thank you. – AbstProcDo Mar 3 '20 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). – Emmanuel Goldstein May 9 '20 at 9:06

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.