mobily / rescript-date

📆 Date manipulation in ReScript.
https://mobily.github.io/rescript-date
MIT License
108 stars 6 forks source link

Curious about a Duration type #12

Open erlandsona opened 4 years ago

erlandsona commented 4 years ago

I'm returning a Postgres interval to my client via EctoInterval which serializes a Postgrex.Interval into some Json that looks like {secs: 0, months: 50, microsecs: 0, days: 25}... I'd like to be able to serialize that into something I can pass to date-fns/formatDuration. For now I'll make my own Duration record but it'd be nice if this lib had a solution for it 🤷‍♂️

mobily commented 4 years ago

@erlandsona 👋 it looks doable, I will handle this over the weekend :) or you can create a pull request with implementation and I'll be happy to merge it in, let me know! :)

erlandsona commented 4 years ago

So I just stuck one of these in my project... Date.re

[@decco]
type interval = {
  months: int,
  days: int,
  secs: int,
  microsecs: int,
};

Then to get years I'm using let (years, months) = Relude.Int.divideWithModulo(months, 12) to get the years / months but I'd prefer to stick that in a decode function that serializes my shape into a Duration type to be used in conjunction with an external to formatDuration although, I'm not a fan of the signature of that function. I'd prefer something that won't throw exceptions on bad input.