Fork me on GitHub
#juxt
<
2020-07-12
>
dcj21:07:37

Given a tick interval and a time, is there an existing function to test if the time is within the interval? Given I couldn't find one, here is what I wrote (not claiming it is any good):

(defn within?
  [i time]
  (and (t/>= time (t/beginning i))
       (t/>= (t/end i) time)))

refset23:07:26

I think intersects? can do the job, but I'm not 100%

dcj05:07:51

What is an ival ?

(defn intersects? [ivals interval]
  (not-empty (intersection ivals [interval])))

refset08:07:17

the example on the docs shows:

(t/intersects? [(t/year)]
               (t/inc (t/year)))

dcj14:07:18

Doesn't seem like intersects? works with a non-interval....

=> t4
#time/zoned-date-time "2020-07-04T00:00-07:00[US/Pacific]"

=> ti
#:tick{:beginning #time/zoned-date-time "2020-07-04T00:00-07:00[US/Pacific]", :end #time/zoned-date-time "2020-07-05T00:00-07:00[US/Pacific]"}

=> (t/intersects? t4 ti)
Execution error (UnsupportedOperationException) at tick.interval/assert-proper-head (interval.cljc:427).
nth not supported on this type: ZonedDateTime

refset15:07:18

comparing with the reference code I posted, what happens when you add a vector in there (t/intersects? [t4] ti)

dcj17:07:49

Not sure how to interpret this:

=> (t/intersects? [t4] ti)
nil

dcj17:07:28

Also:

=> (t/intersects? [ti] ti)
(#:tick{:beginning #time/zoned-date-time "2020-07-04T00:00-07:00[US/Pacific]", :end #time/zoned-date-time "2020-07-05T00:00-07:00[US/Pacific]"})

dcj17:07:17

I question the use of the question mark at the end of this function name 🙂

tomd17:07:01

The tick versions of >= etc are variadic just like clojure.core's, so your within? function could just be:

(t/<= (t/beginning i) time (t/end i))
I'm not sure there is a shorter version than that

😎 3
dcj18:07:23

@UE1N3HAJH Nice! Thanks for the tip!

🙂 3