Skip to content

Instantly share code, notes, and snippets.

@chopmo
Created January 12, 2016 18:15
Show Gist options
  • Save chopmo/2c995cfaed9505d09a56 to your computer and use it in GitHub Desktop.
Save chopmo/2c995cfaed9505d09a56 to your computer and use it in GitHub Desktop.
(ns playground.core
(:require [clojure.pprint :refer [pprint]]
[clojure.test :refer :all]
[clojure.set :as s]
[clj-time.core :as time]))
;; Just for testing
(def sessions-start (time/date-time 2010 1 1 10 00))
(defn download-sessions
[t]
(if (time/after? t sessions-start)
1
0))
(def tolerance 2)
(defn chopmo
([] (chopmo
(time/date-time 2005 1 1 3 0)
(time/now)
#(pos? (download-sessions %))
tolerance))
([start end pred tolerance]
(let [duration-hours (-> (time/interval start end) .toDuration .getStandardHours)]
(if (< duration-hours tolerance)
end
(let [halfway (time/plus start (time/hours (quot duration-hours 2)))
first-half? (pred halfway)
new-start (if first-half? start halfway)
new-end (if first-half? halfway end)]
(recur new-start new-end pred tolerance))))))
(deftest find-start-date-test
(testing "Finding the right date"
(let [t (chopmo)]
(is (time/after? t (time/minus sessions-start (time/hours tolerance))))
(is (time/before? t (time/plus sessions-start (time/hours tolerance)))))))
(find-start-date-test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment