Skip to content

Instantly share code, notes, and snippets.

@werand
Created April 14, 2012 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werand/2387286 to your computer and use it in GitHub Desktop.
Save werand/2387286 to your computer and use it in GitHub Desktop.
Date calculations concerning my bday and easter
;; The content for the project.clj
#_(defproject datetest "1.0.0-SNAPSHOT"
:description "Some date calculations"
:dependencies [[clj-time "0.3.5"]
[org.clojure/clojure "1.3.0"]
[org.clojure/math.numeric-tower "0.0.1"]])
(ns test.date
(:use clojure.math.numeric-tower)
(:refer-clojure :exclude (extend))
(:use clj-time.core)
(:use clojure.pprint))
(defn div [& more] (floor (apply / more)))
;; http://en.wikipedia.org/wiki/Computus
;; Using the algorithm from http://www.bitshift.me/calculate-easter-in-clojure/ with a small
;; correction - i used the floor-function to correct the division results
(defn easter-sunday-for-year [year]
(let [golden-year (+ 1 (mod year 19))
century (+ (div year 100) 1)
skipped-leap-years (- (div (* 3 century) 4) 12)
correction (- (div (+ (* 8 century) 5) 25) 5)
d (- (div (* 5 year) 4) skipped-leap-years 10)
epac (let [h (mod (- (+ (* 11 golden-year) 20 correction)
skipped-leap-years) 30)]
(if (or (and (= h 25) (> golden-year 11)) (= h 24))
(inc h) h))
m (let [t (- 44 epac)]
(if (< t 21) (+ 30 t) t))
n (- (+ m 7) (mod (+ d m) 7))
day (if (> n 31) (- n 31) n)
month (if (> n 31) 4 3)]
(date-time year (int month) (int day))))
(defn good-friday-for-year [year]
(minus (easter-sunday-for-year year) (days 2)))
(defn birthday-on-good-friday [year-range bday-month bday-day]
(filter #(let [g (good-friday-for-year %)]
(and (= bday-month (month g)) (= bday-day (day g)))) year-range))
(defn birthday-on-easter [year-range bday-month bday-day]
(filter #(let [e (easter-sunday-for-year %)
easter-interval (interval e (plus e (days 2)))]
(within? easter-interval (date-time % bday-month bday-day))) year-range))
(print "Years with my birthday on good friday: ")
(pprint (birthday-on-good-friday (range 1900 2100) 4 9))
(print "Years with my birthday on easter sunday or easter monday: ")
(pprint (birthday-on-easter (range 1900 2100) 4 9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment