create or replace function do_times_overlap(
  start_1 timestamp with time_zone,
  end_1 timestamp with time_zone,
  start_2 timestamp with time_zone,
  end_2 timestamp with time_zone
) returns boolean as $$
begin
  if end_1 < start_1 then
    raise exception 'Zakoczenie pierwszej emisji nastpuje przed jej rozpoczciem.';
  end if;
  if end_2 < start_2 then
    raise exception 'Zakoczenie drugiej emisji nastpuje przed jej rozpoczciem.';
  end if;
  if start_1 = end_2 or start_2 = end_1 then
    return false;
  else
    return (start_1 between start_2 and end_2)
        or (end_1 between start_2 and end_2)
        or (start_2 between start_1 and end_1)
        or (end_2 between start_1 and end_1);
  end if;
end
## language plpgSQL;


