Re: Time Intervals

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Michael Richards" <michael(at)fastmail(dot)ca>
Cc: jason(dot)earl(at)simplot(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: Time Intervals
Date: 2002-02-13 21:50:55
Message-ID: 883.1013637055@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Michael Richards" <michael(at)fastmail(dot)ca> writes:
> The only solution I can think of is if I can convert a date into a
> number of some sort and then just use normal math on it. I really
> need:

> | now - then |
> | ---------- | * (period+1) + then
> |_ period _|

Are you *sure* that's what you want? You can certainly do it that way
--- extract(epoch from timestamp), do math, convert back --- but the
above only works if the "period" is a constant number of seconds.
Intervals like "1 month" cannot be handled as above. Less obviously,
intervals like "1 day" cannot be handled that way either (think about
daylight savings transitions).

There was a thread on this same topic just recently, and I think the
conclusion was that the cleanest way to handle real-world interval
definitions is to rely on a loop around a timestamp + interval addition
operator:

while tstamp < now do
tstamp := tstamp + interval;

This is trivial to program in a plpgsql function, for example, and it's
quite cheap as long as you don't let too many periods elapse between
updates.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Bax 2002-02-14 02:12:45 Re: How long does it take?
Previous Message Tom Lane 2002-02-13 21:43:07 Re: How long does it take?