Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Curious about a Duration type #12

Open
erlandsona opened this issue Jun 1, 2020 · 2 comments
Open

Curious about a Duration type #12

erlandsona opened this issue Jun 1, 2020 · 2 comments

Comments

@erlandsona
Copy link

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
Copy link
Owner

mobily commented Jun 3, 2020

@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
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants