Converting time interval to double precision of time unit

From: Mike Toews <mwtoews(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Converting time interval to double precision of time unit
Date: 2010-03-30 16:29:52
Message-ID: 22f0a52d1003300929w37da164fk20e0f0bf87bc33f4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm using 8.3, and I'm trying to work with the interval type, and I
can't seem to get things right. I've been all over the docs[1,2], and
there is no mention on how this can be done.

While I can get:
SELECT '3 day 2 hour 34 minute'::interval

.. how can then get the fractional hours of this time interval in
double precision (or seconds, minutes, years, decades, etc.)?

Do I really need to extract the time subcomponents and do the math myself?

For example:
SELECT extract(day from interval)*24 + extract(hour from interval) +
extract(minute from interval)/60 as hours
FROM (SELECT '3 day 2 hour 34 minute'::interval) AS foo;

This seem like a bad hack, and I can't believe a function doesn't
already exist to properly cast a time interval to a fractional unit of
time, so I thought I'd check up to see if there is a better solution.

Thanks,

-Mike

[1] http://www.postgresql.org/docs/8.3/static/functions-datetime.html
[2] http://www.postgresql.org/docs/8.3/static/datatype-datetime.html

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andy Colson 2010-03-30 16:40:48 Re: User action accounting
Previous Message Merlin Moncure 2010-03-30 16:24:45 Re: Dblink vs calling a function that returns void