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 18:26:26 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* timestamp.c
|
|
|
|
* Functions for the built-in SQL92 type "timestamp" and "interval".
|
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
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 18:26:26 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-03-09 18:35:37 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.65 2002/03/09 17:35:36 tgl Exp $
|
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 18:26:26 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-05-29 03:59:17 +02:00
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
1997-04-03 21:58:11 +02:00
|
|
|
#include <ctype.h>
|
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 18:26:26 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <errno.h>
|
2000-05-29 03:59:17 +02:00
|
|
|
#include <sys/types.h>
|
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 18:26:26 +01:00
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
2000-07-13 00:59:15 +02:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
#include "access/hash.h"
|
2000-05-29 03:59:17 +02:00
|
|
|
#include "access/xact.h"
|
1997-07-01 02:22:46 +02:00
|
|
|
#include "miscadmin.h"
|
2000-07-17 05:05:41 +02:00
|
|
|
#include "utils/array.h"
|
1997-03-14 06:58:13 +01:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
static double time2t(const int hour, const int min, const double sec);
|
2000-06-09 00:38:00 +02:00
|
|
|
static int EncodeSpecialTimestamp(Timestamp dt, char *str);
|
|
|
|
static Timestamp dt2local(Timestamp dt, int timezone);
|
2001-10-03 17:50:48 +02:00
|
|
|
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod);
|
2001-10-18 19:30:21 +02:00
|
|
|
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod);
|
2001-09-28 10:09:14 +02:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* timestamp_in()
|
|
|
|
* Convert a string to internal form.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_in(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid typelem = PG_GETARG_OID(1);
|
|
|
|
#endif
|
|
|
|
int32 typmod = PG_GETARG_INT32(2);
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp result;
|
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 18:26:26 +01:00
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
int dtype;
|
|
|
|
int nf;
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
|
|
|
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
|
|
|
|
elog(ERROR, "Bad timestamp external representation '%s'", str);
|
|
|
|
|
|
|
|
switch (dtype)
|
|
|
|
{
|
|
|
|
case DTK_DATE:
|
2001-09-28 10:09:14 +02:00
|
|
|
if (tm2timestamp(tm, fsec, NULL, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP out of range '%s'", str);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EPOCH:
|
2001-09-28 10:09:14 +02:00
|
|
|
result = SetEpochTimestamp();
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_LATE:
|
2000-06-09 03:11:16 +02:00
|
|
|
TIMESTAMP_NOEND(result);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EARLY:
|
2000-06-09 03:11:16 +02:00
|
|
|
TIMESTAMP_NOBEGIN(result);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_INVALID:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP '%s' no longer supported", str);
|
2001-09-28 10:09:14 +02:00
|
|
|
TIMESTAMP_NOEND(result);
|
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 18:26:26 +01:00
|
|
|
break;
|
1997-07-01 02:22:46 +02:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP '%s' not parsed; internal coding error", str);
|
2001-09-28 10:09:14 +02:00
|
|
|
TIMESTAMP_NOEND(result);
|
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 18:26:26 +01:00
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
AdjustTimestampForTypmod(&result, typmod);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
/* timestamp_out()
|
|
|
|
* Convert a timestamp to external form.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_out(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2001-10-03 07:29:27 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
2001-09-28 10:09:14 +02:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
char *tzn = NULL;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
EncodeSpecialTimestamp(timestamp, buf);
|
|
|
|
else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
|
2001-09-28 10:09:14 +02:00
|
|
|
EncodeDateTime(tm, fsec, NULL, &tzn, DateStyle, buf);
|
|
|
|
else
|
|
|
|
elog(ERROR, "Unable to format timestamp; internal coding error");
|
|
|
|
|
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
/* timestamp_scale()
|
|
|
|
* Adjust time type for specified scale factor.
|
|
|
|
* Used by PostgreSQL type system to stuff columns.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamp_scale(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
|
|
|
int32 typmod = PG_GETARG_INT32(1);
|
|
|
|
Timestamp result;
|
|
|
|
|
|
|
|
result = timestamp;
|
|
|
|
|
2001-10-03 17:50:48 +02:00
|
|
|
AdjustTimestampForTypmod(&result, typmod);
|
2001-10-03 07:29:27 +02:00
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
if (!TIMESTAMP_NOT_FINITE(*time) &&
|
2001-10-03 17:50:48 +02:00
|
|
|
(typmod >= 0) && (typmod <= 13))
|
2001-10-03 07:29:27 +02:00
|
|
|
{
|
|
|
|
static double TimestampScale = 1;
|
|
|
|
static int32 TimestampTypmod = 0;
|
|
|
|
|
|
|
|
if (typmod != TimestampTypmod)
|
2001-10-04 16:49:57 +02:00
|
|
|
{
|
|
|
|
TimestampScale = pow(10.0, typmod);
|
|
|
|
TimestampTypmod = typmod;
|
|
|
|
}
|
2001-10-03 07:29:27 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
*time = (rint(((double) *time) * TimestampScale) / TimestampScale);
|
2001-10-03 07:29:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
/* timestamptz_in()
|
|
|
|
* Convert a string to internal form.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid typelem = PG_GETARG_OID(1);
|
|
|
|
#endif
|
|
|
|
int32 typmod = PG_GETARG_INT32(2);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2001-09-28 10:09:14 +02:00
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
int dtype;
|
|
|
|
int nf;
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
|
|
|
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
|
|
|
|
elog(ERROR, "Bad timestamp external representation '%s'", str);
|
|
|
|
|
|
|
|
switch (dtype)
|
|
|
|
{
|
|
|
|
case DTK_DATE:
|
|
|
|
if (tm2timestamp(tm, fsec, &tz, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE out of range '%s'", str);
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EPOCH:
|
|
|
|
result = SetEpochTimestamp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_LATE:
|
|
|
|
TIMESTAMP_NOEND(result);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EARLY:
|
|
|
|
TIMESTAMP_NOBEGIN(result);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_INVALID:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE '%s' no longer supported", str);
|
2001-09-28 10:09:14 +02:00
|
|
|
TIMESTAMP_NOEND(result);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE '%s' not parsed; internal coding error", str);
|
2001-09-28 10:09:14 +02:00
|
|
|
TIMESTAMP_NOEND(result);
|
|
|
|
}
|
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
AdjustTimestampForTypmod(&result, typmod);
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timestamptz_out()
|
|
|
|
* Convert a timestamp to external form.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz dt = PG_GETARG_TIMESTAMP(0);
|
2001-09-28 10:09:14 +02:00
|
|
|
char *result;
|
1997-09-08 04:41:22 +02:00
|
|
|
int tz;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
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 18:26:26 +01:00
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
1997-09-08 04:41:22 +02:00
|
|
|
char buf[MAXDATELEN + 1];
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(dt))
|
2000-06-09 03:11:16 +02:00
|
|
|
EncodeSpecialTimestamp(dt, buf);
|
|
|
|
else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn) == 0)
|
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 18:26:26 +01:00
|
|
|
EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
|
|
|
|
else
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "Unable to format timestamp with time zone; internal coding error");
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
/* timestamptz_scale()
|
|
|
|
* Adjust time type for specified scale factor.
|
|
|
|
* Used by PostgreSQL type system to stuff columns.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_scale(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
2001-10-03 07:29:27 +02:00
|
|
|
int32 typmod = PG_GETARG_INT32(1);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2001-10-03 07:29:27 +02:00
|
|
|
|
|
|
|
result = timestamp;
|
|
|
|
|
2001-10-03 17:50:48 +02:00
|
|
|
AdjustTimestampForTypmod(&result, typmod);
|
2001-10-03 07:29:27 +02:00
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
}
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/* interval_in()
|
|
|
|
* Convert a string to internal form.
|
|
|
|
*
|
|
|
|
* External format(s):
|
|
|
|
* Uses the generic date/time parsing and decoding routines.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_in(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid typelem = PG_GETARG_OID(1);
|
|
|
|
#endif
|
|
|
|
int32 typmod = PG_GETARG_INT32(2);
|
2001-09-28 10:09:14 +02:00
|
|
|
Interval *result;
|
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 18:26:26 +01:00
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int dtype;
|
|
|
|
int nf;
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
tm->tm_year = 0;
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
tm->tm_mday = 0;
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_min = 0;
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
fsec = 0;
|
|
|
|
|
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
|
|
|
|| (DecodeDateDelta(field, ftype, nf, &dtype, tm, &fsec) != 0))
|
|
|
|
elog(ERROR, "Bad interval external representation '%s'", str);
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
switch (dtype)
|
|
|
|
{
|
|
|
|
case DTK_DELTA:
|
2001-09-28 10:09:14 +02:00
|
|
|
if (tm2interval(tm, fsec, result) != 0)
|
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 18:26:26 +01:00
|
|
|
elog(ERROR, "Bad interval external representation '%s'", str);
|
2001-10-18 19:30:21 +02:00
|
|
|
AdjustIntervalForTypmod(result, typmod);
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_INVALID:
|
|
|
|
elog(ERROR, "Interval '%s' no longer supported", str);
|
1997-10-09 07:02:17 +02:00
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
1997-10-09 07:02:17 +02:00
|
|
|
default:
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "Interval '%s' not parsed; internal coding error", str);
|
1997-10-09 07:02:17 +02:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/* interval_out()
|
|
|
|
* Convert a time span to external form.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_out(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(0);
|
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 18:26:26 +01:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (interval2tm(*span, tm, &fsec) != 0)
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "Unable to encode interval; internal coding error");
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
if (EncodeTimeSpan(tm, fsec, DateStyle, buf) != 0)
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "Unable to format interval; internal coding error");
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
/* interval_scale()
|
|
|
|
* Adjust interval type for specified fields.
|
|
|
|
* Used by PostgreSQL type system to stuff columns.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
interval_scale(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *interval = PG_GETARG_INTERVAL_P(0);
|
|
|
|
int32 typmod = PG_GETARG_INT32(1);
|
|
|
|
Interval *result;
|
|
|
|
|
|
|
|
result = palloc(sizeof(Interval));
|
|
|
|
*result = *interval;
|
|
|
|
|
|
|
|
AdjustIntervalForTypmod(result, typmod);
|
|
|
|
|
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MASK(b) (1 << (b))
|
|
|
|
|
|
|
|
static void
|
|
|
|
AdjustIntervalForTypmod(Interval *interval, int32 typmod)
|
|
|
|
{
|
|
|
|
if (typmod != -1)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int range = ((typmod >> 16) & 0x7FFF);
|
|
|
|
int precision = (typmod & 0xFFFF);
|
2001-10-18 19:30:21 +02:00
|
|
|
|
2001-10-20 03:02:22 +02:00
|
|
|
if (range == 0x7FFF)
|
2001-10-18 19:30:21 +02:00
|
|
|
{
|
|
|
|
/* Do nothing... */
|
|
|
|
}
|
2001-10-20 03:02:22 +02:00
|
|
|
else if (range == MASK(YEAR))
|
2001-10-18 19:30:21 +02:00
|
|
|
{
|
|
|
|
interval->month = ((interval->month / 12) * 12);
|
|
|
|
interval->time = 0;
|
|
|
|
}
|
|
|
|
else if (range == MASK(MONTH))
|
|
|
|
{
|
|
|
|
interval->month %= 12;
|
|
|
|
interval->time = 0;
|
|
|
|
}
|
|
|
|
/* YEAR TO MONTH */
|
2001-10-25 07:50:21 +02:00
|
|
|
else if (range == (MASK(YEAR) | MASK(MONTH)))
|
2001-10-18 19:30:21 +02:00
|
|
|
interval->time = 0;
|
|
|
|
else if (range == MASK(DAY))
|
|
|
|
{
|
|
|
|
interval->month = 0;
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 86400)) * 86400);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
else if (range == MASK(HOUR))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double day;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, day, 86400.0);
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 3600)) * 3600.0);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
else if (range == MASK(MINUTE))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double hour;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, hour, 3600.0);
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 60)) * 60);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
else if (range == MASK(SECOND))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double hour;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, hour, 60.0);
|
2001-10-25 07:50:21 +02:00
|
|
|
/* interval->time = (int)(interval->time); */
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
/* DAY TO HOUR */
|
|
|
|
else if (range == (MASK(DAY) | MASK(HOUR)))
|
|
|
|
{
|
|
|
|
interval->month = 0;
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 3600)) * 3600);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
/* DAY TO MINUTE */
|
|
|
|
else if (range == (MASK(DAY) | MASK(HOUR) | MASK(MINUTE)))
|
|
|
|
{
|
|
|
|
interval->month = 0;
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 60)) * 60);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
/* DAY TO SECOND */
|
|
|
|
else if (range == (MASK(DAY) | MASK(HOUR) | MASK(MINUTE) | MASK(SECOND)))
|
|
|
|
interval->month = 0;
|
|
|
|
/* HOUR TO MINUTE */
|
|
|
|
else if (range == (MASK(HOUR) | MASK(MINUTE)))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double day;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, day, 86400.0);
|
2001-10-25 07:50:21 +02:00
|
|
|
interval->time = (((int) (interval->time / 60)) * 60);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
/* HOUR TO SECOND */
|
|
|
|
else if (range == (MASK(HOUR) | MASK(MINUTE) | MASK(SECOND)))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double day;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, day, 86400.0);
|
|
|
|
}
|
|
|
|
/* MINUTE TO SECOND */
|
|
|
|
else if (range == (MASK(MINUTE) | MASK(SECOND)))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double hour;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
interval->month = 0;
|
|
|
|
TMODULO(interval->time, hour, 3600.0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "AdjustIntervalForTypmod(): internal coding error");
|
|
|
|
|
|
|
|
if (precision != 0xFFFF)
|
|
|
|
{
|
|
|
|
static double IntervalScale = 1;
|
2001-10-25 07:50:21 +02:00
|
|
|
static int IntervalTypmod = 0;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
if (precision != IntervalTypmod)
|
|
|
|
{
|
|
|
|
IntervalTypmod = precision;
|
|
|
|
IntervalScale = pow(10.0, IntervalTypmod);
|
|
|
|
}
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Hmm. For the time field, we can get to a large value since
|
|
|
|
* we store everything related to an absolute interval (e.g.
|
|
|
|
* years worth of days) in this one field. So we have
|
2001-10-20 03:02:22 +02:00
|
|
|
* precision problems doing rint() on this field if the field
|
2001-10-25 07:50:21 +02:00
|
|
|
* is too large. This resulted in an annoying "...0001"
|
|
|
|
* appended to the printed result on my Linux box. I hate
|
|
|
|
* doing an expensive math operation like log10() to avoid
|
|
|
|
* this, but what else can we do?? - thomas 2001-10-19
|
2001-10-20 03:02:22 +02:00
|
|
|
*/
|
|
|
|
if ((log10(interval->time) + IntervalTypmod) <= 13)
|
|
|
|
interval->time = (rint(interval->time * IntervalScale) / IntervalScale);
|
2001-10-18 19:30:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/* EncodeSpecialTimestamp()
|
|
|
|
* Convert reserved timestamp data type to string.
|
|
|
|
*/
|
2000-06-09 00:38:00 +02:00
|
|
|
static int
|
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 18:26:26 +01:00
|
|
|
EncodeSpecialTimestamp(Timestamp dt, char *str)
|
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
if (TIMESTAMP_IS_NOBEGIN(dt))
|
|
|
|
strcpy(str, EARLY);
|
|
|
|
else if (TIMESTAMP_IS_NOEND(dt))
|
|
|
|
strcpy(str, LATE);
|
|
|
|
else
|
|
|
|
return FALSE;
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
return TRUE;
|
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 18:26:26 +01:00
|
|
|
} /* EncodeSpecialTimestamp() */
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
now(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
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 18:26:26 +01:00
|
|
|
AbsoluteTime sec;
|
2001-09-28 10:09:14 +02:00
|
|
|
int usec;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
sec = GetCurrentTransactionStartTimeUsec(&usec);
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = (sec + (usec * 1.0e-6) - ((date2j(2000, 1, 1) - date2j(1970, 1, 1)) * 86400));
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
void
|
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 18:26:26 +01:00
|
|
|
dt2time(Timestamp jd, int *hour, int *min, double *sec)
|
|
|
|
{
|
|
|
|
double time;
|
|
|
|
|
|
|
|
time = jd;
|
|
|
|
|
|
|
|
*hour = (time / 3600);
|
|
|
|
time -= ((*hour) * 3600);
|
|
|
|
*min = (time / 60);
|
|
|
|
time -= ((*min) * 60);
|
|
|
|
*sec = JROUND(time);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} /* dt2time() */
|
|
|
|
|
|
|
|
|
|
|
|
/* timestamp2tm()
|
|
|
|
* Convert timestamp data type to POSIX time structure.
|
|
|
|
* Note that year is _not_ 1900-based, but is an explicit full value.
|
|
|
|
* Also, month is one-based, _not_ zero-based.
|
|
|
|
* Returns:
|
|
|
|
* 0 on success
|
|
|
|
* -1 on out of range
|
|
|
|
*
|
|
|
|
* For dates within the system-supported time_t range, convert to the
|
|
|
|
* local time zone. If out of this range, leave as GMT. - tgl 97/05/27
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
timestamp2tm(Timestamp dt, int *tzp, struct tm * tm, double *fsec, char **tzn)
|
|
|
|
{
|
|
|
|
double date,
|
|
|
|
date0,
|
|
|
|
time,
|
|
|
|
sec;
|
|
|
|
time_t utime;
|
|
|
|
|
2000-10-29 14:17:34 +01:00
|
|
|
#if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
|
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 18:26:26 +01:00
|
|
|
struct tm *tx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
date0 = date2j(2000, 1, 1);
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* If HasCTZSet is true then we have a brute force time zone
|
|
|
|
* specified. Go ahead and rotate to the local time zone since we will
|
|
|
|
* later bypass any calls which adjust the tm fields.
|
2001-10-18 19:30:21 +02:00
|
|
|
*/
|
|
|
|
if (HasCTZSet && (tzp != NULL))
|
|
|
|
dt -= CTimeZone;
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
time = dt;
|
|
|
|
TMODULO(time, date, 86400e0);
|
|
|
|
|
|
|
|
if (time < 0)
|
|
|
|
{
|
|
|
|
time += 86400;
|
|
|
|
date -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Julian day routine does not work for negative Julian days */
|
|
|
|
if (date < -date0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* add offset to go from J2000 back to standard Julian date */
|
|
|
|
date += date0;
|
|
|
|
|
|
|
|
j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
|
|
|
|
dt2time(time, &tm->tm_hour, &tm->tm_min, &sec);
|
|
|
|
|
|
|
|
*fsec = JROUND(sec);
|
|
|
|
TMODULO(*fsec, tm->tm_sec, 1e0);
|
|
|
|
|
|
|
|
if (tzp != NULL)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* We have a brute force time zone per SQL99? Then use it without
|
|
|
|
* change since we have already rotated to the time zone.
|
2001-10-18 19:30:21 +02:00
|
|
|
*/
|
|
|
|
if (HasCTZSet)
|
|
|
|
{
|
|
|
|
*tzp = CTimeZone;
|
|
|
|
tm->tm_isdst = 0;
|
2001-10-18 21:54:59 +02:00
|
|
|
#if defined(HAVE_TM_ZONE)
|
|
|
|
tm->tm_gmtoff = CTimeZone;
|
2001-10-18 19:30:21 +02:00
|
|
|
tm->tm_zone = NULL;
|
2001-10-18 21:54:59 +02:00
|
|
|
#endif
|
2001-10-18 19:30:21 +02:00
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Does this fall within the capabilities of the localtime()
|
|
|
|
* interface? Then use this to rotate to the local time zone.
|
2001-10-18 19:30:21 +02:00
|
|
|
*/
|
|
|
|
else if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
utime = (dt + (date0 - date2j(1970, 1, 1)) * 86400);
|
|
|
|
|
2000-10-29 14:17:34 +01:00
|
|
|
#if defined(HAVE_TM_ZONE) || defined(HAVE_INT_TIMEZONE)
|
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 18:26:26 +01:00
|
|
|
tx = localtime(&utime);
|
2001-03-22 05:01:46 +01:00
|
|
|
#ifdef NO_MKTIME_BEFORE_1970
|
2001-02-13 15:32:52 +01:00
|
|
|
if (tx->tm_year < 70 && tx->tm_isdst == 1)
|
|
|
|
{
|
|
|
|
utime -= 3600;
|
|
|
|
tx = localtime(&utime);
|
|
|
|
tx->tm_isdst = 0;
|
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
#endif
|
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 18:26:26 +01:00
|
|
|
tm->tm_year = tx->tm_year + 1900;
|
|
|
|
tm->tm_mon = tx->tm_mon + 1;
|
|
|
|
tm->tm_mday = tx->tm_mday;
|
|
|
|
tm->tm_hour = tx->tm_hour;
|
|
|
|
tm->tm_min = tx->tm_min;
|
|
|
|
#if NOT_USED
|
|
|
|
/* XXX HACK
|
|
|
|
* Argh! My Linux box puts in a 1 second offset for dates less than 1970
|
|
|
|
* but only if the seconds field was non-zero. So, don't copy the seconds
|
2001-01-18 08:22:43 +01:00
|
|
|
* field and instead carry forward from the original - thomas 97/06/18
|
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 18:26:26 +01:00
|
|
|
* Note that GNU/Linux uses the standard freeware zic package as do
|
|
|
|
* many other platforms so this may not be GNU/Linux/ix86-specific.
|
2001-01-18 08:22:43 +01:00
|
|
|
* Still shows a problem on my up to date Linux box - thomas 2001-01-17
|
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 18:26:26 +01:00
|
|
|
*/
|
|
|
|
tm->tm_sec = tx->tm_sec;
|
|
|
|
#endif
|
|
|
|
tm->tm_isdst = tx->tm_isdst;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
#if defined(HAVE_TM_ZONE)
|
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 18:26:26 +01:00
|
|
|
tm->tm_gmtoff = tx->tm_gmtoff;
|
|
|
|
tm->tm_zone = tx->tm_zone;
|
|
|
|
|
|
|
|
*tzp = -(tm->tm_gmtoff); /* tm_gmtoff is Sun/DEC-ism */
|
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = (char *) tm->tm_zone;
|
2001-03-22 05:01:46 +01:00
|
|
|
#elif defined(HAVE_INT_TIMEZONE)
|
2001-01-18 08:22:43 +01:00
|
|
|
*tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL);
|
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 18:26:26 +01:00
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = tzname[(tm->tm_isdst > 0)];
|
2001-03-22 05:01:46 +01:00
|
|
|
#endif
|
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 18:26:26 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
#else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
|
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 18:26:26 +01:00
|
|
|
*tzp = CTimeZone; /* V7 conventions; don't know timezone? */
|
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = CTZName;
|
|
|
|
#endif
|
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
dt = dt2local(dt, *tzp);
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*tzp = 0;
|
2001-10-20 03:02:22 +02:00
|
|
|
/* Mark this as *no* time zone available */
|
|
|
|
tm->tm_isdst = -1;
|
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 18:26:26 +01:00
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-20 03:02:22 +02:00
|
|
|
tm->tm_isdst = -1;
|
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 18:26:26 +01:00
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* timestamp2tm() */
|
|
|
|
|
|
|
|
|
|
|
|
/* tm2timestamp()
|
|
|
|
* Convert a tm structure to a timestamp data type.
|
|
|
|
* Note that year is _not_ 1900-based, but is an explicit full value.
|
|
|
|
* Also, month is one-based, _not_ zero-based.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tm2timestamp(struct tm * tm, double fsec, int *tzp, Timestamp *result)
|
|
|
|
{
|
|
|
|
|
|
|
|
double date,
|
|
|
|
time;
|
|
|
|
|
|
|
|
/* Julian day routines are not correct for negative Julian days */
|
|
|
|
if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
|
|
|
time = time2t(tm->tm_hour, tm->tm_min, (tm->tm_sec + fsec));
|
|
|
|
*result = (date * 86400 + time);
|
|
|
|
if (tzp != NULL)
|
|
|
|
*result = dt2local(*result, -(*tzp));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* tm2timestamp() */
|
|
|
|
|
|
|
|
|
|
|
|
/* interval2tm()
|
|
|
|
* Convert a interval data type to a tm structure.
|
|
|
|
*/
|
2001-09-06 05:22:42 +02:00
|
|
|
int
|
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 18:26:26 +01:00
|
|
|
interval2tm(Interval span, struct tm * tm, float8 *fsec)
|
|
|
|
{
|
|
|
|
double time;
|
|
|
|
|
|
|
|
if (span.month != 0)
|
|
|
|
{
|
|
|
|
tm->tm_year = span.month / 12;
|
|
|
|
tm->tm_mon = span.month % 12;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tm->tm_year = 0;
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ROUND_ALL
|
|
|
|
time = JROUND(span.time);
|
|
|
|
#else
|
|
|
|
time = span.time;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TMODULO(time, tm->tm_mday, 86400e0);
|
|
|
|
TMODULO(time, tm->tm_hour, 3600e0);
|
|
|
|
TMODULO(time, tm->tm_min, 60e0);
|
|
|
|
TMODULO(time, tm->tm_sec, 1e0);
|
|
|
|
*fsec = time;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* interval2tm() */
|
|
|
|
|
2001-09-06 05:22:42 +02:00
|
|
|
int
|
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 18:26:26 +01:00
|
|
|
tm2interval(struct tm * tm, double fsec, Interval *span)
|
|
|
|
{
|
|
|
|
span->month = ((tm->tm_year * 12) + tm->tm_mon);
|
|
|
|
span->time = ((((((tm->tm_mday * 24.0)
|
2000-04-12 19:17:23 +02:00
|
|
|
+ tm->tm_hour) * 60.0)
|
|
|
|
+ tm->tm_min) * 60.0)
|
|
|
|
+ tm->tm_sec);
|
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 18:26:26 +01:00
|
|
|
span->time = JROUND(span->time + fsec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* tm2interval() */
|
|
|
|
|
|
|
|
static double
|
|
|
|
time2t(const int hour, const int min, const double sec)
|
|
|
|
{
|
|
|
|
return (((hour * 60) + min) * 60) + sec;
|
|
|
|
} /* time2t() */
|
|
|
|
|
2000-06-09 00:38:00 +02:00
|
|
|
static Timestamp
|
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 18:26:26 +01:00
|
|
|
dt2local(Timestamp dt, int tz)
|
|
|
|
{
|
|
|
|
dt -= tz;
|
|
|
|
dt = JROUND(dt);
|
|
|
|
return dt;
|
|
|
|
} /* dt2local() */
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_finite(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
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 18:26:26 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_BOOL(!TIMESTAMP_NOT_FINITE(timestamp));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_finite(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Relational operators for timestamp.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
void
|
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 18:26:26 +01:00
|
|
|
GetEpochTime(struct tm * tm)
|
|
|
|
{
|
|
|
|
struct tm *t0;
|
|
|
|
time_t epoch = 0;
|
|
|
|
|
|
|
|
t0 = gmtime(&epoch);
|
|
|
|
|
|
|
|
tm->tm_year = t0->tm_year;
|
|
|
|
tm->tm_mon = t0->tm_mon;
|
|
|
|
tm->tm_mday = t0->tm_mday;
|
|
|
|
tm->tm_hour = t0->tm_hour;
|
|
|
|
tm->tm_min = t0->tm_min;
|
|
|
|
tm->tm_sec = t0->tm_sec;
|
|
|
|
|
|
|
|
if (tm->tm_year < 1900)
|
|
|
|
tm->tm_year += 1900;
|
|
|
|
tm->tm_mon++;
|
|
|
|
|
|
|
|
return;
|
|
|
|
} /* GetEpochTime() */
|
|
|
|
|
|
|
|
Timestamp
|
2001-09-28 10:09:14 +02:00
|
|
|
SetEpochTimestamp(void)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
Timestamp dt;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
GetEpochTime(tm);
|
|
|
|
tm2timestamp(tm, 0, NULL, &dt);
|
1997-03-14 06:58:13 +01:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
return dt;
|
2001-09-28 10:09:14 +02:00
|
|
|
} /* SetEpochTimestamp() */
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
/*
|
|
|
|
* timestamp_relop - is timestamp1 relop timestamp2
|
2001-05-03 21:00:37 +02:00
|
|
|
*
|
|
|
|
* collate invalid timestamp at the end
|
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 18:26:26 +01:00
|
|
|
*/
|
2001-05-03 21:00:37 +02:00
|
|
|
static int
|
|
|
|
timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
|
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
return ((dt1 < dt2) ? -1 : ((dt1 > dt2) ? 1 : 0));
|
2001-05-03 21:00:37 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_eq(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_ne(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_lt(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
1997-03-14 06:58:13 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_gt(PG_FUNCTION_ARGS)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_le(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_ge(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_cmp(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
/*
|
|
|
|
* interval_relop - is interval1 relop interval2
|
2001-05-03 21:00:37 +02:00
|
|
|
*
|
|
|
|
* collate invalid interval at the end
|
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 18:26:26 +01:00
|
|
|
*/
|
2001-05-03 21:00:37 +02:00
|
|
|
static int
|
|
|
|
interval_cmp_internal(Interval *interval1, Interval *interval2)
|
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
double span1,
|
|
|
|
span2;
|
2001-05-03 21:00:37 +02:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
span1 = interval1->time;
|
|
|
|
if (interval1->month != 0)
|
|
|
|
span1 += (interval1->month * (30.0 * 86400));
|
|
|
|
span2 = interval2->time;
|
|
|
|
if (interval2->month != 0)
|
|
|
|
span2 += (interval2->month * (30.0 * 86400));
|
2001-05-03 21:00:37 +02:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
return ((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0);
|
2001-05-03 21:00:37 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_eq(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_ne(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_lt(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
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_gt(PG_FUNCTION_ARGS)
|
1997-10-25 07:22:58 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_le(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_ge(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_cmp(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
/*
|
|
|
|
* interval, being an unusual size, needs a specialized hash function.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
interval_hash(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *key = PG_GETARG_INTERVAL_P(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specify hash length as sizeof(double) + sizeof(int4), not as
|
|
|
|
* sizeof(Interval), so that any garbage pad bytes in the structure
|
|
|
|
* won't be included in the hash!
|
|
|
|
*/
|
2002-03-09 18:35:37 +01:00
|
|
|
return hash_any((unsigned char *) key, sizeof(double) + sizeof(int4));
|
2000-06-19 05:55:01 +02:00
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* overlaps_timestamp() --- 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_timestamp(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* The arguments are Timestamps, but we leave them as generic Datums
|
2000-12-07 19:38:59 +01:00
|
|
|
* to avoid unnecessary conversions between value and reference forms
|
|
|
|
* --- not to mention possible dereferences of null pointers.
|
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 TIMESTAMP_GT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
|
|
|
|
#define TIMESTAMP_LT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +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.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
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 (TIMESTAMP_GT(ts1, te1))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts1;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
ts1 = te1;
|
|
|
|
te1 = tt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
2000-12-07 19:38:59 +01:00
|
|
|
else if (!te2IsNull)
|
|
|
|
{
|
|
|
|
if (TIMESTAMP_GT(ts2, te2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts2;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
ts2 = te2;
|
|
|
|
te2 = tt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point neither ts1 nor ts2 is null, so we can consider three
|
|
|
|
* cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
|
|
|
|
*/
|
|
|
|
if (TIMESTAMP_GT(ts1, ts2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* This case is ts1 < te2 OR te1 < te2, which may look redundant
|
2000-12-07 19:38:59 +01:00
|
|
|
* but in the presence of nulls it's not quite completely so.
|
|
|
|
*/
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMESTAMP_LT(ts1, te2))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If te1 is not null then we had ts1 <= te1 above, and we just
|
2000-12-07 19:38:59 +01:00
|
|
|
* found ts1 >= te2, hence te1 >= te2.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else if (TIMESTAMP_LT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts2 < te1 OR te2 < te1 */
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMESTAMP_LT(ts2, te1))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If te2 is not null then we had ts2 <= te2 above, and we just
|
2000-12-07 19:38:59 +01:00
|
|
|
* found ts2 >= te1, hence te2 >= te1.
|
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* 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".
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
if (te1IsNull || te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
#undef TIMESTAMP_GT
|
|
|
|
#undef TIMESTAMP_LT
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* "Arithmetic" operators on date/times.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* We are currently sharing some code between timestamp and timestamptz.
|
|
|
|
* The comparison functions are among them. - thomas 2001-09-25
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_smaller(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp result;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = ((dt2 < dt1) ? dt2 : dt1);
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_larger(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp result;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = ((dt2 > dt1) ? dt2 : dt1);
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_mi(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(dt1) || TIMESTAMP_NOT_FINITE(dt2))
|
|
|
|
{
|
|
|
|
elog(ERROR, "Unable to subtract non-finite timestamps");
|
|
|
|
result->time = 0;
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
else
|
|
|
|
result->time = JROUND(dt1 - dt2);
|
2001-09-28 10:09:14 +02:00
|
|
|
|
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 18:26:26 +01:00
|
|
|
result->month = 0;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* timestamp_pl_span()
|
|
|
|
* Add a interval to a timestamp data type.
|
|
|
|
* Note that interval has provisions for qualitative year/month
|
|
|
|
* units, so try to do the right thing with them.
|
|
|
|
* To add a month, increment the month, and use the same day of month.
|
|
|
|
* Then, if the next month has fewer days, set the day of month
|
|
|
|
* to the last day of month.
|
|
|
|
* Lastly, add in the "quantitative time".
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_pl_span(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
Timestamp result;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
result = timestamp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (span->month != 0)
|
|
|
|
{
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
|
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
|
|
|
|
{
|
|
|
|
tm->tm_mon += span->month;
|
|
|
|
if (tm->tm_mon > 12)
|
|
|
|
{
|
|
|
|
tm->tm_year += ((tm->tm_mon - 1) / 12);
|
|
|
|
tm->tm_mon = (((tm->tm_mon - 1) % 12) + 1);
|
|
|
|
}
|
|
|
|
else if (tm->tm_mon < 1)
|
|
|
|
{
|
|
|
|
tm->tm_year += ((tm->tm_mon / 12) - 1);
|
|
|
|
tm->tm_mon = ((tm->tm_mon % 12) + 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust for end of month boundary problems... */
|
|
|
|
if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
|
|
|
|
tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
|
|
|
|
|
|
|
|
if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0)
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to add TIMESTAMP and INTERVAL"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\ttimestamp_pl_span() internal error encoding timestamp");
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to add TIMESTAMP and INTERVAL"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\ttimestamp_pl_span() internal error decoding timestamp");
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ROUND_ALL
|
|
|
|
timestamp = JROUND(timestamp + span->time);
|
|
|
|
#else
|
|
|
|
timestamp += span->time;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
result = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_mi_span(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
Interval tspan;
|
|
|
|
|
|
|
|
tspan.month = -span->month;
|
|
|
|
tspan.time = -span->time;
|
|
|
|
|
|
|
|
return DirectFunctionCall2(timestamp_pl_span,
|
|
|
|
TimestampGetDatum(timestamp),
|
|
|
|
PointerGetDatum(&tspan));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-12 05:38:38 +01:00
|
|
|
/* timestamptz_pl_span()
|
2001-09-28 10:09:14 +02:00
|
|
|
* Add a interval to a timestamp with time zone data type.
|
|
|
|
* Note that interval has provisions for qualitative year/month
|
|
|
|
* units, so try to do the right thing with them.
|
|
|
|
* To add a month, increment the month, and use the same day of month.
|
|
|
|
* Then, if the next month has fewer days, set the day of month
|
|
|
|
* to the last day of month.
|
|
|
|
* Lastly, add in the "quantitative time".
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_pl_span(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
2001-09-28 10:09:14 +02:00
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
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 18:26:26 +01:00
|
|
|
int tz;
|
|
|
|
char *tzn;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
result = timestamp;
|
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 18:26:26 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (span->month != 0)
|
|
|
|
{
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) == 0)
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
tm->tm_mon += span->month;
|
|
|
|
if (tm->tm_mon > 12)
|
|
|
|
{
|
|
|
|
tm->tm_year += ((tm->tm_mon - 1) / 12);
|
|
|
|
tm->tm_mon = (((tm->tm_mon - 1) % 12) + 1);
|
|
|
|
}
|
|
|
|
else if (tm->tm_mon < 1)
|
|
|
|
{
|
|
|
|
tm->tm_year += ((tm->tm_mon / 12) - 1);
|
|
|
|
tm->tm_mon = ((tm->tm_mon % 12) + 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust for end of month boundary problems... */
|
|
|
|
if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
|
|
|
|
tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
|
|
|
|
|
2001-05-04 00:53:07 +02:00
|
|
|
tz = DetermineLocalTimeZone(tm);
|
2000-11-06 16:57:00 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (tm2timestamp(tm, fsec, &tz, ×tamp) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to add TIMESTAMP and INTERVAL"
|
2001-10-25 07:50:21 +02:00
|
|
|
"\n\ttimestamptz_pl_span() internal error encoding timestamp");
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
else
|
2001-09-28 10:09:14 +02:00
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to add TIMESTAMP and INTERVAL"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\ttimestamptz_pl_span() internal error decoding timestamp");
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ROUND_ALL
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamp = JROUND(timestamp + span->time);
|
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 18:26:26 +01:00
|
|
|
#else
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamp += span->time;
|
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 18:26:26 +01:00
|
|
|
#endif
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = timestamp;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamptz_mi_span(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval tspan;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
tspan.month = -span->month;
|
|
|
|
tspan.time = -span->time;
|
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 18:26:26 +01:00
|
|
|
|
2002-01-12 05:38:38 +01:00
|
|
|
return DirectFunctionCall2(timestamptz_pl_span,
|
2000-06-09 03:11:16 +02:00
|
|
|
TimestampGetDatum(timestamp),
|
|
|
|
PointerGetDatum(&tspan));
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_um(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval = PG_GETARG_INTERVAL_P(0);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
result->time = -(interval->time);
|
|
|
|
result->month = -(interval->month);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_smaller(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
double span1,
|
|
|
|
span2;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
span1 = interval1->time;
|
|
|
|
if (interval1->month != 0)
|
|
|
|
span1 += (interval1->month * (30.0 * 86400));
|
|
|
|
span2 = interval2->time;
|
|
|
|
if (interval2->month != 0)
|
|
|
|
span2 += (interval2->month * (30.0 * 86400));
|
|
|
|
|
|
|
|
if (span2 < span1)
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
result->time = interval2->time;
|
|
|
|
result->month = interval2->month;
|
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
else
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
result->time = interval1->time;
|
|
|
|
result->month = interval1->month;
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_larger(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *interval2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
double span1,
|
|
|
|
span2;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
span1 = interval1->time;
|
|
|
|
if (interval1->month != 0)
|
|
|
|
span1 += (interval1->month * (30.0 * 86400));
|
|
|
|
span2 = interval2->time;
|
|
|
|
if (interval2->month != 0)
|
|
|
|
span2 += (interval2->month * (30.0 * 86400));
|
|
|
|
|
|
|
|
if (span2 > span1)
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
result->time = interval2->time;
|
|
|
|
result->month = interval2->month;
|
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
else
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
result->time = interval1->time;
|
|
|
|
result->month = interval1->month;
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_pl(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *span2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
result->month = (span1->month + span2->month);
|
|
|
|
result->time = JROUND(span1->time + span2->time);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_mi(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Interval *span2 = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
result->month = (span1->month - span2->month);
|
|
|
|
result->time = JROUND(span1->time - span2->time);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_mul(PG_FUNCTION_ARGS)
|
2000-04-07 15:40:45 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
float8 factor = PG_GETARG_FLOAT8(1);
|
2000-04-07 15:40:45 +02:00
|
|
|
Interval *result;
|
|
|
|
double months;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
months = (span1->month * factor);
|
2000-04-07 15:40:45 +02:00
|
|
|
result->month = rint(months);
|
2000-06-09 03:11:16 +02:00
|
|
|
result->time = JROUND(span1->time * factor);
|
2000-04-07 15:40:45 +02:00
|
|
|
/* evaluate fractional months as 30 days */
|
|
|
|
result->time += JROUND((months - result->month) * 30 * 86400);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
mul_d_interval(PG_FUNCTION_ARGS)
|
2000-04-07 15:40:45 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
/* Args are float8 and Interval *, but leave them as generic Datum */
|
|
|
|
Datum factor = PG_GETARG_DATUM(0);
|
|
|
|
Datum span1 = PG_GETARG_DATUM(1);
|
2000-04-07 15:40:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
return DirectFunctionCall2(interval_mul, span1, factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
interval_div(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *span1 = PG_GETARG_INTERVAL_P(0);
|
|
|
|
float8 factor = PG_GETARG_FLOAT8(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
2000-04-07 15:40:45 +02:00
|
|
|
double months;
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (factor == 0.0)
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "interval_div: divide by 0.0 error");
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
months = (span1->month / factor);
|
2000-04-07 15:40:45 +02:00
|
|
|
result->month = rint(months);
|
2000-06-09 03:11:16 +02:00
|
|
|
result->time = JROUND(span1->time / factor);
|
2000-04-07 15:40:45 +02:00
|
|
|
/* evaluate fractional months as 30 days */
|
|
|
|
result->time += JROUND((months - result->month) * 30 * 86400);
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* interval_accum and interval_avg implement the AVG(interval) aggregate.
|
|
|
|
*
|
|
|
|
* The transition datatype for this aggregate is a 2-element array of
|
|
|
|
* intervals, where the first is the running sum and the second contains
|
|
|
|
* the number of values so far in its 'time' field. This is a bit ugly
|
|
|
|
* but it beats inventing a specialized datatype for the purpose.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Datum
|
|
|
|
interval_accum(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
Interval *newval = PG_GETARG_INTERVAL_P(1);
|
|
|
|
Datum *transdatums;
|
|
|
|
int ndatums;
|
|
|
|
Interval sumX,
|
|
|
|
N;
|
|
|
|
Interval *newsum;
|
|
|
|
ArrayType *result;
|
|
|
|
|
|
|
|
/* We assume the input is array of interval */
|
|
|
|
deconstruct_array(transarray,
|
|
|
|
false, 12, 'd',
|
|
|
|
&transdatums, &ndatums);
|
|
|
|
if (ndatums != 2)
|
|
|
|
elog(ERROR, "interval_accum: expected 2-element interval array");
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* XXX memcpy, instead of just extracting a pointer, to work around
|
|
|
|
* buggy array code: it won't ensure proper alignment of Interval
|
2001-03-22 05:01:46 +01:00
|
|
|
* objects on machines where double requires 8-byte alignment. That
|
|
|
|
* should be fixed, but in the meantime...
|
2001-11-21 19:29:48 +01:00
|
|
|
*
|
|
|
|
* Note: must use DatumGetPointer here, not DatumGetIntervalP,
|
|
|
|
* else some compilers optimize into double-aligned load/store anyway.
|
2000-07-17 05:05:41 +02:00
|
|
|
*/
|
2001-11-21 19:29:48 +01:00
|
|
|
memcpy((void *) &sumX, DatumGetPointer(transdatums[0]), sizeof(Interval));
|
|
|
|
memcpy((void *) &N, DatumGetPointer(transdatums[1]), sizeof(Interval));
|
2000-07-17 05:05:41 +02:00
|
|
|
|
|
|
|
newsum = DatumGetIntervalP(DirectFunctionCall2(interval_pl,
|
2001-10-25 07:50:21 +02:00
|
|
|
IntervalPGetDatum(&sumX),
|
|
|
|
IntervalPGetDatum(newval)));
|
2000-07-17 05:05:41 +02:00
|
|
|
N.time += 1;
|
|
|
|
|
|
|
|
transdatums[0] = IntervalPGetDatum(newsum);
|
|
|
|
transdatums[1] = IntervalPGetDatum(&N);
|
|
|
|
|
|
|
|
result = construct_array(transdatums, 2,
|
|
|
|
false, 12, 'd');
|
|
|
|
|
|
|
|
PG_RETURN_ARRAYTYPE_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
interval_avg(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
|
|
|
|
Datum *transdatums;
|
|
|
|
int ndatums;
|
|
|
|
Interval sumX,
|
|
|
|
N;
|
|
|
|
|
|
|
|
/* We assume the input is array of interval */
|
|
|
|
deconstruct_array(transarray,
|
|
|
|
false, 12, 'd',
|
|
|
|
&transdatums, &ndatums);
|
|
|
|
if (ndatums != 2)
|
|
|
|
elog(ERROR, "interval_avg: expected 2-element interval array");
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* XXX memcpy, instead of just extracting a pointer, to work around
|
|
|
|
* buggy array code: it won't ensure proper alignment of Interval
|
2001-03-22 05:01:46 +01:00
|
|
|
* objects on machines where double requires 8-byte alignment. That
|
|
|
|
* should be fixed, but in the meantime...
|
2001-11-21 19:29:48 +01:00
|
|
|
*
|
|
|
|
* Note: must use DatumGetPointer here, not DatumGetIntervalP,
|
|
|
|
* else some compilers optimize into double-aligned load/store anyway.
|
2000-07-17 05:05:41 +02:00
|
|
|
*/
|
2001-11-21 19:29:48 +01:00
|
|
|
memcpy((void *) &sumX, DatumGetPointer(transdatums[0]), sizeof(Interval));
|
|
|
|
memcpy((void *) &N, DatumGetPointer(transdatums[1]), sizeof(Interval));
|
2000-07-17 05:05:41 +02:00
|
|
|
|
|
|
|
/* SQL92 defines AVG of no values to be NULL */
|
|
|
|
if (N.time == 0)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
return DirectFunctionCall2(interval_div,
|
|
|
|
IntervalPGetDatum(&sumX),
|
|
|
|
Float8GetDatum(N.time));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
/* timestamp_age()
|
|
|
|
* Calculate time difference while retaining year/month fields.
|
|
|
|
* Note that this does not result in an accurate absolute time span
|
|
|
|
* since year and month are out of context once the arithmetic
|
|
|
|
* is done.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_age(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
double fsec,
|
|
|
|
fsec1,
|
|
|
|
fsec2;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
struct tm tt1,
|
|
|
|
*tm1 = &tt1;
|
|
|
|
struct tm tt2,
|
|
|
|
*tm2 = &tt2;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if ((timestamp2tm(dt1, NULL, tm1, &fsec1, NULL) == 0)
|
|
|
|
&& (timestamp2tm(dt2, NULL, tm2, &fsec2, NULL) == 0))
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
fsec = (fsec1 - fsec2);
|
|
|
|
tm->tm_sec = (tm1->tm_sec - tm2->tm_sec);
|
|
|
|
tm->tm_min = (tm1->tm_min - tm2->tm_min);
|
|
|
|
tm->tm_hour = (tm1->tm_hour - tm2->tm_hour);
|
|
|
|
tm->tm_mday = (tm1->tm_mday - tm2->tm_mday);
|
|
|
|
tm->tm_mon = (tm1->tm_mon - tm2->tm_mon);
|
|
|
|
tm->tm_year = (tm1->tm_year - tm2->tm_year);
|
|
|
|
|
|
|
|
/* flip sign if necessary... */
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
fsec = -fsec;
|
|
|
|
tm->tm_sec = -tm->tm_sec;
|
|
|
|
tm->tm_min = -tm->tm_min;
|
|
|
|
tm->tm_hour = -tm->tm_hour;
|
|
|
|
tm->tm_mday = -tm->tm_mday;
|
|
|
|
tm->tm_mon = -tm->tm_mon;
|
|
|
|
tm->tm_year = -tm->tm_year;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_sec < 0)
|
|
|
|
{
|
|
|
|
tm->tm_sec += 60;
|
|
|
|
tm->tm_min--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_min < 0)
|
|
|
|
{
|
|
|
|
tm->tm_min += 60;
|
|
|
|
tm->tm_hour--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_hour < 0)
|
|
|
|
{
|
|
|
|
tm->tm_hour += 24;
|
|
|
|
tm->tm_mday--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_mday < 0)
|
|
|
|
{
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
|
|
|
|
tm->tm_mon--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
|
|
|
|
tm->tm_mon--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_mon < 0)
|
|
|
|
{
|
|
|
|
tm->tm_mon += 12;
|
|
|
|
tm->tm_year--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recover sign if necessary... */
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
fsec = -fsec;
|
|
|
|
tm->tm_sec = -tm->tm_sec;
|
|
|
|
tm->tm_min = -tm->tm_min;
|
|
|
|
tm->tm_hour = -tm->tm_hour;
|
|
|
|
tm->tm_mday = -tm->tm_mday;
|
|
|
|
tm->tm_mon = -tm->tm_mon;
|
|
|
|
tm->tm_year = -tm->tm_year;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm2interval(tm, fsec, result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to encode INTERVAL"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\ttimestamp_age() internal coding error");
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
else
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to decode TIMESTAMP"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\ttimestamp_age() internal coding error");
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_age()
|
|
|
|
* Calculate time difference while retaining year/month fields.
|
|
|
|
* Note that this does not result in an accurate absolute time span
|
|
|
|
* since year and month are out of context once the arithmetic
|
|
|
|
* is done.
|
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 18:26:26 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamptz_age(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMP(1);
|
2001-09-28 10:09:14 +02:00
|
|
|
Interval *result;
|
|
|
|
double fsec,
|
|
|
|
fsec1,
|
|
|
|
fsec2;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
struct tm tt1,
|
|
|
|
*tm1 = &tt1;
|
|
|
|
struct tm tt2,
|
|
|
|
*tm2 = &tt2;
|
|
|
|
|
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
|
|
|
|
|
|
|
if ((timestamp2tm(dt1, NULL, tm1, &fsec1, NULL) == 0)
|
|
|
|
&& (timestamp2tm(dt2, NULL, tm2, &fsec2, NULL) == 0))
|
|
|
|
{
|
|
|
|
fsec = (fsec1 - fsec2);
|
|
|
|
tm->tm_sec = (tm1->tm_sec - tm2->tm_sec);
|
|
|
|
tm->tm_min = (tm1->tm_min - tm2->tm_min);
|
|
|
|
tm->tm_hour = (tm1->tm_hour - tm2->tm_hour);
|
|
|
|
tm->tm_mday = (tm1->tm_mday - tm2->tm_mday);
|
|
|
|
tm->tm_mon = (tm1->tm_mon - tm2->tm_mon);
|
|
|
|
tm->tm_year = (tm1->tm_year - tm2->tm_year);
|
|
|
|
|
|
|
|
/* flip sign if necessary... */
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
fsec = -fsec;
|
|
|
|
tm->tm_sec = -tm->tm_sec;
|
|
|
|
tm->tm_min = -tm->tm_min;
|
|
|
|
tm->tm_hour = -tm->tm_hour;
|
|
|
|
tm->tm_mday = -tm->tm_mday;
|
|
|
|
tm->tm_mon = -tm->tm_mon;
|
|
|
|
tm->tm_year = -tm->tm_year;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_sec < 0)
|
|
|
|
{
|
|
|
|
tm->tm_sec += 60;
|
|
|
|
tm->tm_min--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_min < 0)
|
|
|
|
{
|
|
|
|
tm->tm_min += 60;
|
|
|
|
tm->tm_hour--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_hour < 0)
|
|
|
|
{
|
|
|
|
tm->tm_hour += 24;
|
|
|
|
tm->tm_mday--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_mday < 0)
|
|
|
|
{
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
|
|
|
|
tm->tm_mon--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
|
|
|
|
tm->tm_mon--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm->tm_mon < 0)
|
|
|
|
{
|
|
|
|
tm->tm_mon += 12;
|
|
|
|
tm->tm_year--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recover sign if necessary... */
|
|
|
|
if (dt1 < dt2)
|
|
|
|
{
|
|
|
|
fsec = -fsec;
|
|
|
|
tm->tm_sec = -tm->tm_sec;
|
|
|
|
tm->tm_min = -tm->tm_min;
|
|
|
|
tm->tm_hour = -tm->tm_hour;
|
|
|
|
tm->tm_mday = -tm->tm_mday;
|
|
|
|
tm->tm_mon = -tm->tm_mon;
|
|
|
|
tm->tm_year = -tm->tm_year;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm2interval(tm, fsec, result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to decode TIMESTAMP");
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
else
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to decode TIMESTAMP");
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Conversion operators.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* timestamp_text()
|
|
|
|
* Convert timestamp to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamp_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Timestamp, but may as well leave it in Datum form */
|
|
|
|
Datum timestamp = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
str = DatumGetCString(DirectFunctionCall1(timestamp_out, timestamp));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
len = (strlen(str) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = len;
|
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 18:26:26 +01:00
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* text_timestamp()
|
|
|
|
* Convert text string to timestamp.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
text_timestamp(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
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 18:26:26 +01:00
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP bad external representation (too long)");
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
2001-10-04 16:49:57 +02:00
|
|
|
return DirectFunctionCall3(timestamp_in,
|
|
|
|
CStringGetDatum(dstr),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_text()
|
|
|
|
* Convert timestamp with time zone to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Timestamp, but may as well leave it in Datum form */
|
|
|
|
Datum timestamp = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(timestamptz_out, timestamp));
|
|
|
|
|
|
|
|
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_timestamptz()
|
|
|
|
* Convert text string to timestamp with time zone.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE bad external representation (too long)");
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
2001-10-04 16:49:57 +02:00
|
|
|
return DirectFunctionCall3(timestamptz_in,
|
|
|
|
CStringGetDatum(dstr),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1));
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
/* interval_text()
|
|
|
|
* Convert interval to text data type.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_text(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
Interval *interval = PG_GETARG_INTERVAL_P(0);
|
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 18:26:26 +01:00
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
str = DatumGetCString(DirectFunctionCall1(interval_out,
|
2001-10-25 07:50:21 +02:00
|
|
|
IntervalPGetDatum(interval)));
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
len = (strlen(str) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = len;
|
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 18:26:26 +01:00
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* text_interval()
|
|
|
|
* Convert text string to interval.
|
|
|
|
* Text type may not be null terminated, so copy to temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
text_interval(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
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 18:26:26 +01:00
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL bad external representation (too long)");
|
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 18:26:26 +01:00
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
return DirectFunctionCall3(interval_in,
|
|
|
|
CStringGetDatum(dstr),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/* timestamp_trunc()
|
2001-09-28 10:09:14 +02:00
|
|
|
* Truncate timestamp to specified units.
|
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 18:26:26 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_trunc(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp result;
|
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 18:26:26 +01:00
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
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 18:26:26 +01:00
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
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 18:26:26 +01:00
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TIMESTAMP(timestamp);
|
|
|
|
|
|
|
|
if ((type == UNITS) && (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0))
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
tm->tm_year = (tm->tm_year / 1000) * 1000;
|
|
|
|
case DTK_CENTURY:
|
|
|
|
tm->tm_year = (tm->tm_year / 100) * 100;
|
|
|
|
case DTK_DECADE:
|
|
|
|
tm->tm_year = (tm->tm_year / 10) * 10;
|
|
|
|
case DTK_YEAR:
|
|
|
|
tm->tm_mon = 1;
|
|
|
|
case DTK_QUARTER:
|
|
|
|
tm->tm_mon = (3 * (tm->tm_mon / 4)) + 1;
|
|
|
|
case DTK_MONTH:
|
|
|
|
tm->tm_mday = 1;
|
|
|
|
case DTK_DAY:
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
case DTK_HOUR:
|
|
|
|
tm->tm_min = 0;
|
|
|
|
case DTK_MINUTE:
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
case DTK_SECOND:
|
|
|
|
fsec = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
|
|
|
fsec = rint(fsec * 1000) / 1000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MICROSEC:
|
|
|
|
fsec = rint(fsec * 1000000) / 1000000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tm2timestamp(tm, fsec, NULL, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to truncate TIMESTAMP to '%s'", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not recognized", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_trunc()
|
|
|
|
* Truncate timestamp to specified units.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_trunc(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
|
|
|
|
TimestampTz result;
|
2001-09-28 10:09:14 +02:00
|
|
|
int tz;
|
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not recognized",
|
2001-09-28 10:09:14 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
2001-09-28 10:09:14 +02:00
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
|
|
|
*lp = '\0';
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
PG_RETURN_TIMESTAMPTZ(timestamp);
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if ((type == UNITS) && (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) == 0))
|
|
|
|
{
|
|
|
|
switch (val)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
tm->tm_year = (tm->tm_year / 1000) * 1000;
|
|
|
|
case DTK_CENTURY:
|
|
|
|
tm->tm_year = (tm->tm_year / 100) * 100;
|
|
|
|
case DTK_DECADE:
|
|
|
|
tm->tm_year = (tm->tm_year / 10) * 10;
|
|
|
|
case DTK_YEAR:
|
|
|
|
tm->tm_mon = 1;
|
|
|
|
case DTK_QUARTER:
|
|
|
|
tm->tm_mon = (3 * (tm->tm_mon / 4)) + 1;
|
|
|
|
case DTK_MONTH:
|
|
|
|
tm->tm_mday = 1;
|
|
|
|
case DTK_DAY:
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
case DTK_HOUR:
|
|
|
|
tm->tm_min = 0;
|
|
|
|
case DTK_MINUTE:
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
case DTK_SECOND:
|
|
|
|
fsec = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
|
|
|
fsec = rint(fsec * 1000) / 1000;
|
|
|
|
break;
|
|
|
|
case DTK_MICROSEC:
|
|
|
|
fsec = rint(fsec * 1000000) / 1000000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
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 18:26:26 +01:00
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
tz = DetermineLocalTimeZone(tm);
|
|
|
|
|
|
|
|
if (tm2timestamp(tm, fsec, &tz, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to truncate TIMESTAMP WITH TIME ZONE to '%s'", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not recognized", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_NULL();
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
/* interval_trunc()
|
|
|
|
* Extract specified field from interval.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_trunc(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
Interval *interval = PG_GETARG_INTERVAL_P(1);
|
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 18:26:26 +01:00
|
|
|
Interval *result;
|
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
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 18:26:26 +01:00
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
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 18:26:26 +01:00
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (type == UNITS)
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
if (interval2tm(*interval, tm, &fsec) == 0)
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
2000-04-14 17:22:10 +02:00
|
|
|
case DTK_MILLENNIUM:
|
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 18:26:26 +01:00
|
|
|
tm->tm_year = (tm->tm_year / 1000) * 1000;
|
|
|
|
case DTK_CENTURY:
|
|
|
|
tm->tm_year = (tm->tm_year / 100) * 100;
|
|
|
|
case DTK_DECADE:
|
|
|
|
tm->tm_year = (tm->tm_year / 10) * 10;
|
|
|
|
case DTK_YEAR:
|
|
|
|
tm->tm_mon = 0;
|
|
|
|
case DTK_QUARTER:
|
|
|
|
tm->tm_mon = (3 * (tm->tm_mon / 4));
|
|
|
|
case DTK_MONTH:
|
|
|
|
tm->tm_mday = 0;
|
|
|
|
case DTK_DAY:
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
case DTK_HOUR:
|
|
|
|
tm->tm_min = 0;
|
|
|
|
case DTK_MINUTE:
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
case DTK_SECOND:
|
|
|
|
fsec = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
|
|
|
fsec = rint(fsec * 1000) / 1000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MICROSEC:
|
|
|
|
fsec = rint(fsec * 1000000) / 1000000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not supported", lowunits);
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tm2interval(tm, fsec, result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to truncate INTERVAL to '%s'", lowunits);
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(WARNING, "Unable to decode INTERVAL; internal coding error");
|
2001-09-28 10:09:14 +02:00
|
|
|
*result = *interval;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
2001-09-28 10:09:14 +02:00
|
|
|
*result = *interval;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* isoweek2date()
|
2001-01-03 17:48:02 +01:00
|
|
|
* Convert ISO week of year number to date.
|
|
|
|
* The year field must be specified!
|
|
|
|
* karel 2000/08/07
|
2000-08-29 06:41:48 +02:00
|
|
|
*/
|
|
|
|
void
|
2001-03-22 05:01:46 +01:00
|
|
|
isoweek2date(int woy, int *year, int *mon, int *mday)
|
2000-08-29 06:41:48 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int day0,
|
|
|
|
day4,
|
|
|
|
dayn;
|
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
if (!*year)
|
|
|
|
elog(ERROR, "isoweek2date(): can't convert without year information");
|
|
|
|
|
|
|
|
/* fourth day of current year */
|
|
|
|
day4 = date2j(*year, 1, 4);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* day0 == offset to first day of week (Monday) */
|
|
|
|
day0 = (j2day(day4 - 1) % 7);
|
|
|
|
|
|
|
|
dayn = ((woy - 1) * 7) + (day4 - day0);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
j2date(dayn, year, mon, mday);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* date2isoweek()
|
2001-03-22 05:01:46 +01:00
|
|
|
*
|
2000-08-29 06:41:48 +02:00
|
|
|
* Returns ISO week number of year.
|
|
|
|
*/
|
|
|
|
int
|
2001-03-22 05:01:46 +01:00
|
|
|
date2isoweek(int year, int mon, int mday)
|
2000-08-29 06:41:48 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
float8 result;
|
|
|
|
int day0,
|
|
|
|
day4,
|
|
|
|
dayn;
|
|
|
|
|
|
|
|
/* current day */
|
2000-08-29 06:41:48 +02:00
|
|
|
dayn = date2j(year, mon, mday);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* fourth day of current year */
|
|
|
|
day4 = date2j(year, 1, 4);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* day0 == offset to first day of week (Monday) */
|
|
|
|
day0 = (j2day(day4 - 1) % 7);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need the first week containing a Thursday, otherwise this day
|
|
|
|
* falls into the previous year for purposes of counting weeks
|
2000-08-29 06:41:48 +02:00
|
|
|
*/
|
|
|
|
if (dayn < (day4 - day0))
|
|
|
|
{
|
|
|
|
day4 = date2j((year - 1), 1, 4);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* day0 == offset to first day of week (Monday) */
|
|
|
|
day0 = (j2day(day4 - 1) % 7);
|
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
result = (((dayn - (day4 - day0)) / 7) + 1);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sometimes the last few days in a year will fall into the first week
|
|
|
|
* of the next year, so check for this.
|
2000-08-29 06:41:48 +02:00
|
|
|
*/
|
|
|
|
if (result >= 53)
|
|
|
|
{
|
|
|
|
day4 = date2j((year + 1), 1, 4);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
/* day0 == offset to first day of week (Monday) */
|
|
|
|
day0 = (j2day(day4 - 1) % 7);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
if (dayn >= (day4 - day0))
|
|
|
|
result = (((dayn - (day4 - day0)) / 7) + 1);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-08-29 06:41:48 +02:00
|
|
|
return (int) result;
|
2001-03-22 05:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 18:26:26 +01:00
|
|
|
/* timestamp_part()
|
|
|
|
* Extract specified field from timestamp.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_part(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
|
|
|
|
float8 result;
|
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 18:26:26 +01:00
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
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 18:26:26 +01:00
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
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 18:26:26 +01:00
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
2001-10-18 19:30:21 +02:00
|
|
|
if (type == UNKNOWN_FIELD)
|
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 18:26:26 +01:00
|
|
|
type = DecodeSpecial(0, lowunits, &val);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
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 18:26:26 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
if ((type == UNITS)
|
|
|
|
&& (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0))
|
2001-09-28 10:09:14 +02:00
|
|
|
{
|
|
|
|
switch (val)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_MICROSEC:
|
2002-03-04 04:55:50 +01:00
|
|
|
result = (tm->tm_sec + fsec) * 1000000;
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
2002-03-04 04:55:50 +01:00
|
|
|
result = (tm->tm_sec + fsec) * 1000;
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_SECOND:
|
|
|
|
result = (tm->tm_sec + fsec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MINUTE:
|
|
|
|
result = tm->tm_min;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_HOUR:
|
|
|
|
result = tm->tm_hour;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DAY:
|
|
|
|
result = tm->tm_mday;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MONTH:
|
|
|
|
result = tm->tm_mon;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_QUARTER:
|
|
|
|
result = ((tm->tm_mon - 1) / 3) + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_WEEK:
|
|
|
|
result = (float8) date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_YEAR:
|
|
|
|
result = tm->tm_year;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DECADE:
|
|
|
|
result = (tm->tm_year / 10);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_CENTURY:
|
|
|
|
result = (tm->tm_year / 100);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
result = (tm->tm_year / 1000);
|
|
|
|
break;
|
|
|
|
|
2001-12-29 19:31:48 +01:00
|
|
|
case DTK_JULIAN:
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
|
|
|
|
result += (((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec) / 86400e0);
|
|
|
|
break;
|
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
case DTK_TZ:
|
|
|
|
case DTK_TZ_MINUTE:
|
|
|
|
case DTK_TZ_HOUR:
|
2001-09-28 10:09:14 +02:00
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == RESERV)
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_EPOCH:
|
|
|
|
result = timestamp - SetEpochTimestamp();
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_DOW:
|
2001-10-03 07:29:27 +02:00
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to encode TIMESTAMP");
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_DOY:
|
2001-10-03 07:29:27 +02:00
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to encode TIMESTAMP");
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
|
|
|
|
- date2j(tm->tm_year, 1, 1) + 1);
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP units '%s' not recognized", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
2000-04-14 17:22:10 +02:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_part()
|
|
|
|
* Extract specified field from timestamp with time zone.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_part(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
|
2001-09-28 10:09:14 +02:00
|
|
|
float8 result;
|
|
|
|
int tz;
|
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double dummy;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not recognized",
|
2001-09-28 10:09:14 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(units))));
|
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
|
|
|
*lp = '\0';
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
2001-10-18 19:30:21 +02:00
|
|
|
if (type == UNKNOWN_FIELD)
|
2001-09-28 10:09:14 +02:00
|
|
|
type = DecodeSpecial(0, lowunits, &val);
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
{
|
|
|
|
result = 0;
|
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-12-29 19:31:48 +01:00
|
|
|
if ((type == UNITS)
|
|
|
|
&& (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) == 0))
|
2001-09-28 10:09:14 +02:00
|
|
|
{
|
|
|
|
switch (val)
|
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 18:26:26 +01:00
|
|
|
{
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_TZ:
|
|
|
|
result = tz;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_TZ_MINUTE:
|
|
|
|
result = tz / 60;
|
|
|
|
TMODULO(result, dummy, 60e0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_TZ_HOUR:
|
|
|
|
dummy = tz;
|
|
|
|
TMODULO(dummy, result, 3600e0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MICROSEC:
|
2002-03-04 04:55:50 +01:00
|
|
|
result = (tm->tm_sec + fsec) * 1000000;
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
2002-03-04 04:55:50 +01:00
|
|
|
result = (tm->tm_sec + fsec) * 1000;
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_SECOND:
|
|
|
|
result = (tm->tm_sec + fsec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MINUTE:
|
|
|
|
result = tm->tm_min;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_HOUR:
|
|
|
|
result = tm->tm_hour;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DAY:
|
|
|
|
result = tm->tm_mday;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MONTH:
|
|
|
|
result = tm->tm_mon;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_QUARTER:
|
|
|
|
result = ((tm->tm_mon - 1) / 3) + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_WEEK:
|
|
|
|
result = (float8) date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_YEAR:
|
|
|
|
result = tm->tm_year;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DECADE:
|
|
|
|
result = (tm->tm_year / 10);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_CENTURY:
|
|
|
|
result = (tm->tm_year / 100);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
result = (tm->tm_year / 1000);
|
|
|
|
break;
|
|
|
|
|
2001-12-29 19:31:48 +01:00
|
|
|
case DTK_JULIAN:
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
|
|
|
|
result += (((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec) / 86400e0);
|
|
|
|
break;
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
else if (type == RESERV)
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_EPOCH:
|
|
|
|
result = timestamp - SetEpochTimestamp();
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_DOW:
|
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to encode TIMESTAMP WITH TIME ZONE");
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
case DTK_DOY:
|
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to encode TIMESTAMP WITH TIME ZONE");
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
|
|
|
|
- date2j(tm->tm_year, 1, 1) + 1);
|
|
|
|
break;
|
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 18:26:26 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not supported", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "TIMESTAMP WITH TIME ZONE units '%s' not recognized", lowunits);
|
2001-09-28 10:09:14 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* interval_part()
|
|
|
|
* Extract specified field from interval.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
interval_part(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
Interval *interval = PG_GETARG_INTERVAL_P(1);
|
|
|
|
float8 result;
|
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 18:26:26 +01:00
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowunits[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(units) - VARHDRSZ > MAXDATELEN)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-03-22 05:01:46 +01:00
|
|
|
PointerGetDatum(units))));
|
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 18:26:26 +01:00
|
|
|
up = VARDATA(units);
|
|
|
|
lp = lowunits;
|
|
|
|
for (i = 0; i < (VARSIZE(units) - VARHDRSZ); i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
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 18:26:26 +01:00
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
2001-10-18 19:30:21 +02:00
|
|
|
if (type == UNKNOWN_FIELD)
|
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 18:26:26 +01:00
|
|
|
type = DecodeSpecial(0, lowunits, &val);
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
if (type == UNITS)
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
if (interval2tm(*interval, tm, &fsec) == 0)
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_MICROSEC:
|
2000-11-11 20:55:19 +01:00
|
|
|
result = ((tm->tm_sec + fsec) * 1000000);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
2000-11-11 20:55:19 +01:00
|
|
|
result = ((tm->tm_sec + fsec) * 1000);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_SECOND:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (tm->tm_sec + fsec);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MINUTE:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = tm->tm_min;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_HOUR:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = tm->tm_hour;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DAY:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = tm->tm_mday;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MONTH:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = tm->tm_mon;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_QUARTER:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (tm->tm_mon / 4) + 1;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_YEAR:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = tm->tm_year;
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DECADE:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (tm->tm_year / 10);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_CENTURY:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (tm->tm_year / 100);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
2000-04-14 17:22:10 +02:00
|
|
|
case DTK_MILLENNIUM:
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (tm->tm_year / 1000);
|
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 18:26:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not supported",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
2000-06-09 03:11:16 +02:00
|
|
|
result = 0;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-06 07:10:59 +01:00
|
|
|
elog(WARNING, "Unable to decode INTERVAL"
|
2001-09-28 10:09:14 +02:00
|
|
|
"\n\tinterval_part() internal coding error");
|
2000-06-09 03:11:16 +02:00
|
|
|
result = 0;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((type == RESERV) && (val == DTK_EPOCH))
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
result = interval->time;
|
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 18:26:26 +01:00
|
|
|
if (interval->month != 0)
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
result += ((365.25 * 86400) * (interval->month / 12));
|
|
|
|
result += ((30 * 86400) * (interval->month % 12));
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "INTERVAL units '%s' not recognized",
|
2000-07-06 01:12:09 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2001-10-25 07:50:21 +02:00
|
|
|
PointerGetDatum(units))));
|
2000-06-09 03:11:16 +02:00
|
|
|
result = 0;
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* timestamp_zone()
|
|
|
|
* Encode timestamp type with specified time zone.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_zone(PG_FUNCTION_ARGS)
|
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 18:26:26 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
text *zone = PG_GETARG_TEXT_P(0);
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2001-09-28 10:09:14 +02:00
|
|
|
int tz;
|
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowzone[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(zone) - VARHDRSZ > MAXDATELEN)
|
|
|
|
elog(ERROR, "Time zone '%s' not recognized",
|
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(zone))));
|
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
PG_RETURN_TIMESTAMPTZ(timestamp);
|
|
|
|
|
|
|
|
up = VARDATA(zone);
|
|
|
|
lp = lowzone;
|
|
|
|
for (i = 0; i < (VARSIZE(zone) - VARHDRSZ); i++)
|
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeSpecial(0, lowzone, &val);
|
|
|
|
|
|
|
|
if ((type == TZ) || (type == DTZ))
|
|
|
|
{
|
|
|
|
tz = val * 60;
|
|
|
|
result = timestamp - tz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elog(ERROR, "Time zone '%s' not recognized", lowzone);
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
} /* timestamp_zone() */
|
|
|
|
|
|
|
|
/* timestamp_izone()
|
|
|
|
* Encode timestamp type with specified time interval as time zone.
|
|
|
|
* Require ISO-formatted result, since character-string time zone is not available.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamp_izone(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *zone = PG_GETARG_INTERVAL_P(0);
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2001-09-28 10:09:14 +02:00
|
|
|
int tz;
|
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
PG_RETURN_TIMESTAMPTZ(timestamp);
|
|
|
|
|
|
|
|
if (zone->month != 0)
|
|
|
|
elog(ERROR, "INTERVAL time zone '%s' not legal (month specified)",
|
|
|
|
DatumGetCString(DirectFunctionCall1(interval_out,
|
|
|
|
PointerGetDatum(zone))));
|
|
|
|
|
|
|
|
tz = -(zone->time);
|
|
|
|
result = timestamp - tz;
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
} /* timestamp_izone() */
|
|
|
|
|
|
|
|
/* timestamp_timestamptz()
|
|
|
|
* Convert local timestamp to timestamp at GMT
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamp_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2001-09-28 10:09:14 +02:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
int tz;
|
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
result = timestamp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to convert TIMESTAMP to TIMESTAMP WITH TIME ZONE (tm)");
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
tz = DetermineLocalTimeZone(tm);
|
|
|
|
|
|
|
|
if (tm2timestamp(tm, fsec, &tz, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to convert TIMESTAMP to TIMESTAMP WITH TIME ZONE");
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timestamptz_timestamp()
|
|
|
|
* Convert timestamp at GMT to local timestamp
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
2001-09-28 10:09:14 +02:00
|
|
|
Timestamp result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
|
|
|
int tz;
|
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
result = timestamp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to convert TIMESTAMP WITH TIME ZONE to TIMESTAMP (tm)");
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
if (tm2timestamp(tm, fsec, NULL, &result) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to convert TIMESTAMP WITH TIME ZONE to TIMESTAMP");
|
2001-09-28 10:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timestamptz_zone()
|
|
|
|
* Encode timestamp with time zone type with specified time zone.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_zone(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *zone = PG_GETARG_TEXT_P(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
|
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 18:26:26 +01:00
|
|
|
text *result;
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz dt;
|
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 18:26:26 +01:00
|
|
|
int tz;
|
|
|
|
int type,
|
|
|
|
val;
|
|
|
|
int i;
|
|
|
|
char *up,
|
|
|
|
*lp,
|
|
|
|
lowzone[MAXDATELEN + 1];
|
|
|
|
char *tzn,
|
|
|
|
upzone[MAXDATELEN + 1];
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
int len;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (VARSIZE(zone) - VARHDRSZ > MAXDATELEN)
|
2000-07-06 01:12:09 +02:00
|
|
|
elog(ERROR, "Time zone '%s' not recognized",
|
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(zone))));
|
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 18:26:26 +01:00
|
|
|
up = VARDATA(zone);
|
|
|
|
lp = lowzone;
|
|
|
|
for (i = 0; i < (VARSIZE(zone) - VARHDRSZ); i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*lp++ = tolower((unsigned char) *up++);
|
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 18:26:26 +01:00
|
|
|
*lp = '\0';
|
|
|
|
|
|
|
|
type = DecodeSpecial(0, lowzone, &val);
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TEXT_P(pstrdup(""));
|
|
|
|
|
|
|
|
if ((type == TZ) || (type == DTZ))
|
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 18:26:26 +01:00
|
|
|
{
|
|
|
|
tm->tm_isdst = ((type == DTZ) ? 1 : 0);
|
|
|
|
tz = val * 60;
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
dt = dt2local(timestamp, tz);
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
if (timestamp2tm(dt, NULL, tm, &fsec, NULL) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to decode TIMESTAMP WITH TIME ZONE"
|
|
|
|
"\n\ttimestamptz_zone() internal coding error");
|
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 18:26:26 +01:00
|
|
|
|
|
|
|
up = upzone;
|
|
|
|
lp = lowzone;
|
|
|
|
for (i = 0; *lp != '\0'; i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
*up++ = toupper((unsigned char) *lp++);
|
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 18:26:26 +01:00
|
|
|
*up = '\0';
|
|
|
|
|
|
|
|
tzn = upzone;
|
|
|
|
EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
|
|
|
|
|
|
|
|
len = (strlen(buf) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = len;
|
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 18:26:26 +01:00
|
|
|
memmove(VARDATA(result), buf, (len - VARHDRSZ));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elog(ERROR, "Time zone '%s' not recognized", lowzone);
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TEXT_P(pstrdup(""));
|
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 18:26:26 +01:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
2001-09-28 10:09:14 +02:00
|
|
|
} /* timestamptz_zone() */
|
2000-11-06 16:57:00 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_izone()
|
|
|
|
* Encode timestamp with time zone type with specified time interval as time zone.
|
2000-11-06 16:57:00 +01:00
|
|
|
* Require ISO-formatted result, since character-string time zone is not available.
|
|
|
|
*/
|
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamptz_izone(PG_FUNCTION_ARGS)
|
2000-11-06 16:57:00 +01:00
|
|
|
{
|
|
|
|
Interval *zone = PG_GETARG_INTERVAL_P(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
|
2000-11-06 16:57:00 +01:00
|
|
|
text *result;
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz dt;
|
2000-11-06 16:57:00 +01:00
|
|
|
int tz;
|
|
|
|
char *tzn = "";
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_TEXT_P(pstrdup(""));
|
2000-11-06 16:57:00 +01:00
|
|
|
|
|
|
|
if (zone->month != 0)
|
2001-09-28 10:09:14 +02:00
|
|
|
elog(ERROR, "INTERVAL time zone '%s' not legal (month specified)",
|
|
|
|
DatumGetCString(DirectFunctionCall1(interval_out,
|
|
|
|
PointerGetDatum(zone))));
|
2000-11-06 16:57:00 +01:00
|
|
|
|
|
|
|
tm->tm_isdst = -1;
|
|
|
|
tz = -(zone->time);
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
dt = dt2local(timestamp, tz);
|
2000-11-06 16:57:00 +01:00
|
|
|
|
|
|
|
if (timestamp2tm(dt, NULL, tm, &fsec, NULL) != 0)
|
2001-10-18 19:30:21 +02:00
|
|
|
elog(ERROR, "Unable to decode TIMESTAMP WITH TIME ZONE"
|
2001-10-25 07:50:21 +02:00
|
|
|
"\n\ttimestamptz_izone() internal coding error");
|
2000-11-06 16:57:00 +01:00
|
|
|
|
|
|
|
EncodeDateTime(tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);
|
|
|
|
len = (strlen(buf) + VARHDRSZ);
|
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), buf, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
2001-09-28 10:09:14 +02:00
|
|
|
} /* timestamptz_izone() */
|