Back

normalize (clj)

(source)

function

(normalize intervals)
Within a time-ordered sequence of disjoint intervals, return a sequence of interval groups, splicing together meeting intervals.

Examples

tick
(ns tick.alpha.interval-test
  (:require
   [clojure.spec.alpha :as s]
   [tick.core :as t]
   [tick.protocols :as p]
   [clojure.test
    :refer [deftest is testing run-tests]
    :refer-macros [deftest is testing run-tests]]
   [tick.alpha.interval :as ti]
   #?@(:cljs [[java.time :refer [Instant LocalDateTime LocalTime]]]))
  #?(:clj
     (:import [java.time LocalDateTime Instant LocalTime])))

(deftest normalize-test
  (let [intervals
        [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
         (ti/new-interval (t/date "2017-06-26") (t/date "2017-06-28"))
         (ti/new-interval (t/date "2017-06-30") (t/date "2017-07-04"))]]
    (is (= [{:tick/intervals
             [{:tick/beginning (t/date-time "2017-06-15T00:00")
               :tick/end (t/date-time "2017-06-26T00:00")}
              {:tick/beginning (t/date-time "2017-06-26T00:00")
               :tick/end (t/date-time "2017-06-29T00:00")}]}
            {:tick/intervals
             [{:tick/beginning (t/date-time "2017-06-30T00:00")
               :tick/end (t/date-time "2017-07-05T00:00")}]}]

           (ti/normalize intervals)))))