tags:

views:

88

answers:

2

In elisp I have a time in the (form of three integers), and I can get the month using decode-time. What I'd like to get is the number of days in that month (and year), using elisp functions (rather than write my own).

i.e:

(defun days-in-month-at-time(t)
    ; Figure out month and year with decode-time
    ; return number of days in that month   
)
+5  A: 
(require 'timezone)

(defun days-in-month-at-time (time)
  "Return number of days in month at TIME."
  (let ((datetime (decode-time time)))
    (timezone-last-day-of-month (nth 4 datetime) (nth 5 datetime))))
Török Gábor
Also see related SO question : http://stackoverflow.com/questions/753248/insert-whole-month-of-dates-in-emacs-lisp
Trey Jackson
Perfect, thanks!
justinhj
timezone-last-day-of-month is not present on my work machine Windows build of emacs 23.1.1 I wonder if it's been removed, or added after that?
justinhj
Ah, I see, you need to load or require calendar (or timezone), hence the apparent difference.
justinhj
@justinhj: you're right, neither `calendar` or `timezone` loaded by default, so you may need require the package first.
Török Gábor
A: 

Looks like this is better since time-zone doesn't seem to be in all recent emacs versions edit: Sorry you just need to require timezone, or calendar depending on whether you use this or the other answer.

(defun days-in-month-at-time (time)
  "Return number of days in month at TIME."
  (let ((datetime (decode-time time)))
    (calendar-last-day-of-month (nth 4 datetime) (nth 5 datetime))))
justinhj
This doesn't work for me in emacs 22.1.1 on Mac: (days-in-month-at-time (current-time))
I think you need (require 'calendar) first.
justinhj