--
-- time_interval__copy/2
--
create or replace function time_interval__copy(
  copy__interval_id integer,
  copy__offset_interval interval
) returns int4 as $$

DECLARE
       interval_row           time_intervals%ROWTYPE;
       v_foo                 timestamptz;
BEGIN
       select * into interval_row
       from   time_intervals
       where  interval_id = copy__interval_id;

       return time_interval__new(
                  (interval_row.start_date ::timestamp + copy__offset_interval) :: timestamptz,
                  (interval_row.end_date ::timestamp + copy__offset_interval) :: timestamptz
                  );

END;
$$ language plpgsql;


--
-- time_interval__copy/1
--
create or replace function time_interval__copy(
  integer
) returns int4 as $$
	-- return time_intervals.interval_id%TYPE
declare    
       copy__interval_id     alias for $1;
       v_query		     varchar;
       v_result		     time_intervals.interval_id%TYPE;
       rec_datecalc	     record;
begin
	return time_interval__copy(
	           copy__interval_id,
		   interval '0 days'
		   );

end;$$ language plpgsql;


--
-- time_interval__copy/2
--
create or replace function time_interval__copy(
  integer,
  integer
) returns int4 as $$
	-- time_intervals.interval_id%TYPE
declare    
       copy__interval_id     alias for $1;
       copy__offset          alias for $2; -- default 0
begin

       return time_interval__copy(
	          copy__interval_id,
		  to_interval(copy__offset,'days')
		  );

end;$$ language plpgsql;