*** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** *** 6653,6658 **** SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})'); --- 6653,6690 ---- + make_date + + make_date(year int, month int, + day int) + + date + + Create date from year, month and day fields + + make_date(2013, 7, 15) + 2013-07-15 + + + + + + make_time + + make_time(hour int, min int, + sec double precision) + + time + + Create time from hour, minutes and second fields + + make_time(8, 15, 23.5) + 08:15:23.5 + + + + + now now() *** a/src/backend/utils/adt/date.c --- b/src/backend/utils/adt/date.c *************** *** 2729,2731 **** timetz_izone(PG_FUNCTION_ARGS) --- 2729,2776 ---- PG_RETURN_TIMETZADT_P(result); } + + /* + * make_date() + * date constructor + */ + Datum + make_date(PG_FUNCTION_ARGS) + { + int tm_year = PG_GETARG_INT32(0); + int tm_mon = PG_GETARG_INT32(1); + int tm_mday = PG_GETARG_INT32(2); + DateADT date; + + if (!IS_VALID_JULIAN(tm_year, tm_mon, tm_mday)) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("date out of range: \"%d-%d-%d\"", tm_year, tm_mon, tm_mday))); + + date = date2j(tm_year, tm_mon, tm_mday) - POSTGRES_EPOCH_JDATE; + + PG_RETURN_DATEADT(date); + } + + /* + * make_time() + * time constructor + */ + Datum + make_time(PG_FUNCTION_ARGS) + { + int tm_hour = PG_GETARG_INT32(0); + int tm_min = PG_GETARG_INT32(1); + float8 sec = PG_GETARG_FLOAT8(2); + TimeADT time; + + #ifdef HAVE_INT64_TIMESTAMP + time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE) + sec) + * USECS_PER_SEC; + #else + time = ((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE) + sec + #endif + + PG_RETURN_TIMEADT(time); + } + *** a/src/include/catalog/pg_proc.h --- b/src/include/catalog/pg_proc.h *************** *** 4646,4651 **** DESCR("int8range constructor"); --- 4646,4657 ---- DATA(insert OID = 3946 ( int8range PGNSP PGUID 12 1 0 0 0 f f f f f f i 3 0 3926 "20 20 25" _null_ _null_ _null_ _null_ range_constructor3 _null_ _null_ _null_ )); DESCR("int8range constructor"); + /* date, time constructors */ + DATA(insert OID = 3968 ( make_date PGNSP PGUID 12 1 0 0 0 f f f f t f s 3 0 1082 "23 23 23" _null_ _null_ "{year,month,day}" _null_ make_date _null_ _null_ _null_ )); + DESCR("construct date"); + DATA(insert OID = 3969 ( make_time PGNSP PGUID 12 1 0 0 0 f f f f t f s 3 0 1083 "23 23 701" _null_ _null_ "{hour,min,sec}" _null_ make_time _null_ _null_ _null_ )); + DESCR("construct time"); + /* spgist support functions */ DATA(insert OID = 4001 ( spggettuple PGNSP PGUID 12 1 0 0 0 f f f f t f v 2 0 16 "2281 2281" _null_ _null_ _null_ _null_ spggettuple _null_ _null_ _null_ )); DESCR("spgist(internal)"); *** a/src/test/regress/expected/date.out --- b/src/test/regress/expected/date.out *************** *** 1184,1186 **** select isfinite('infinity'::date), isfinite('-infinity'::date), isfinite('today' --- 1184,1199 ---- f | f | t (1 row) + -- test constructors + select make_date(2013, 7, 15); + make_date + ------------ + 07-15-2013 + (1 row) + + select make_time(8, 20, 0.0); + make_time + ----------- + 08:20:00 + (1 row) + *** a/src/test/regress/sql/date.sql --- b/src/test/regress/sql/date.sql *************** *** 276,278 **** select 'infinity'::date, '-infinity'::date; --- 276,283 ---- select 'infinity'::date > 'today'::date as t; select '-infinity'::date < 'today'::date as t; select isfinite('infinity'::date), isfinite('-infinity'::date), isfinite('today'::date); + + + -- test constructors + select make_date(2013, 7, 15); + select make_time(8, 20, 0.0);