pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-sql@postgresql.org
Subject: Function overlaps_interval
Date: Thu, 12 Apr 2001 11:23:28 -0700
Message-ID: <web-37366@davinci.ethosmedia.com> (raw)

Folks,

	I wrote this PL/pgSQL function for my current project, and thought it
would be generally useful.  An expansion of the builtin
overlaps(dt1,dt2,dt3,dt4) function, this function returns the interval
of time for which the two datetime ranges overlap.

	Roberto, please include this in your online PL/pgSQL function library.

CREATE FUNCTION overlap_interval(DATETIME, DATETIME, DATETIME, DATETIME)
RETURNS INTERVAL AS '
DECLARE
	begin1 ALIAS for $1;
	end1 ALIAS for $2;
	begin2 ALIAS for $3;
	end2 ALIAS for $4;
	overlap_amount INTERVAL;
BEGIN
	--test for overlap using the ovelap function.
	--if not found, return 0 interval.

	IF NOT overlaps(begin1, end1, begin2, end2) THEN
		RETURN ''00:00:00''::INTERVAL;
	END IF;

	--otherwise, test for the various forms of overlap

	IF begin1 < begin2 THEN
		IF end1 < end2 THEN
			overlap_amount := end1 - begin2;
		ELSE
			overlap_amount := end2 - begin2;
		END IF;
	ELSE
		IF end1 < end2 THEN
			overlap_amount := end1 - begin1;
		ELSE
			overlap_amount := end2 - begin1;
		END IF;
	END IF;

	RETURN overlap_amount;
END;'
LANGUAGE 'plpgsql';
			
-Josh Berkus


______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



view thread (2+ messages)  latest in thread

Message-ID: <web-37366@davinci.ethosmedia.com>
Permalink:  ../web-37366@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-37366@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com
  Subject: Re: Function overlaps_interval
  In-Reply-To: <web-37366@davinci.ethosmedia.com>
  References: <web-37366@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox