Link to home
Start Free TrialLog in
Avatar of daniel_spiri
daniel_spiri

asked on

Time conversion to Metric

Hello Experts!

I am looking for this Postgres function that will take a "timestamp without time zone" value and convert it to metric time.

for example:
time                             metric time [hh.mm]
19 days 06:17:00      462.28
155:05:00                     155.08
Avatar of borgunit
borgunit
Flag of United States of America image

Avatar of TommySzalapski
You could extract the epoch (which does second) and convert
EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours')/3600;
This would give hours since 60*60 = 3600
do you mean interval instead of timestamp ?
select round(cast ( extract(epoch from interval '19 days 06:17:00')as numeric )/3600.0, 2);
Avatar of daniel_spiri
daniel_spiri

ASKER

Earthman2,
When I run the query:

select round(cast ( extract(epoch from interval '19 days 06:17:00')as numeric )/3600.0, 2);

Result: 462.28

The minutes don't seem right.

The query on '19 days 06:01:00' returns 462.02.   (but it should be 462.01)
ASKER CERTIFIED SOLUTION
Avatar of earth man2
earth man2
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, can you please help me in transforming this into a function?
create or replace function interval2dec( interval )returns text as $$
select div(cast (extract( epoch from $1)as integer),3600)::text || ':' || extract( minute from $1);
$$ language SQL;

select interval2dec( '19 days 06:17:00'::interval);