-- datetime_functions.sql --
--
-- SQL code to define the new date and time functions and operators
--
-- Copyright (c) 1998, Massimo Dal Zotto <dz@cs.unitn.it>
--
-- This file is distributed under the GNU General Public License
-- either version 2, or (at your option) any later version.

-- Define the new time functions.
--
create function hhmm_in(opaque) returns time
  as 'MODULE_PATHNAME' 
  language 'c';

create function hhmm_out(opaque) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function hhmm(time) returns time
  as 'MODULE_PATHNAME' 
  language 'c';

create function time_difference(time,time) returns time
  as 'MODULE_PATHNAME' 
  language 'c';

create function time_hours(time) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function time_minutes(time) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function time_seconds(time) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function as_minutes(time) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function as_seconds(time) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function date_day(date) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function date_month(date) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function date_year(date) returns int4
  as 'MODULE_PATHNAME' 
  language 'c';

create function currenttime() returns time
  as 'MODULE_PATHNAME' 
  language 'c';

create function currentdate() returns date
  as 'MODULE_PATHNAME' 
  language 'c';

-- Define new operator - for time.
--
create operator - (
  leftarg=time, 
  rightarg=time, 
  procedure=time_difference);

-- Define functions to switch from time to hhmm representation.
--
--   select hhmm_mode();
--   select time_mode();
--
create function hhmm_mode() returns text
  as 'update pg_type set typinput =''hhmm_in''  where typname=''time'';
      update pg_type set typoutput=''hhmm_out'' where typname=''time''
      select ''hhmm_mode''::text'
  language 'sql';

create function time_mode() returns text
  as 'update pg_type set typinput =''time_in''  where typname=''time'';
      update pg_type set typoutput=''time_out'' where typname=''time''
      select ''time_mode''::text'
  language 'sql';

-- Use these to do the updates manually
--
-- update pg_type set typinput ='hhmm_in'  where typname='time';
-- update pg_type set typoutput='hhmm_out' where typname='time';
--
-- update pg_type set typinput ='time_in'  where typname='time';
-- update pg_type set typoutput='time_out' where typname='time';

-- end of file
