Converting time to float

From: Jorge Godoy <jgodoy(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Converting time to float
Date: 2007-04-27 11:43:59
Message-ID: 874pn2xcsg.fsf@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Hi!

I am needing to convert from TIME type to floats and taking 1h as the
unit, so what I'd like is something that would allow me doing operations
like:

SELECT '00:10:00'::TIME / '1:00:00'::TIME; -- Answer is: 0.16666666666666666667
SELECT '00:30:00'::TIME / '1:00:00'::TIME; -- Answer is: 0.5
SELECT '01:10:00'::TIME / '1:00:00'::TIME; -- Answer is: 1.16666666666666666667

or

SELECT '00:10:00'::TIME / '1 hour'::INTERVAL; -- Answer is: 0.16666666666666666667
SELECT '00:30:00'::TIME / '1 hour'::INTERVAL; -- Answer is: 0.5
SELECT '01:10:00'::TIME / '1 hour'::INTERVAL; -- Answer is: 1.16666666666666666667

i.e., I want to see how many times one time value fits in another. I'll
have times with just seconds, minutes and seconds and I might have times
with hours, minutes and seconds to make this fit.

My first idea is converting each part of the time individually by diving
it by 1.0 (hours), by 60.0 (minutes) or by 3600.0 (for seconds) and then
adding it all up... Of course I won't do the division for hours and I'm
using floats here to for a float division instead of an integer
division.

Any hints or a better recipe? :-)

--
Jorge Godoy <jgodoy(at)gmail(dot)com>

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-04-27 11:47:46 Re: Vacuum-full very slow
Previous Message Gregory Stark 2007-04-27 11:35:26 Re: When the locially dropped column is also physically dropped