Sha256: 78c68806f54b3ccd6614b284cf97850ba1a98a57cc15e69b27027f0a6524b5ca

Contents?: true

Size: 1.72 KB

Versions: 396

Compression:

Stored size: 1.72 KB

Contents

(ns meetup
  (:import (java.util Calendar)))

(def day-offsets
  {:sunday    1
   :monday    2
   :tuesday   3
   :wednesday 4
   :thursday  5
   :friday    6
   :saturday  7})

(def days-of-month
  {:first 1
   :second 8
   :third 15
   :fourth 22
   :teenth 13})

(defn- ^Calendar to-date
  [year month day]
  (let [cal (Calendar/getInstance)]
    (.set cal Calendar/YEAR year)
    (.set cal Calendar/MONTH (dec month))
    (.set cal Calendar/DATE day)
    (.set cal Calendar/HOUR_OF_DAY 0)
    (.set cal Calendar/MINUTE 0)
    (.set cal Calendar/SECOND 0)
    (.set cal Calendar/MILLISECOND 0)
    cal))

(defn- date-parts
  [date]
  [(.get date Calendar/YEAR)
   (inc (.get date Calendar/MONTH))
   (.get date Calendar/DATE)])

(defn- days-to-move
  [weekday offset-and-direction]
  (let [offset    (Math/abs offset-and-direction)
        direction (/ offset-and-direction offset)]
    (* (mod (* -1 direction (- weekday offset)) 7) direction)))

(defn- offset-date
  [year month day offset]
  (let [date    (to-date year month day)
        weekday (.get date Calendar/DAY_OF_WEEK)]
    (.roll date Calendar/DATE (days-to-move weekday offset))
    date))

(defn- last-day-of-month
  [year month]
  (let [date (to-date year month 1)]
    (.getActualMaximum date Calendar/DAY_OF_MONTH)))

(defn- offset-for
  [schedule day-of-week]
  (if (= :last schedule)
    (* -1 day-of-week)
    day-of-week))

(defn meetup
  [month year day-name schedule]
  (let [day-of-month (if (= :last schedule)
                       (last-day-of-month year month)
                       (get days-of-month schedule))
        day-of-week (get day-offsets day-name)
        offset (offset-for schedule day-of-week)]
    (date-parts (offset-date year month day-of-month offset))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/meetup/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/meetup/src/example.clj