mobily / rescript-date

📆 Date manipulation in ReScript.

Home Page:https://mobily.github.io/rescript-date

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Curious about a Duration type

erlandsona opened this issue · 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 👋 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! :)

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.