bendrucker / postgres-interval

Parse Postgres interval columns
MIT License
33 stars 19 forks source link

Add a toSeconds method #23

Closed gajus closed 5 years ago

gajus commented 5 years ago

I am doing a lot of round(extract(epoch from intervalField)).

It would be handy if postgres-interval parser gave this as an option.

gajus commented 5 years ago

As a quick workaround, I have used:

import parseInterval from 'postgres-interval';
import {
  parse as parseIsoDuration,
  toSeconds as durationToSeconds
} from 'iso8601-duration';

durationToSeconds(parseIsoDuration(parseInterval(value).toISOString()));
bendrucker commented 5 years ago

I'd be happy to review a PR!

gabegorelick commented 5 years ago

You should be able to pass postgres-interval objects to moment.duration. From there, you can do whatever you want with it.

I'd recommend against adding functionality like toSeconds and instead point people in that direction.

bendrucker commented 5 years ago

After reading the original post a bit more carefully, I'm inclined to agree. We're not looking to build up a complete Interval class here and returning standardized (8601) output should be good enough to interop with other tools to provide additional functionality.