1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* date.c
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* implements DATE and TIME data types specified in SQL-92 standard
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* Portions Copyright (c) 1994-5, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-01-24 20:43:33 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.55 2001/01/24 19:43:13 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <float.h>
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
#include "access/hash.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "utils/date.h"
|
|
|
|
#include "utils/nabstime.h"
|
1997-03-15 00:21:12 +01:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* Date ADT
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
|
|
|
/* date_in()
|
|
|
|
* Given date text string, convert to internal date format.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_in(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
DateADT date;
|
|
|
|
double fsec;
|
1997-09-08 04:41:22 +02:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int tzp;
|
1997-09-08 04:41:22 +02:00
|
|
|
int dtype;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int nf;
|
1997-09-08 04:41:22 +02:00
|
|
|
char *field[MAXDATEFIELDS];
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int ftype[MAXDATEFIELDS];
|
1997-09-08 04:41:22 +02:00
|
|
|
char lowstr[MAXDATELEN + 1];
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
2000-06-09 03:11:16 +02:00
|
|
|
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp) != 0))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Bad date external representation '%s'", str);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (dtype)
|
|
|
|
{
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
case DTK_DATE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_CURRENT:
|
|
|
|
GetCurrentTime(tm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EPOCH:
|
|
|
|
tm->tm_year = 1970;
|
|
|
|
tm->tm_mon = 1;
|
|
|
|
tm->tm_mday = 1;
|
|
|
|
break;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Unrecognized date external representation '%s'", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-03-15 00:21:12 +01:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
date = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(date);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* date_out()
|
|
|
|
* Given internal format date, convert to text string.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_out(PG_FUNCTION_ARGS)
|
1997-07-29 17:54:49 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
char buf[MAXDATELEN + 1];
|
1997-07-29 17:54:49 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
j2date((date + date2j(2000, 1, 1)),
|
|
|
|
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
1997-07-29 17:54:49 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
EncodeDateOnly(tm, DateStyle, buf);
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(dateVal1 == dateVal2);
|
|
|
|
}
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_ne(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 != dateVal2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_lt(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 < dateVal2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_le(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(dateVal1 <= dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_gt(PG_FUNCTION_ARGS)
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 > dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_ge(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 >= dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_cmp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
if (dateVal1 < dateVal2)
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INT32(-1);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
else if (dateVal1 > dateVal2)
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INT32(1);
|
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_larger(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_smaller(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Compute difference between two dates in days.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_mi(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Add a number of days to a date, giving a new date.
|
|
|
|
* Must handle both positive and negative numbers of days.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_pli(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
int32 days = PG_GETARG_INT32(1);
|
|
|
|
|
|
|
|
PG_RETURN_DATEADT(dateVal + days);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Subtract a number of days from a date, giving a new date.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_mii(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
int32 days = PG_GETARG_INT32(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(dateVal - days);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* date_timestamp()
|
|
|
|
* Convert date to timestamp data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_timestamp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp result;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
2000-09-12 07:41:37 +02:00
|
|
|
time_t utime;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-09-12 07:41:37 +02:00
|
|
|
j2date((dateVal + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-09-12 07:41:37 +02:00
|
|
|
if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
|
|
|
|
{
|
2000-10-29 14:17:34 +01:00
|
|
|
#if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
|
2000-09-12 07:41:37 +02:00
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_min = 0;
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
tm->tm_isdst = -1;
|
|
|
|
|
|
|
|
tm->tm_year -= 1900;
|
|
|
|
tm->tm_mon -= 1;
|
|
|
|
utime = mktime(tm);
|
|
|
|
if (utime == -1)
|
|
|
|
elog(ERROR, "Unable to convert date to tm");
|
|
|
|
|
|
|
|
result = utime + ((date2j(1970,1,1)-date2j(2000,1,1))*86400.0);
|
2000-10-29 14:17:34 +01:00
|
|
|
#else
|
2000-09-12 07:41:37 +02:00
|
|
|
result = dateVal*86400.0+CTimeZone;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Outside of range for timezone support, so assume UTC */
|
|
|
|
result = dateVal*86400.0;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* timestamp_date()
|
|
|
|
* Convert timestamp to date data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_date(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
DateADT result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_IS_EPOCH(timestamp))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
}
|
2000-06-09 03:11:16 +02:00
|
|
|
else if (TIMESTAMP_IS_CURRENT(timestamp))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
timestamp2tm(SetTimestamp(timestamp), &tz, tm, &fsec, &tzn);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(result);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* abstime_date()
|
|
|
|
* Convert abstime to date data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
abstime_date(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
DateADT result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
switch (abstime)
|
|
|
|
{
|
|
|
|
case INVALID_ABSTIME:
|
|
|
|
case NOSTART_ABSTIME:
|
|
|
|
case NOEND_ABSTIME:
|
|
|
|
elog(ERROR, "Unable to convert reserved abstime value to date");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pretend to drop through to make compiler think that result
|
|
|
|
* will be set
|
|
|
|
*/
|
|
|
|
|
|
|
|
case EPOCH_ABSTIME:
|
|
|
|
result = date2j(1970, 1, 1) - date2j(2000, 1, 1);
|
|
|
|
break;
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
case CURRENT_ABSTIME:
|
|
|
|
GetCurrentTime(tm);
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
|
|
|
break;
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
default:
|
|
|
|
abstime2tm(abstime, &tz, tm, NULL);
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(result);
|
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
|
|
|
|
2000-11-11 20:55:19 +01:00
|
|
|
/* date_text()
|
|
|
|
* Convert date to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Date, but may as well leave it in Datum form */
|
|
|
|
Datum date = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(date_out, date));
|
|
|
|
|
|
|
|
len = (strlen(str) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_date()
|
|
|
|
* Convert text string to date.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
|
|
|
elog(ERROR, "Bad date external representation (too long)");
|
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
return DirectFunctionCall1(date_in,
|
|
|
|
CStringGetDatum(dstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Time ADT
|
|
|
|
*****************************************************************************/
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_in(PG_FUNCTION_ARGS)
|
1997-09-20 18:17:45 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
|
|
|
TimeADT time;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int nf;
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int dtype;
|
|
|
|
int ftype[MAXDATEFIELDS];
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
2000-04-12 19:17:23 +02:00
|
|
|
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, NULL) != 0))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Bad time external representation '%s'", str);
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMEADT(time);
|
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_out(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
char buf[MAXDATELEN + 1];
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
tm->tm_hour = (time / (60 * 60));
|
|
|
|
tm->tm_min = (((int) (time / 60)) % 60);
|
|
|
|
tm->tm_sec = (((int) time) % 60);
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
fsec = 0;
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
EncodeTimeOnly(tm, fsec, NULL, DateStyle, buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 == time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_ne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 != time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_lt(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 < time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_le(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 <= time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_gt(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 > time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_ge(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 >= time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_cmp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (time1 < time2)
|
|
|
|
PG_RETURN_INT32(-1);
|
|
|
|
if (time1 > time2)
|
|
|
|
PG_RETURN_INT32(1);
|
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_larger(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_smaller(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* overlaps_time() --- implements the SQL92 OVERLAPS operator.
|
|
|
|
*
|
|
|
|
* Algorithm is per SQL92 spec. This is much harder than you'd think
|
|
|
|
* because the spec requires us to deliver a non-null answer in some cases
|
|
|
|
* where some of the inputs are null.
|
2000-03-15 00:06:59 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
overlaps_time(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-12-07 19:38:59 +01:00
|
|
|
/* The arguments are TimeADT, but we leave them as generic Datums
|
|
|
|
* to avoid dereferencing nulls (TimeADT is pass-by-reference!)
|
|
|
|
*/
|
|
|
|
Datum ts1 = PG_GETARG_DATUM(0);
|
|
|
|
Datum te1 = PG_GETARG_DATUM(1);
|
|
|
|
Datum ts2 = PG_GETARG_DATUM(2);
|
|
|
|
Datum te2 = PG_GETARG_DATUM(3);
|
|
|
|
bool ts1IsNull = PG_ARGISNULL(0);
|
|
|
|
bool te1IsNull = PG_ARGISNULL(1);
|
|
|
|
bool ts2IsNull = PG_ARGISNULL(2);
|
|
|
|
bool te2IsNull = PG_ARGISNULL(3);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
#define TIMEADT_GT(t1,t2) \
|
|
|
|
(DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
|
|
|
|
#define TIMEADT_LT(t1,t2) \
|
|
|
|
(DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* If both endpoints of interval 1 are null, the result is null (unknown).
|
|
|
|
* If just one endpoint is null, take ts1 as the non-null one.
|
|
|
|
* Otherwise, take ts1 as the lesser endpoint.
|
|
|
|
*/
|
|
|
|
if (ts1IsNull)
|
|
|
|
{
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts1 = te1;
|
2000-12-07 19:38:59 +01:00
|
|
|
te1IsNull = true;
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
2000-12-07 19:38:59 +01:00
|
|
|
else if (!te1IsNull)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-12-07 19:38:59 +01:00
|
|
|
if (TIMEADT_GT(ts1, te1))
|
|
|
|
{
|
|
|
|
Datum tt = ts1;
|
|
|
|
|
|
|
|
ts1 = te1;
|
|
|
|
te1 = tt;
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* Likewise for interval 2. */
|
|
|
|
if (ts2IsNull)
|
|
|
|
{
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts2 = te2;
|
2000-12-07 19:38:59 +01:00
|
|
|
te2IsNull = true;
|
|
|
|
}
|
|
|
|
else if (!te2IsNull)
|
|
|
|
{
|
|
|
|
if (TIMEADT_GT(ts2, te2))
|
|
|
|
{
|
|
|
|
Datum tt = ts2;
|
|
|
|
|
|
|
|
ts2 = te2;
|
|
|
|
te2 = tt;
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* At this point neither ts1 nor ts2 is null, so we can consider three
|
|
|
|
* cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
|
|
|
|
*/
|
|
|
|
if (TIMEADT_GT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts1 < te2 OR te1 < te2, which may look redundant
|
|
|
|
* but in the presence of nulls it's not quite completely so.
|
|
|
|
*/
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMEADT_LT(ts1, te2))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* If te1 is not null then we had ts1 <= te1 above, and we just
|
|
|
|
* found ts1 >= te2, hence te1 >= te2.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else if (TIMEADT_LT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts2 < te1 OR te2 < te1 */
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMEADT_LT(ts2, te1))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* If te2 is not null then we had ts2 <= te2 above, and we just
|
|
|
|
* found ts2 >= te1, hence te2 >= te1.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
|
|
|
|
* rather silly way of saying "true if both are nonnull, else null".
|
|
|
|
*/
|
|
|
|
if (te1IsNull || te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TIMEADT_GT
|
|
|
|
#undef TIMEADT_LT
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* timestamp_time()
|
|
|
|
* Convert timestamp to time data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_time(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
|
|
|
TimeADT result;
|
1997-09-08 04:41:22 +02:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int tz;
|
1997-09-08 04:41:22 +02:00
|
|
|
double fsec;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char *tzn;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_IS_EPOCH(timestamp))
|
|
|
|
timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
|
|
|
|
else if (TIMESTAMP_IS_CURRENT(timestamp))
|
|
|
|
timestamp2tm(SetTimestamp(timestamp), &tz, tm, &fsec, &tzn);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
else
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* datetime_timestamp()
|
|
|
|
* Convert date and time to timestamp data type.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
datetime_timestamp(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(1);
|
|
|
|
Timestamp result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
|
|
|
|
DateADTGetDatum(date)));
|
|
|
|
result += time;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
/* time_interval()
|
|
|
|
* Convert time to interval data type.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_interval(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
2000-03-15 00:06:59 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result->time = time;
|
2000-03-15 00:06:59 +01:00
|
|
|
result->month = 0;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
/* interval_time()
|
|
|
|
* Convert interval to time data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
interval_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(0);
|
|
|
|
TimeADT result;
|
|
|
|
Interval span1;
|
|
|
|
|
|
|
|
result = span->time;
|
|
|
|
TMODULO(result, span1.time, 86400e0);
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time_pl_interval()
|
|
|
|
* Add interval to time.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_pl_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeADT result;
|
|
|
|
TimeADT time1;
|
|
|
|
|
|
|
|
result = (time + span->time);
|
|
|
|
TMODULO(result, time1, 86400e0);
|
|
|
|
if (result < 0)
|
|
|
|
result += 86400;
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time_mi_interval()
|
|
|
|
* Subtract interval from time.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_mi_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeADT result;
|
|
|
|
TimeADT time1;
|
|
|
|
|
|
|
|
result = (time - span->time);
|
|
|
|
TMODULO(result, time1, 86400e0);
|
|
|
|
if (result < 0)
|
|
|
|
result += 86400;
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* interval_pl_time()
|
|
|
|
* Add time to interval.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
interval_pl_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Datum span = PG_GETARG_DATUM(0);
|
|
|
|
Datum time = PG_GETARG_DATUM(1);
|
|
|
|
|
|
|
|
return DirectFunctionCall2(time_pl_interval, time, span);
|
|
|
|
}
|
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-11-11 20:55:19 +01:00
|
|
|
/* time_text()
|
|
|
|
* Convert time to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Time, but may as well leave it in Datum form */
|
|
|
|
Datum time = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(time_out, time));
|
|
|
|
|
|
|
|
len = (strlen(str) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_time()
|
|
|
|
* Convert text string to time.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
|
|
|
elog(ERROR, "Bad time external representation (too long)");
|
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
return DirectFunctionCall1(time_in,
|
|
|
|
CStringGetDatum(dstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Time With Time Zone ADT
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_in(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2000-03-15 00:06:59 +01:00
|
|
|
TimeTzADT *time;
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
int nf;
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int dtype;
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
|
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
2000-04-12 19:17:23 +02:00
|
|
|
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
|
2000-03-15 00:06:59 +01:00
|
|
|
elog(ERROR, "Bad time external representation '%s'", str);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
time = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
time->time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
|
|
|
time->zone = tz;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMETZADT_P(time);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_out(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
2000-03-15 00:06:59 +01:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
int tz;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
tm->tm_hour = (time->time / (60 * 60));
|
|
|
|
tm->tm_min = (((int) (time->time / 60)) % 60);
|
|
|
|
tm->tm_sec = (((int) time->time) % 60);
|
|
|
|
|
|
|
|
fsec = 0;
|
|
|
|
tz = time->zone;
|
|
|
|
|
|
|
|
EncodeTimeOnly(tm, fsec, &tz, DateStyle, buf);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_eq(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) == (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_ne(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) != (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_lt(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) < (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_le(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) <= (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_gt(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) > (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_ge(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(((time1->time+time1->zone) >= (time2->time+time2->zone)));
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
|
|
|
|
if (DatumGetBool(DirectFunctionCall2(timetz_lt,
|
|
|
|
TimeTzADTPGetDatum(time1),
|
|
|
|
TimeTzADTPGetDatum(time2))))
|
|
|
|
PG_RETURN_INT32(-1);
|
|
|
|
if (DatumGetBool(DirectFunctionCall2(timetz_gt,
|
|
|
|
TimeTzADTPGetDatum(time1),
|
|
|
|
TimeTzADTPGetDatum(time2))))
|
|
|
|
PG_RETURN_INT32(1);
|
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
/*
|
|
|
|
* timetz, being an unusual size, needs a specialized hash function.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_hash(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *key = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specify hash length as sizeof(double) + sizeof(int4), not as
|
|
|
|
* sizeof(TimeTzADT), so that any garbage pad bytes in the structure
|
|
|
|
* won't be included in the hash!
|
|
|
|
*/
|
|
|
|
return hash_any((char *) key, sizeof(double) + sizeof(int4));
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_larger(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
|
|
|
|
if (DatumGetBool(DirectFunctionCall2(timetz_gt,
|
|
|
|
TimeTzADTPGetDatum(time1),
|
|
|
|
TimeTzADTPGetDatum(time2))))
|
|
|
|
PG_RETURN_TIMETZADT_P(time1);
|
|
|
|
PG_RETURN_TIMETZADT_P(time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_smaller(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
|
|
|
|
if (DatumGetBool(DirectFunctionCall2(timetz_lt,
|
|
|
|
TimeTzADTPGetDatum(time1),
|
|
|
|
TimeTzADTPGetDatum(time2))))
|
|
|
|
PG_RETURN_TIMETZADT_P(time1);
|
|
|
|
PG_RETURN_TIMETZADT_P(time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
/* timetz_pl_interval()
|
|
|
|
* Add interval to timetz.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_pl_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeTzADT *result;
|
|
|
|
TimeTzADT time1;
|
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
|
|
|
result->time = (time->time + span->time);
|
|
|
|
TMODULO(result->time, time1.time, 86400e0);
|
|
|
|
if (result->time < 0)
|
|
|
|
result->time += 86400;
|
|
|
|
result->zone = time->zone;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timetz_mi_interval()
|
|
|
|
* Subtract interval from timetz.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_mi_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeTzADT *result;
|
|
|
|
TimeTzADT time1;
|
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
|
|
|
result->time = (time->time - span->time);
|
|
|
|
TMODULO(result->time, time1.time, 86400e0);
|
|
|
|
if (result->time < 0)
|
|
|
|
result->time += 86400;
|
|
|
|
result->zone = time->zone;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* overlaps_timetz() --- implements the SQL92 OVERLAPS operator.
|
|
|
|
*
|
|
|
|
* Algorithm is per SQL92 spec. This is much harder than you'd think
|
|
|
|
* because the spec requires us to deliver a non-null answer in some cases
|
|
|
|
* where some of the inputs are null.
|
2000-03-15 00:06:59 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
overlaps_timetz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* The arguments are TimeTzADT *, but we leave them as generic Datums
|
2000-12-07 19:38:59 +01:00
|
|
|
* for convenience of notation --- and to avoid dereferencing nulls.
|
2000-06-09 03:11:16 +02:00
|
|
|
*/
|
|
|
|
Datum ts1 = PG_GETARG_DATUM(0);
|
|
|
|
Datum te1 = PG_GETARG_DATUM(1);
|
|
|
|
Datum ts2 = PG_GETARG_DATUM(2);
|
|
|
|
Datum te2 = PG_GETARG_DATUM(3);
|
2000-12-07 19:38:59 +01:00
|
|
|
bool ts1IsNull = PG_ARGISNULL(0);
|
|
|
|
bool te1IsNull = PG_ARGISNULL(1);
|
|
|
|
bool ts2IsNull = PG_ARGISNULL(2);
|
|
|
|
bool te2IsNull = PG_ARGISNULL(3);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
#define TIMETZ_GT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
|
|
|
|
#define TIMETZ_LT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* If both endpoints of interval 1 are null, the result is null (unknown).
|
|
|
|
* If just one endpoint is null, take ts1 as the non-null one.
|
|
|
|
* Otherwise, take ts1 as the lesser endpoint.
|
|
|
|
*/
|
|
|
|
if (ts1IsNull)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-12-07 19:38:59 +01:00
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts1 = te1;
|
2000-12-07 19:38:59 +01:00
|
|
|
te1IsNull = true;
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
2000-12-07 19:38:59 +01:00
|
|
|
else if (!te1IsNull)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-12-07 19:38:59 +01:00
|
|
|
if (TIMETZ_GT(ts1, te1))
|
|
|
|
{
|
|
|
|
Datum tt = ts1;
|
|
|
|
|
|
|
|
ts1 = te1;
|
|
|
|
te1 = tt;
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* Likewise for interval 2. */
|
|
|
|
if (ts2IsNull)
|
|
|
|
{
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts2 = te2;
|
2000-12-07 19:38:59 +01:00
|
|
|
te2IsNull = true;
|
|
|
|
}
|
|
|
|
else if (!te2IsNull)
|
|
|
|
{
|
|
|
|
if (TIMETZ_GT(ts2, te2))
|
|
|
|
{
|
|
|
|
Datum tt = ts2;
|
|
|
|
|
|
|
|
ts2 = te2;
|
|
|
|
te2 = tt;
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* At this point neither ts1 nor ts2 is null, so we can consider three
|
|
|
|
* cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
|
|
|
|
*/
|
|
|
|
if (TIMETZ_GT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts1 < te2 OR te1 < te2, which may look redundant
|
|
|
|
* but in the presence of nulls it's not quite completely so.
|
|
|
|
*/
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMETZ_LT(ts1, te2))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* If te1 is not null then we had ts1 <= te1 above, and we just
|
|
|
|
* found ts1 >= te2, hence te1 >= te2.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else if (TIMETZ_LT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts2 < te1 OR te2 < te1 */
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMETZ_LT(ts2, te1))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* If te2 is not null then we had ts2 <= te2 above, and we just
|
|
|
|
* found ts2 >= te1, hence te2 >= te1.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
|
|
|
|
* rather silly way of saying "true if both are nonnull, else null".
|
|
|
|
*/
|
|
|
|
if (te1IsNull || te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
#undef TIMETZ_GT
|
|
|
|
#undef TIMETZ_LT
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
/* timestamp_timetz()
|
|
|
|
* Convert timestamp to timetz data type.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_timetz(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
2000-04-12 19:17:23 +02:00
|
|
|
TimeTzADT *result;
|
2000-03-15 00:06:59 +01:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2000-03-15 00:06:59 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_IS_EPOCH(timestamp))
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
timestamp2tm(SetTimestamp(timestamp), NULL, tm, &fsec, NULL);
|
2000-03-15 00:06:59 +01:00
|
|
|
tz = 0;
|
|
|
|
}
|
2000-06-09 03:11:16 +02:00
|
|
|
else if (TIMESTAMP_IS_CURRENT(timestamp))
|
|
|
|
timestamp2tm(SetTimestamp(timestamp), &tz, tm, &fsec, &tzn);
|
2000-03-15 00:06:59 +01:00
|
|
|
else
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
2000-03-15 00:06:59 +01:00
|
|
|
elog(ERROR, "Unable to convert timestamp to date");
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
result->time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
|
|
|
result->zone = tz;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* datetimetz_timestamp()
|
|
|
|
* Convert date and timetz to timestamp data type.
|
|
|
|
* Timestamp is stored in GMT, so add the time zone
|
|
|
|
* stored with the timetz to the result.
|
|
|
|
* - thomas 2000-03-10
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
datetimetz_timestamp(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
Timestamp result;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-09-12 07:41:37 +02:00
|
|
|
result = date*86400.0 + time->time + time->zone;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* timetz_text()
|
|
|
|
* Convert timetz to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Timetz, but may as well leave it in Datum form */
|
|
|
|
Datum timetz = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(timetz_out, timetz));
|
|
|
|
|
|
|
|
len = (strlen(str) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_timetz()
|
|
|
|
* Convert text string to timetz.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_timetz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
|
|
|
elog(ERROR, "Bad timetz external representation (too long)");
|
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
return DirectFunctionCall1(timetz_in,
|
|
|
|
CStringGetDatum(dstr));
|
|
|
|
}
|