0

Usually, a calendar comes as a table. I want to generate a list instead:

  • October

  • October 1, 2017, Sunday

  • October 2, 2017, Monday
  • October 3, 2017, Tuesday
  • October 4, 2017, Wednesday
  • October 5, 2017, Thursday
  • October 6, 2017, Friday
  • October 7, 2017, Saturday
  • October 8, 2017, Sunday
  • ...
  • October 30, 2017, Monday
  • October 31, 2017, Tuesday

  • November

  • November 1, 2017, Wednesday

  • ...

I am wondering how to do it for the whole year. M-x calendar can generate tex and html for only a month and, what's more substantial, day names do not correspond to dates directly. Please, help.

2

The commando calendar-list defined in the following elisp code delivers the list as you want it in the buffer *calendar-list*. The code is split into two functions. The function my-date-list generates a lisp list of strings for the dates. One can specify the format for the strings. One can also specify month boundaries with corresponding format string to have separate entries for month boundaries in the list.

The function calendar-list inserts the text generated by my-date-list into a buffer called *calendar-list* and displays that buffer.

(defun my-date-list (start end &optional format limits)
  "Generate a list of days from START date to END date.
The list contains for each day the string generated
with `format-time-string' with format string FORMAT.
LIMITS is the list of changes indicated by extra header lines.
Valid list entries are pairs (month . \"%B\") and (year . \"%Y\")."
  (interactive "sStart time:\nsEnd time:")
  (unless format
    (setq format "%B %e, %Y, %A"))
  (when (stringp start)
    (setq start (parse-time-string start)))
  (when (stringp end)
    (setq end (parse-time-string end)))
  (setq start (cl-substitute 0 nil start)
    end (cl-substitute 0 nil end))
  (let ((time (apply #'encode-time start))
    (end-time (apply #'encode-time end))
    (one-day (days-to-time 1))
    (year (nth 5 start))
    fmt-year
    fmt-month
    (month (nth 4 start))
    ret)
    (when (setq fmt-year (assoc-string 'year limits))
      (setq ret (list (format-time-string (cdr fmt-year) start))))
    (when (setq fmt-month (assoc-string 'month limits))
      (setq ret (list (format-time-string (cdr fmt-month) start))))
    (while (null (time-less-p end-time time))
      (let* ((date (decode-time time))
         (new-year (nth 5 date))
         (new-month (nth 4 date)))
    (when (and (assoc-string 'year limits)
           (/= new-year year))
      (setq ret (cons (format-time-string (cdr fmt-year) time) ret)
        year new-year))
    (when (and (assoc-string 'month limits)
           (/= new-month month))
      (setq ret (cons (format-time-string (cdr fmt-month) time) ret)
        month new-month))
    (setq ret (cons (format-time-string format time) ret)
          time (time-add time one-day))))
    (nreverse ret)))

(defun calendar-list (start end)
  "Create a calendar from START date to END date."
  (interactive "sStart date:\nsEnd date:")
  (with-current-buffer (get-buffer-create "*calendar-list*")
    (delete-region (point-min) (point-max))
    (insert (mapconcat (lambda (date) (concat "* " date)) (my-date-list start end nil '((month . "%B"))) "\n"))
    (display-buffer (current-buffer))))
| improve this answer | |
0

Ok, I found it simpler to write a quick python script. In case someone has the same problem:

import calendar
import pprint

x = calendar.Calendar(calendar.SUNDAY).yeardays2calendar(2017, 12)

for month in range(1, 13):
    if (month == 1): 
        month_str = "January"
    elif (month == 2): 
        month_str = "February"
    elif (month == 3): 
        month_str = "March"
    elif (month == 4): 
        month_str = "April"
    elif (month == 5): 
        month_str = "May"
    elif (month == 6): 
        month_str = "June"
    elif (month == 7): 
        month_str = "July"
    elif (month == 8): 
        month_str = "August"
    elif (month == 9): 
        month_str = "September"
    elif (month == 10): 
        month_str = "October"
    elif (month == 11): 
        month_str = "November"
    elif month == 12:
        month_str = "December"
    print(month_str)
    # print(x[0][month - 1])
    for week in x[0][month - 1]:
        for day in week:
            if (day[1] == 0):
                day_str = "Monday"
            elif (day[1] == 1):
                day_str = "Tuesday"
            elif (day[1] == 2):
                day_str = "Wednesday"
            elif (day[1] == 3):
                day_str = "Thursday"
            elif (day[1] == 4):
                day_str = "Friday"
            elif (day[1] == 5):
                day_str = "Saturday"
            elif (day[1] == 6):
                day_str = "Sunday"

            # print("Total score for {} is {}".format(name, score))
            if (day[0] != 0):
                if (day[0] < 10):
                    print("{}  {}, {}".format(month_str, day[0], day_str))
                else:
                    print("{} {}, {}".format(month_str, day[0], day_str))
| improve this answer | |
  • 1
    I've been using Emacs for about 5 years, and I've dabbled in python when writing plugins for Sublime Text 2. I also work on my own feature requests for Emacs internals using C. I have to say that by looking at this particular answer, I haven't the slightest idea how to use it or how any intermediate Emacs user could make heads or tails out of it. How would someone coming to this thread know how to generate this list from within Emacs? Consider writing up a Lisp solution so that a user can just type M-x butterfly and generate the list. In addition, most calendar functions have ranges. – lawlist Oct 29 '17 at 20:34
  • 2
    What does this answer have to do with emacs? If the answer is (as I expect) "nothing", why is it on emacs SE? – NickD Oct 29 '17 at 22:41
  • Please try to make this answer (and question!) more specific to Emacs. (For example: can you write a function in elisp to call your python script and deal with the output?) If it's not Emacs specific, this Q&A probably belongs on a sister site instead. – Dan Oct 30 '17 at 17:44
  • Is there some exciting way for me to use Python to extend Emacs that I don't know about? – Dodgie Jan 27 '18 at 19:17

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.