rounding a timestamp to nearest x seconds - Mailing list pgsql-general

From Andy Colson
Subject rounding a timestamp to nearest x seconds
Date
Msg-id 4F577A32.60001@squeakycode.net
Whole thread Raw
Responses Re: rounding a timestamp to nearest x seconds
List pgsql-general
Took me a while to figure this out, thought I'd paste it here for others
to use:

create or replace function round_timestamp(timestamp, integer) returns
timestamp as $$
    select date_trunc('minute', $1) + cast(round(date_part('seconds',
$1)/$2)*$2 || ' seconds' as interval);
$$ language sql immutable;


If you pass 10 to the second argument, it'll round the timestamp to the
nearest 10 seconds.  Pass 5 to round to nearest 5 seconds, etc..

-Andy

pgsql-general by date:

Previous
From: Gary Chambers
Date:
Subject: Re: Fixing the loss of 'template1'
Next
From: Adrian Klaver
Date:
Subject: Re: Fixing the loss of 'template1'