rescript-date icon indicating copy to clipboard operation
rescript-date copied to clipboard

Curious about a Duration type

Open erlandsona opened this issue 4 years ago • 2 comments

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 🤷‍♂️

erlandsona avatar Jun 01 '20 19:06 erlandsona

@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! :)

mobily avatar Jun 03 '20 07:06 mobily

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.

erlandsona avatar Jun 03 '20 16:06 erlandsona