rluiten/elm-date-extra

Period diff example

Closed this issue · 4 comments

I'm having trouble understanding how best to extract data out of Date.Period.diff. It seems you must always "case result of Delta", is that right?

--- this doesn't work
dayDiff : Date -> Date -> Int
dayDiff d0 d1 = (diff d0 d1).day
--- this works, but is clunky
dayDiff : Date -> Date -> Int
dayDiff = 
  case (diff d0 d1) of
    Delta x -> x.day
    _ ->  Debug.crash "Never get here"

Thanks for a super useful library.

Glad you find it useful.

Due to the way diff works you may need to do (x.day + 7 * x.week) to get total whole days.
I am still not 100% happy with that specific area, but at the time i thought the week concept was worth having.

Another option that might suit you to get whole days in Period, this also lets you get fractional if you want them by using the floating point division instead.

days = (Period.toTicks (Period.diff d0 d1)) // Core.ticksADay

Just an aside - ticks in date extra are Integers not Floats it became that way because i was using Modulus and Integer Division in some places.

OK, thanks. What if you changed the signature of diff to

diff : Date -> Date -> DeltaRecord

Then you wouldn't need the awkward case matching. But maybe I'm missing something.

The Period.toTicks method doesn't work at the moment because toTicks isn't exposed. It seems a little internal-ish to do it that way anyway, to me.

I like the idea of changing the output of diff improves flexibility with no real added complexity.
I have published 3.0.0 with the change.

toTicks was meant to be available originally.
I don't think exposing toTicks is a bad idea at the moment, but I will have to think about it for now.

wow, thanks!