justinhj
justinhj

Reputation: 11316

How do I get the number of days in the month specified by an elisp time?

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   
)

Upvotes: 2

Views: 679

Answers (3)

Jeff Trull
Jeff Trull

Reputation: 1323

I came up with a solution that doesn't require timezone or calendar by extracting the day from the last day of the month:

(defun days-in-month-at-time(tm)
  (let ((d (decoded-time-add (decode-time tm) (make-decoded-time :month 1))))
    (setf (decoded-time-day d) 1)
    (decoded-time-day (decoded-time-add d (make-decoded-time :day -1))))
  )

Upvotes: 0

justinhj
justinhj

Reputation: 11316

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))))

Upvotes: 1

viam0Zah
viam0Zah

Reputation: 26322

(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))))

Upvotes: 6

Related Questions