月の最後の日を調べる


Tags: 日付と時刻, R6RS, SRFI

月の最後の日を調べる

単純な計算によるもの。

(import (rnrs) (srfi :19))

(define the-last-day-in-the-month
  (let ((dv '#(31 #f 31 30 31 30 31 31 30 31 30 31)))
    (define (leap-year? year)
      (or (zero? (mod year 400))
          (and (not (zero? (mod year 100)))
               (zero? (mod year 4)))))
    (lambda (date)
      (or (vector-ref dv (- (date-month date) 1))
          (if (leap-year? (date-year date)) 29 28)))))

次の月の最初の日の前の日 = 月の最後の日、ということを利用した計算。

(import (rnrs) (srfi :19))

(define (the-last-day-in-the-month date)
  (let ((m (date-month date)))
    (date-day
      (time-tai->date
        (subtract-duration (date->time-tai
          (if (= m 12)
            (make-date 0 0 0 0 1 1 (+ (date-year date) 1) (date-zone-offset date))
            (make-date 0 0 0 0 1 (+ m 1) (date-year date) (date-zone-offset date))))
          (make-time time-duration 0 1))))))