How do you divide a Data.Time.Duration into chunks?

I’m trying to find the N intermediate points between 2 dates. For the 2 dates I use the diff function in order to get a duration in Days. But then I cannot “escape” from the Duration constructors. I cannot get the wrapped Number in them.

How would you divide a duration by n :: Int ?

I guess the easiest way would be to use Milliseconds - as diff is generic you should be able to directly get one Millisecond value back and Milliseconds exports it’s data-constructor or if you prefer is Newtype.

So you can get a `number’ representing the milliseconds in your time diff.


EDIT: Sorry Days is surely better than Milliseconds as you want dates

Something like this should do:

intermediate :: Int -> Date -> Date -> Array Date
intermediate n from to =
  mapMaybe (\i -> adjust (delta i) from) (Array.range 0 (n-1))
  where
  (Days daysDiff) = diff from to
  delta i = Days (toNumber i * (daysDiff / toNumber n))

(you’ll probably want to think about the edge cases and ranges - I did not really test this or think too much about)

1 Like