interval question

Started by Fernando P. Schapachnikabout 25 years ago6 messagesgeneral
Jump to latest
#1Fernando P. Schapachnik
fernando@schapachnik.com.ar

Hello:
Maybe somebody on the list can give me a hand with this. I
have:

id serial,
start time,
duration time

(That is, ids have an start time and a duration).

I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

The problem is that start+duration is an interval, and I
can't find the way to cast now to interval or interval to time or
tinterval in order to make the comparison.

Any ideas?

Thanks in advance.

Fernando P. Schapachnik
fernando@schapachnik.com.ar

#2Eric G. Miller
egm2@jps.net
In reply to: Fernando P. Schapachnik (#1)
Re: interval question

On Mon, Mar 05, 2001 at 09:59:40PM -0300, Fernando P. Schapachnik wrote:

Hello:
Maybe somebody on the list can give me a hand with this. I
have:

id serial,
start time,
duration time

(That is, ids have an start time and a duration).

I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

The problem is that start+duration is an interval, and I
can't find the way to cast now to interval or interval to time or
tinterval in order to make the comparison.

Maybe something like:

CREATE start_stop (
id SERIAL,
start DATETIME,
stop DATETIME
);

INSERT INTO start_stop (start, stop) VALUES (now(), now() + '1:00');

SELECT id from start_stop WHERE now() BETWEEN start AND stop;

--
Eric G. Miller <egm2@jps.net>

#3Stefan Waidele jun.
St.Waidele.jun@Krone-Neuenburg.de
In reply to: Fernando P. Schapachnik (#1)
Re: interval question

At 21:59 05.03.2001 -0300, Fernando P. Schapachnik wrote:

[...]
I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

The problem is that start+duration is an interval, and I
can't find the way to cast now to interval or interval to time or
tinterval in order to make the comparison.

Any ideas?

Yes, maybe...

'0:00'+start <= '0:00'+now <= start+duration

It is not really beautiful, and I have not tested it, but if it works: The
are no awards for beauty, are there?

Stefan

#4Richard Huxton
dev@archonet.com
In reply to: Fernando P. Schapachnik (#1)
Re: interval question

On 3/6/01, 12:59:40 AM, Fernando "P." Schapachnik
<fernando@schapachnik.com.ar> wrote regarding [GENERAL] interval question:

Hello:
Maybe somebody on the list can give me a hand with this. I
have:
id serial,
start time,
duration time

Surely "duration" should be of type interval.
Also - are you sure "start" should be time and not timestamp - depends if
you don't care about dates.

(That is, ids have an start time and a duration).

I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

SELECT id FROM foo WHERE start<=now() AND now<=(start + duration);

The problem is that start+duration is an interval, and I
can't find the way to cast now to interval or interval to time or
tinterval in order to make the comparison.

The cast should happen automatically. If not, you can do things like:

select now() + '10:00'::interval;
select '2001-03-13'::timestamp;

to convert into interval or a timestamp.

#5Fernando P. Schapachnik
fernando@schapachnik.com.ar
In reply to: Stefan Waidele jun. (#3)
Re: Re: interval question

En un mensaje anterior Stefan Waidele jun. escribi�:

At 21:59 05.03.2001 -0300, Fernando P. Schapachnik wrote:

[...]
I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

The problem is that start+duration is an interval, and I
can't find the way to cast now to interval or interval to time or
tinterval in order to make the comparison.

Any ideas?

Yes, maybe...

'0:00'+start <= '0:00'+now <= start+duration

When I do:

select * from table where '0:00'::time+'now'<=start+duration

I lost conection to the backend. Why?

Fernando P. Schapachnik
fernando@schapachnik.com.ar

#6Fernando P. Schapachnik
fernando@schapachnik.com.ar
In reply to: Richard Huxton (#4)
Re: interval question

En un mensaje anterior dev@archonet.com escribi�:

On 3/6/01, 12:59:40 AM, Fernando "P." Schapachnik
<fernando@schapachnik.com.ar> wrote regarding [GENERAL] interval question:

Hello:
Maybe somebody on the list can give me a hand with this. I
have:
id serial,
start time,
duration time

Surely "duration" should be of type interval.
Also - are you sure "start" should be time and not timestamp - depends if
you don't care about dates.

(That is, ids have an start time and a duration).

I want to know which ids are current, ie, which satisfy
start<=now<=start+duration.

SELECT id FROM foo WHERE start<=now() AND now<=(start + duration);

It worked fine! Thanks!

Fernando P. Schapachnik
fernando@schapachnik.com.ar