1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* date.c
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* implements DATE and TIME data types specified in SQL-92 standard
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* Portions Copyright (c) 1994-5, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-03-05 16:59:11 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.124 2006/03/05 15:58:41 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
2000-06-19 05:55:01 +02:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
#include <ctype.h>
|
2000-06-19 05:55:01 +02:00
|
|
|
#include <limits.h>
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <float.h>
|
2005-10-15 04:49:52 +02:00
|
|
|
#include <time.h>
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
#include "access/hash.h"
|
2003-05-13 01:08:52 +02:00
|
|
|
#include "libpq/pqformat.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
2004-05-07 02:24:59 +02:00
|
|
|
#include "parser/scansup.h"
|
2001-09-28 10:09:14 +02:00
|
|
|
#include "utils/builtins.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "utils/date.h"
|
|
|
|
#include "utils/nabstime.h"
|
2001-09-28 10:09:14 +02:00
|
|
|
#include "utils/timestamp.h"
|
1997-03-15 00:21:12 +01:00
|
|
|
|
2002-09-21 21:52:41 +02:00
|
|
|
/*
|
|
|
|
* gcc's -ffast-math switch breaks routines that expect exact results from
|
2005-07-21 05:56:25 +02:00
|
|
|
* expressions like timeval / SECS_PER_HOUR, where timeval is double.
|
2002-09-21 21:52:41 +02:00
|
|
|
*/
|
|
|
|
#ifdef __FAST_MATH__
|
|
|
|
#error -ffast-math is known to break this code
|
|
|
|
#endif
|
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
static int time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec);
|
|
|
|
static int timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp);
|
|
|
|
static int tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result);
|
|
|
|
static int tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result);
|
2001-10-04 17:14:22 +02:00
|
|
|
static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
|
2001-10-03 07:29:27 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
* Date ADT
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
|
|
|
/* date_in()
|
|
|
|
* Given date text string, convert to internal date format.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_in(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADT date;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
1997-09-08 04:41:22 +02:00
|
|
|
*tm = &tt;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int tzp;
|
1997-09-08 04:41:22 +02:00
|
|
|
int dtype;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int nf;
|
2003-08-28 01:29:29 +02:00
|
|
|
int dterr;
|
1997-09-08 04:41:22 +02:00
|
|
|
char *field[MAXDATEFIELDS];
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int ftype[MAXDATEFIELDS];
|
2005-05-26 04:04:14 +02:00
|
|
|
char workbuf[MAXDATELEN + 1];
|
1997-03-15 00:21:12 +01:00
|
|
|
|
2005-05-26 04:04:14 +02:00
|
|
|
dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
|
|
|
|
field, ftype, MAXDATEFIELDS, &nf);
|
2003-08-28 01:29:29 +02:00
|
|
|
if (dterr == 0)
|
|
|
|
dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp);
|
|
|
|
if (dterr != 0)
|
|
|
|
DateTimeParseError(dterr, str, "date");
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (dtype)
|
|
|
|
{
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
case DTK_DATE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_CURRENT:
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("date/time value \"current\" is no longer supported")));
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2002-06-11 15:40:53 +02:00
|
|
|
GetCurrentDateTime(tm);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_EPOCH:
|
2001-09-28 10:09:14 +02:00
|
|
|
GetEpochTime(tm);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
break;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
2003-08-28 01:29:29 +02:00
|
|
|
DateTimeParseError(DTERR_BAD_FORMAT, str, "date");
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-03-15 00:21:12 +01:00
|
|
|
|
2006-02-09 04:39:17 +01:00
|
|
|
if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("date out of range: \"%s\"", str)));
|
|
|
|
|
2003-04-04 06:50:44 +02:00
|
|
|
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(date);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* date_out()
|
|
|
|
* Given internal format date, convert to text string.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_out(PG_FUNCTION_ARGS)
|
1997-07-29 17:54:49 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
1997-09-08 04:41:22 +02:00
|
|
|
*tm = &tt;
|
|
|
|
char buf[MAXDATELEN + 1];
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
j2date(date + POSTGRES_EPOCH_JDATE,
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
1997-07-29 17:54:49 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
EncodeDateOnly(tm, DateStyle, buf);
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
/*
|
|
|
|
* date_recv - converts external binary format to date
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
|
|
|
|
PG_RETURN_DATEADT((DateADT) pq_getmsgint(buf, sizeof(DateADT)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* date_send - converts date to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_send(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
2003-05-13 01:08:52 +02:00
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendint(&buf, date, sizeof(date));
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
/*
|
|
|
|
* Comparison functions for dates
|
|
|
|
*/
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(dateVal1 == dateVal2);
|
|
|
|
}
|
1997-07-29 17:54:49 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_ne(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 != dateVal2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_lt(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 < dateVal2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_le(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(dateVal1 <= dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_gt(PG_FUNCTION_ARGS)
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 > dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_ge(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(dateVal1 >= dateVal2);
|
|
|
|
}
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_cmp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
if (dateVal1 < dateVal2)
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INT32(-1);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
else if (dateVal1 > dateVal2)
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INT32(1);
|
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_larger(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT((dateVal1 > dateVal2) ? dateVal1 : dateVal2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_smaller(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_DATEADT((dateVal1 < dateVal2) ? dateVal1 : dateVal2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Compute difference between two dates in days.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_mi(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal1 = PG_GETARG_DATEADT(0);
|
|
|
|
DateADT dateVal2 = PG_GETARG_DATEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_INT32((int32) (dateVal1 - dateVal2));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Add a number of days to a date, giving a new date.
|
|
|
|
* Must handle both positive and negative numbers of days.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_pli(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
int32 days = PG_GETARG_INT32(1);
|
|
|
|
|
|
|
|
PG_RETURN_DATEADT(dateVal + days);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* Subtract a number of days from a date, giving a new date.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_mii(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
int32 days = PG_GETARG_INT32(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(dateVal - days);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
/*
|
|
|
|
* Internal routines for promoting date to timestamp and timestamp with
|
|
|
|
* time zone
|
2002-04-21 21:52:18 +02:00
|
|
|
*/
|
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
/* date is days since 2000, timestamp is microseconds since same... */
|
|
|
|
#define date2timestamp(dateVal) \
|
2005-05-23 20:56:55 +02:00
|
|
|
((Timestamp) ((dateVal) * USECS_PER_DAY))
|
2004-02-14 21:16:18 +01:00
|
|
|
#else
|
|
|
|
/* date is days since 2000, timestamp is seconds since same... */
|
|
|
|
#define date2timestamp(dateVal) \
|
2005-05-23 20:56:55 +02:00
|
|
|
((Timestamp) ((dateVal) * (double)SECS_PER_DAY))
|
2004-02-14 21:16:18 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static TimestampTz
|
|
|
|
date2timestamptz(DateADT dateVal)
|
|
|
|
{
|
|
|
|
TimestampTz result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2004-02-14 21:16:18 +01:00
|
|
|
*tm = &tt;
|
2004-06-03 04:08:07 +02:00
|
|
|
int tz;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
j2date(dateVal + POSTGRES_EPOCH_JDATE,
|
|
|
|
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
|
|
|
|
2004-06-03 04:08:07 +02:00
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_min = 0;
|
|
|
|
tm->tm_sec = 0;
|
2005-07-23 16:25:34 +02:00
|
|
|
tz = DetermineTimeZoneOffset(tm, global_timezone);
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
|
2004-02-14 21:16:18 +01:00
|
|
|
#else
|
2005-10-15 04:49:52 +02:00
|
|
|
result = dateVal * (double) SECS_PER_DAY + tz;
|
2004-02-14 21:16:18 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Crosstype comparison functions for dates
|
|
|
|
*/
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_eq_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_ne_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_lt_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_gt_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_le_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_ge_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_cmp_timestamp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
|
|
|
|
Timestamp dt1;
|
|
|
|
|
|
|
|
dt1 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_eq_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_ne_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_lt_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_gt_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_le_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_ge_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
date_cmp_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
|
|
|
|
TimestampTz dt1;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt1 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_eq_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_ne_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_lt_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_gt_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_le_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_ge_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamp_cmp_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
|
|
|
Timestamp dt2;
|
|
|
|
|
|
|
|
dt2 = date2timestamp(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_eq_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_ne_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_lt_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_gt_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_le_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_ge_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(timestamptz_cmp_internal(dt1, dt2) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
timestamptz_cmp_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
|
2004-02-14 21:16:18 +01:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
TimestampTz dt2;
|
2004-02-14 21:16:18 +01:00
|
|
|
|
|
|
|
dt2 = date2timestamptz(dateVal);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(timestamptz_cmp_internal(dt1, dt2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
/* Add an interval to a date, giving a new date.
|
|
|
|
* Must handle both positive and negative intervals.
|
2004-02-14 21:16:18 +01:00
|
|
|
*
|
|
|
|
* We implement this by promoting the date to timestamp (without time zone)
|
|
|
|
* and then using the timestamp plus interval function.
|
2002-04-21 21:52:18 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_pl_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
2004-02-14 21:16:18 +01:00
|
|
|
Timestamp dateStamp;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
dateStamp = date2timestamp(dateVal);
|
2002-04-21 21:52:18 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
return DirectFunctionCall2(timestamp_pl_interval,
|
|
|
|
TimestampGetDatum(dateStamp),
|
|
|
|
PointerGetDatum(span));
|
2002-04-21 21:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Subtract an interval from a date, giving a new date.
|
|
|
|
* Must handle both positive and negative intervals.
|
2004-02-14 21:16:18 +01:00
|
|
|
*
|
|
|
|
* We implement this by promoting the date to timestamp (without time zone)
|
|
|
|
* and then using the timestamp minus interval function.
|
2002-04-21 21:52:18 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_mi_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
2004-02-14 21:16:18 +01:00
|
|
|
Timestamp dateStamp;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
dateStamp = date2timestamp(dateVal);
|
2002-04-21 21:52:18 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
return DirectFunctionCall2(timestamp_mi_interval,
|
|
|
|
TimestampGetDatum(dateStamp),
|
|
|
|
PointerGetDatum(span));
|
2002-04-21 21:52:18 +02:00
|
|
|
}
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* date_timestamp()
|
|
|
|
* Convert date to timestamp data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
date_timestamp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
|
|
|
Timestamp result;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
result = date2timestamp(dateVal);
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* timestamp_date()
|
|
|
|
* Convert timestamp to date data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamp_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
2001-09-28 10:09:14 +02:00
|
|
|
DateADT result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2003-07-24 02:21:26 +02:00
|
|
|
*tm = &tt;
|
2003-07-24 06:38:19 +02:00
|
|
|
fsec_t fsec;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("timestamp out of range")));
|
2003-07-24 02:21:26 +02:00
|
|
|
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
PG_RETURN_DATEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* date_timestamptz()
|
|
|
|
* Convert date to timestamp with time zone data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_timestamptz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DateADT dateVal = PG_GETARG_DATEADT(0);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2000-09-12 07:41:37 +02:00
|
|
|
|
2004-02-14 21:16:18 +01:00
|
|
|
result = date2timestamptz(dateVal);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timestamptz_date()
|
|
|
|
* Convert timestamp with time zone to date data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamptz_date(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
DateADT result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
*tm = &tt;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2001-09-28 10:09:14 +02:00
|
|
|
int tz;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char *tzn;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_NULL();
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("timestamp out of range")));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-04-04 06:50:44 +02:00
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(result);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* abstime_date()
|
|
|
|
* Convert abstime to date data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
abstime_date(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
DateADT result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
1997-09-20 18:17:45 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
switch (abstime)
|
|
|
|
{
|
|
|
|
case INVALID_ABSTIME:
|
|
|
|
case NOSTART_ABSTIME:
|
|
|
|
case NOEND_ABSTIME:
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("cannot convert reserved abstime value to date")));
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* pretend to drop through to make compiler think that result will
|
|
|
|
* be set
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
default:
|
|
|
|
abstime2tm(abstime, &tz, tm, NULL);
|
2003-04-04 06:50:44 +02:00
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
break;
|
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_DATEADT(result);
|
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
|
|
|
|
2000-11-11 20:55:19 +01:00
|
|
|
/* date_text()
|
|
|
|
* Convert date to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
date_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Date, but may as well leave it in Datum form */
|
2005-10-15 04:49:52 +02:00
|
|
|
Datum date = PG_GETARG_DATUM(0);
|
2000-11-11 20:55:19 +01:00
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(date_out, date));
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
len = strlen(str) + VARHDRSZ;
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_date()
|
|
|
|
* Convert text string to date.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_date(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("invalid input syntax for type date: \"%s\"",
|
2003-08-04 02:43:34 +02:00
|
|
|
VARDATA(str))));
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
sp = VARDATA(str);
|
|
|
|
dp = dstr;
|
|
|
|
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
return DirectFunctionCall1(date_in,
|
|
|
|
CStringGetDatum(dstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Time ADT
|
|
|
|
*****************************************************************************/
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_in(PG_FUNCTION_ARGS)
|
1997-09-20 18:17:45 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
2002-09-04 22:31:48 +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);
|
|
|
|
TimeADT result;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
*tm = &tt;
|
2003-01-31 02:08:08 +01:00
|
|
|
int tz;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
int nf;
|
2003-08-28 01:29:29 +02:00
|
|
|
int dterr;
|
2005-05-26 04:04:14 +02:00
|
|
|
char workbuf[MAXDATELEN + 1];
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int dtype;
|
|
|
|
int ftype[MAXDATEFIELDS];
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2005-05-26 04:04:14 +02:00
|
|
|
dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
|
|
|
|
field, ftype, MAXDATEFIELDS, &nf);
|
2003-08-28 01:29:29 +02:00
|
|
|
if (dterr == 0)
|
|
|
|
dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
|
|
|
|
if (dterr != 0)
|
|
|
|
DateTimeParseError(dterr, str, "time");
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
tm2time(tm, fsec, &result);
|
2001-10-03 07:29:27 +02:00
|
|
|
AdjustTimeForTypmod(&result, typmod);
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
PG_RETURN_TIMEADT(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
/* tm2time()
|
|
|
|
* Convert a tm structure to a time data type.
|
|
|
|
*/
|
2002-09-21 21:52:41 +02:00
|
|
|
static int
|
2005-10-15 04:49:52 +02:00
|
|
|
tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 20:06:13 +02:00
|
|
|
*result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
|
2005-10-15 04:49:52 +02:00
|
|
|
* USECS_PER_SEC) + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 20:06:13 +02:00
|
|
|
*result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time2tm()
|
|
|
|
* Convert time data type to POSIX time structure.
|
|
|
|
* 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
|
|
|
|
*/
|
2002-09-21 21:52:41 +02:00
|
|
|
static int
|
2005-10-15 04:49:52 +02:00
|
|
|
time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
tm->tm_hour = time / USECS_PER_HOUR;
|
|
|
|
time -= tm->tm_hour * USECS_PER_HOUR;
|
|
|
|
tm->tm_min = time / USECS_PER_MINUTE;
|
|
|
|
time -= tm->tm_min * USECS_PER_MINUTE;
|
|
|
|
tm->tm_sec = time / USECS_PER_SEC;
|
|
|
|
time -= tm->tm_sec * USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
*fsec = time;
|
|
|
|
#else
|
|
|
|
double trem;
|
|
|
|
|
2005-10-09 19:21:47 +02:00
|
|
|
recalc:
|
2002-04-21 21:52:18 +02:00
|
|
|
trem = time;
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
|
|
|
|
TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
|
2005-07-12 17:17:44 +02:00
|
|
|
TMODULO(trem, tm->tm_sec, 1.0);
|
2005-10-09 19:21:47 +02:00
|
|
|
trem = TIMEROUND(trem);
|
|
|
|
/* roundoff may need to propagate to higher-order fields */
|
|
|
|
if (trem >= 1.0)
|
|
|
|
{
|
|
|
|
time = ceil(time);
|
|
|
|
goto recalc;
|
|
|
|
}
|
2002-04-21 21:52:18 +02:00
|
|
|
*fsec = trem;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_out(PG_FUNCTION_ARGS)
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char *result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
*tm = &tt;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
char buf[MAXDATELEN + 1];
|
1997-09-20 18:17:45 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
time2tm(time, tm, &fsec);
|
2000-03-15 00:06:59 +01:00
|
|
|
EncodeTimeOnly(tm, fsec, NULL, DateStyle, buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
/*
|
|
|
|
* time_recv - converts external binary format to time
|
|
|
|
*
|
|
|
|
* We make no attempt to provide compatibility between int and float
|
|
|
|
* time representations ...
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-07-10 23:14:00 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid typelem = PG_GETARG_OID(1);
|
|
|
|
#endif
|
|
|
|
int32 typmod = PG_GETARG_INT32(2);
|
|
|
|
TimeADT result;
|
2003-05-13 01:08:52 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-10 23:14:00 +02:00
|
|
|
result = pq_getmsgint64(buf);
|
2003-05-13 01:08:52 +02:00
|
|
|
#else
|
2005-07-10 23:14:00 +02:00
|
|
|
result = pq_getmsgfloat8(buf);
|
2003-05-13 01:08:52 +02:00
|
|
|
#endif
|
2005-07-10 23:14:00 +02:00
|
|
|
|
|
|
|
AdjustTimeForTypmod(&result, typmod);
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
2003-05-13 01:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* time_send - converts time to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_send(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
pq_sendint64(&buf, time);
|
|
|
|
#else
|
|
|
|
pq_sendfloat8(&buf, time);
|
|
|
|
#endif
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
/* time_scale()
|
|
|
|
* Adjust time type for specified scale factor.
|
|
|
|
* Used by PostgreSQL type system to stuff columns.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_scale(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
int32 typmod = PG_GETARG_INT32(1);
|
|
|
|
TimeADT result;
|
|
|
|
|
|
|
|
result = time;
|
|
|
|
AdjustTimeForTypmod(&result, typmod);
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
2002-08-04 08:44:47 +02:00
|
|
|
/* AdjustTimeForTypmod()
|
|
|
|
* Force the precision of the time value to a specified value.
|
|
|
|
* Uses *exactly* the same code as in AdjustTimestampForTypemod()
|
|
|
|
* but we make a separate copy because those types do not
|
|
|
|
* have a fundamental tie together but rather a coincidence of
|
|
|
|
* implementation. - thomas
|
|
|
|
*/
|
2001-10-03 07:29:27 +02:00
|
|
|
static void
|
|
|
|
AdjustTimeForTypmod(TimeADT *time, int32 typmod)
|
|
|
|
{
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2003-01-29 02:08:42 +01:00
|
|
|
static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
|
2002-08-04 08:44:47 +02:00
|
|
|
INT64CONST(1000000),
|
|
|
|
INT64CONST(100000),
|
|
|
|
INT64CONST(10000),
|
|
|
|
INT64CONST(1000),
|
|
|
|
INT64CONST(100),
|
|
|
|
INT64CONST(10),
|
|
|
|
INT64CONST(1)
|
|
|
|
};
|
|
|
|
|
2003-01-29 02:08:42 +01:00
|
|
|
static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
|
2003-01-09 02:06:57 +01:00
|
|
|
INT64CONST(500000),
|
|
|
|
INT64CONST(50000),
|
|
|
|
INT64CONST(5000),
|
|
|
|
INT64CONST(500),
|
|
|
|
INT64CONST(50),
|
|
|
|
INT64CONST(5),
|
2002-08-04 08:44:47 +02:00
|
|
|
INT64CONST(0)
|
|
|
|
};
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2003-01-29 02:08:42 +01:00
|
|
|
/* note MAX_TIME_PRECISION differs in this case */
|
|
|
|
static const double TimeScales[MAX_TIME_PRECISION + 1] = {
|
2003-04-08 19:02:04 +02:00
|
|
|
1.0,
|
|
|
|
10.0,
|
|
|
|
100.0,
|
|
|
|
1000.0,
|
|
|
|
10000.0,
|
|
|
|
100000.0,
|
|
|
|
1000000.0,
|
|
|
|
10000000.0,
|
|
|
|
100000000.0,
|
|
|
|
1000000000.0,
|
|
|
|
10000000000.0
|
2002-08-04 08:44:47 +02:00
|
|
|
};
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-10-03 07:29:27 +02:00
|
|
|
|
2005-05-24 04:09:45 +02:00
|
|
|
if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
|
2002-08-04 08:44:47 +02:00
|
|
|
{
|
2003-01-09 02:06:57 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Note: this round-to-nearest code is not completely consistent about
|
|
|
|
* rounding values that are exactly halfway between integral values.
|
|
|
|
* On most platforms, rint() will implement round-to-nearest-even, but
|
|
|
|
* the integer code always rounds up (away from zero). Is it worth
|
|
|
|
* trying to be consistent?
|
2003-01-09 02:06:57 +01:00
|
|
|
*/
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2002-08-04 08:44:47 +02:00
|
|
|
if (*time >= INT64CONST(0))
|
2005-05-24 04:09:45 +02:00
|
|
|
*time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
|
2005-10-15 04:49:52 +02:00
|
|
|
TimeScales[typmod];
|
2002-08-04 08:44:47 +02:00
|
|
|
else
|
2005-05-24 04:09:45 +02:00
|
|
|
*time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
|
2005-10-15 04:49:52 +02:00
|
|
|
TimeScales[typmod]);
|
2003-01-09 02:06:57 +01:00
|
|
|
#else
|
2005-10-15 04:49:52 +02:00
|
|
|
*time = rint((double) *time * TimeScales[typmod]) / TimeScales[typmod];
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-10-03 07:29:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 == time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_ne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 != time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_lt(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 < time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_le(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 <= time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_gt(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 > time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_ge(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_BOOL(time1 >= time2);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_cmp(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (time1 < time2)
|
|
|
|
PG_RETURN_INT32(-1);
|
|
|
|
if (time1 > time2)
|
|
|
|
PG_RETURN_INT32(1);
|
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_larger(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMEADT((time1 > time2) ? time1 : time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_smaller(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT((time1 < time2) ? time1 : time2);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* overlaps_time() --- implements the SQL92 OVERLAPS operator.
|
|
|
|
*
|
|
|
|
* Algorithm is per SQL92 spec. This is much harder than you'd think
|
|
|
|
* because the spec requires us to deliver a non-null answer in some cases
|
|
|
|
* where some of the inputs are null.
|
2000-03-15 00:06:59 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
overlaps_time(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The arguments are TimeADT, but we leave them as generic Datums to avoid
|
|
|
|
* dereferencing nulls (TimeADT is pass-by-reference!)
|
2000-12-07 19:38:59 +01: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);
|
|
|
|
bool ts1IsNull = PG_ARGISNULL(0);
|
|
|
|
bool te1IsNull = PG_ARGISNULL(1);
|
|
|
|
bool ts2IsNull = PG_ARGISNULL(2);
|
|
|
|
bool te2IsNull = PG_ARGISNULL(3);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
#define TIMEADT_GT(t1,t2) \
|
|
|
|
(DatumGetTimeADT(t1) > DatumGetTimeADT(t2))
|
|
|
|
#define TIMEADT_LT(t1,t2) \
|
|
|
|
(DatumGetTimeADT(t1) < DatumGetTimeADT(t2))
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02: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)
|
|
|
|
{
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts1 = te1;
|
2000-12-07 19:38:59 +01:00
|
|
|
te1IsNull = true;
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
2000-12-07 19:38:59 +01:00
|
|
|
else if (!te1IsNull)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-12-07 19:38:59 +01:00
|
|
|
if (TIMEADT_GT(ts1, te1))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts1;
|
2000-12-07 19:38:59 +01:00
|
|
|
|
|
|
|
ts1 = te1;
|
|
|
|
te1 = tt;
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* Likewise for interval 2. */
|
|
|
|
if (ts2IsNull)
|
|
|
|
{
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts2 = te2;
|
2000-12-07 19:38:59 +01:00
|
|
|
te2IsNull = true;
|
|
|
|
}
|
|
|
|
else if (!te2IsNull)
|
|
|
|
{
|
|
|
|
if (TIMEADT_GT(ts2, te2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts2;
|
2000-12-07 19:38:59 +01:00
|
|
|
|
|
|
|
ts2 = te2;
|
|
|
|
te2 = tt;
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* At this point neither ts1 nor ts2 is null, so we can consider three
|
|
|
|
* cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
|
|
|
|
*/
|
|
|
|
if (TIMEADT_GT(ts1, ts2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This case is ts1 < te2 OR te1 < te2, which may look redundant but
|
|
|
|
* in the presence of nulls it's not quite completely so.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMEADT_LT(ts1, te2))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If te1 is not null then we had ts1 <= te1 above, and we just found
|
|
|
|
* ts1 >= te2, hence te1 >= te2.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else if (TIMEADT_LT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts2 < te1 OR te2 < te1 */
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMEADT_LT(ts2, te1))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If te2 is not null then we had ts2 <= te2 above, and we just found
|
|
|
|
* ts2 >= te1, hence te2 >= te1.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
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
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TIMEADT_GT
|
|
|
|
#undef TIMEADT_LT
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* timestamp_time()
|
|
|
|
* Convert timestamp to time data type.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timestamp_time(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
1997-09-08 04:41:22 +02:00
|
|
|
*tm = &tt;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_NULL();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("timestamp out of range")));
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
/*
|
2005-05-23 23:54:02 +02:00
|
|
|
* Could also do this with time = (timestamp / USECS_PER_DAY *
|
|
|
|
* USECS_PER_DAY) - timestamp;
|
2002-04-21 21:52:18 +02:00
|
|
|
*/
|
2005-07-21 20:06:13 +02:00
|
|
|
result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
|
2005-10-15 04:49:52 +02:00
|
|
|
USECS_PER_SEC) + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 20:06:13 +02:00
|
|
|
result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timestamptz_time()
|
|
|
|
* Convert timestamptz to time data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timestamptz_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
|
|
|
TimeADT result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2002-04-21 21:52:18 +02:00
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
fsec_t fsec;
|
|
|
|
char *tzn;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("timestamp out of range")));
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
/*
|
2005-05-23 23:54:02 +02:00
|
|
|
* Could also do this with time = (timestamp / USECS_PER_DAY *
|
|
|
|
* USECS_PER_DAY) - timestamp;
|
2002-04-21 21:52:18 +02:00
|
|
|
*/
|
2005-07-21 20:06:13 +02:00
|
|
|
result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
|
2005-10-15 04:49:52 +02:00
|
|
|
USECS_PER_SEC) + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 20:06:13 +02:00
|
|
|
result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
All regression tests pass except for rules.sql (unrelated).
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 19:17:02 +01:00
|
|
|
/* datetime_timestamp()
|
|
|
|
* Convert date and time to timestamp data type.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
datetime_timestamp(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time = PG_GETARG_TIMEADT(1);
|
|
|
|
Timestamp result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADTGetDatum(date)));
|
2000-06-09 03:11:16 +02:00
|
|
|
result += time;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
/* time_interval()
|
|
|
|
* Convert time to interval data type.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
time_interval(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
2000-03-15 00:06:59 +01:00
|
|
|
Interval *result;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result->time = time;
|
2005-07-20 18:42:32 +02:00
|
|
|
result->day = 0;
|
2000-03-15 00:06:59 +01:00
|
|
|
result->month = 0;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
/* interval_time()
|
|
|
|
* Convert interval to time data type.
|
2003-06-16 20:56:45 +02:00
|
|
|
*
|
|
|
|
* This is defined as producing the fractional-day portion of the interval.
|
2003-08-04 02:43:34 +02:00
|
|
|
* Therefore, we can just ignore the months field. It is not real clear
|
2003-06-16 20:56:45 +02:00
|
|
|
* what to do with negative intervals, but we choose to subtract the floor,
|
|
|
|
* so that, say, '-2 hours' becomes '22:00:00'.
|
2000-12-03 15:51:12 +01:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
interval_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(0);
|
|
|
|
TimeADT result;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2003-06-16 20:56:45 +02:00
|
|
|
int64 days;
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
result = span->time;
|
2005-05-23 20:56:55 +02:00
|
|
|
if (result >= USECS_PER_DAY)
|
2003-06-16 20:56:45 +02:00
|
|
|
{
|
2005-05-23 20:56:55 +02:00
|
|
|
days = result / USECS_PER_DAY;
|
|
|
|
result -= days * USECS_PER_DAY;
|
2003-06-16 20:56:45 +02:00
|
|
|
}
|
|
|
|
else if (result < 0)
|
|
|
|
{
|
2005-05-23 20:56:55 +02:00
|
|
|
days = (-result + USECS_PER_DAY - 1) / USECS_PER_DAY;
|
|
|
|
result += days * USECS_PER_DAY;
|
2003-06-16 20:56:45 +02:00
|
|
|
}
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2000-12-03 15:51:12 +01:00
|
|
|
result = span->time;
|
2005-10-15 04:49:52 +02:00
|
|
|
if (result >= (double) SECS_PER_DAY || result < 0)
|
|
|
|
result -= floor(result / (double) SECS_PER_DAY) * (double) SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-12-03 15:51:12 +01:00
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* time_mi_time()
|
|
|
|
* Subtract two times to produce an interval.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_mi_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time1 = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeADT time2 = PG_GETARG_TIMEADT(1);
|
|
|
|
Interval *result;
|
|
|
|
|
|
|
|
result = (Interval *) palloc(sizeof(Interval));
|
|
|
|
|
|
|
|
result->month = 0;
|
2005-07-20 18:42:32 +02:00
|
|
|
result->day = 0;
|
|
|
|
result->time = time1 - time2;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
PG_RETURN_INTERVAL_P(result);
|
|
|
|
}
|
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
/* time_pl_interval()
|
|
|
|
* Add interval to time.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_pl_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeADT result;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result = time + span->time;
|
|
|
|
result -= result / USECS_PER_DAY * USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
if (result < INT64CONST(0))
|
2005-05-23 20:56:55 +02:00
|
|
|
result += USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2000-12-03 15:51:12 +01:00
|
|
|
TimeADT time1;
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
result = time + span->time;
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(result, time1, (double) SECS_PER_DAY);
|
2000-12-03 15:51:12 +01:00
|
|
|
if (result < 0)
|
2005-05-23 23:54:02 +02:00
|
|
|
result += SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-12-03 15:51:12 +01:00
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* time_mi_interval()
|
|
|
|
* Subtract interval from time.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_mi_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeADT result;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result = time - span->time;
|
|
|
|
result -= result / USECS_PER_DAY * USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
if (result < INT64CONST(0))
|
2005-05-23 20:56:55 +02:00
|
|
|
result += USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2000-12-03 15:51:12 +01:00
|
|
|
TimeADT time1;
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
result = time - span->time;
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(result, time1, (double) SECS_PER_DAY);
|
2000-12-03 15:51:12 +01:00
|
|
|
if (result < 0)
|
2005-05-23 23:54:02 +02:00
|
|
|
result += SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-12-03 15:51:12 +01:00
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-11-11 20:55:19 +01:00
|
|
|
/* time_text()
|
|
|
|
* Convert time to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Time, but may as well leave it in Datum form */
|
|
|
|
Datum time = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(time_out, time));
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
len = strlen(str) + VARHDRSZ;
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_time()
|
|
|
|
* Convert text string to time.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("invalid input syntax for type time: \"%s\"",
|
2003-08-04 02:43:34 +02:00
|
|
|
VARDATA(str))));
|
2000-11-11 20:55:19 +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(time_in,
|
|
|
|
CStringGetDatum(dstr),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1));
|
2000-11-11 20:55:19 +01:00
|
|
|
}
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
/* time_part()
|
|
|
|
* Extract specified field from time type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
time_part(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(1);
|
|
|
|
float8 result;
|
|
|
|
int type,
|
|
|
|
val;
|
2004-05-07 02:24:59 +02:00
|
|
|
char *lowunits;
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
lowunits = downcase_truncate_identifier(VARDATA(units),
|
|
|
|
VARSIZE(units) - VARHDRSZ,
|
|
|
|
false);
|
2002-04-21 21:52:18 +02:00
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
|
|
|
if (type == UNKNOWN_FIELD)
|
|
|
|
type = DecodeSpecial(0, lowunits, &val);
|
|
|
|
|
|
|
|
if (type == UNITS)
|
|
|
|
{
|
|
|
|
fsec_t fsec;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2002-04-21 21:52:18 +02:00
|
|
|
*tm = &tt;
|
|
|
|
|
|
|
|
time2tm(time, tm, &fsec);
|
|
|
|
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_MICROSEC:
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec * USECS_PER_SEC + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = (tm->tm_sec + fsec) * 1000000;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec * INT64CONST(1000) + fsec / INT64CONST(1000);
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = (tm->tm_sec + fsec) * 1000;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_SECOND:
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec + fsec / USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MINUTE:
|
|
|
|
result = tm->tm_min;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_HOUR:
|
|
|
|
result = tm->tm_hour;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_TZ:
|
|
|
|
case DTK_TZ_MINUTE:
|
|
|
|
case DTK_TZ_HOUR:
|
|
|
|
case DTK_DAY:
|
|
|
|
case DTK_MONTH:
|
|
|
|
case DTK_QUARTER:
|
|
|
|
case DTK_YEAR:
|
|
|
|
case DTK_DECADE:
|
|
|
|
case DTK_CENTURY:
|
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
default:
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("\"time\" units \"%s\" not recognized",
|
2005-10-15 04:49:52 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(units))))));
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-05-24 04:09:45 +02:00
|
|
|
else if (type == RESERV && val == DTK_EPOCH)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result = time / 1000000.0;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
|
|
|
result = time;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("\"time\" units \"%s\" not recognized",
|
2003-08-04 02:43:34 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
2005-10-15 04:49:52 +02:00
|
|
|
PointerGetDatum(units))))));
|
2002-04-21 21:52:18 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
|
|
|
|
2000-11-11 20:55:19 +01:00
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Time With Time Zone ADT
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
/* tm2timetz()
|
|
|
|
* Convert a tm structure to a time data type.
|
|
|
|
*/
|
2002-09-21 21:52:41 +02:00
|
|
|
static int
|
2005-10-15 04:49:52 +02:00
|
|
|
tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 20:06:13 +02:00
|
|
|
result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
|
2005-05-24 04:09:45 +02:00
|
|
|
USECS_PER_SEC) + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 20:06:13 +02:00
|
|
|
result->time = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
result->zone = tz;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_in(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
char *str = PG_GETARG_CSTRING(0);
|
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);
|
|
|
|
TimeTzADT *result;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2000-03-15 00:06:59 +01:00
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
int nf;
|
2003-08-28 01:29:29 +02:00
|
|
|
int dterr;
|
2005-05-26 04:04:14 +02:00
|
|
|
char workbuf[MAXDATELEN + 1];
|
2000-03-15 00:06:59 +01:00
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int dtype;
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
|
2005-05-26 04:04:14 +02:00
|
|
|
dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
|
|
|
|
field, ftype, MAXDATEFIELDS, &nf);
|
2003-08-28 01:29:29 +02:00
|
|
|
if (dterr == 0)
|
|
|
|
dterr = DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec, &tz);
|
|
|
|
if (dterr != 0)
|
|
|
|
DateTimeParseError(dterr, str, "time with time zone");
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2002-04-21 21:52:18 +02:00
|
|
|
tm2timetz(tm, fsec, tz, result);
|
2001-10-03 07:29:27 +02:00
|
|
|
AdjustTimeForTypmod(&(result->time), typmod);
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_out(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
2000-03-15 00:06:59 +01:00
|
|
|
char *result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2000-03-15 00:06:59 +01:00
|
|
|
*tm = &tt;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2000-03-15 00:06:59 +01:00
|
|
|
int tz;
|
|
|
|
char buf[MAXDATELEN + 1];
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
timetz2tm(time, tm, &fsec, &tz);
|
|
|
|
EncodeTimeOnly(tm, fsec, &tz, DateStyle, buf);
|
|
|
|
|
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
/*
|
|
|
|
* timetz_recv - converts external binary format to timetz
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-07-10 23:14:00 +02:00
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid typelem = PG_GETARG_OID(1);
|
|
|
|
#endif
|
|
|
|
int32 typmod = PG_GETARG_INT32(2);
|
|
|
|
TimeTzADT *result;
|
2003-05-13 01:08:52 +02:00
|
|
|
|
2005-07-10 23:14:00 +02:00
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2003-05-13 01:08:52 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-10 23:14:00 +02:00
|
|
|
result->time = pq_getmsgint64(buf);
|
2003-05-13 01:08:52 +02:00
|
|
|
#else
|
2005-07-10 23:14:00 +02:00
|
|
|
result->time = pq_getmsgfloat8(buf);
|
2003-05-13 01:08:52 +02:00
|
|
|
#endif
|
2005-07-10 23:14:00 +02:00
|
|
|
result->zone = pq_getmsgint(buf, sizeof(result->zone));
|
|
|
|
|
|
|
|
AdjustTimeForTypmod(&(result->time), typmod);
|
2003-05-13 01:08:52 +02:00
|
|
|
|
2005-07-10 23:14:00 +02:00
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2003-05-13 01:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* timetz_send - converts timetz to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_send(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
pq_sendint64(&buf, time->time);
|
|
|
|
#else
|
|
|
|
pq_sendfloat8(&buf, time->time);
|
|
|
|
#endif
|
|
|
|
pq_sendint(&buf, time->zone, sizeof(time->zone));
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
/* timetz2tm()
|
|
|
|
* Convert TIME WITH TIME ZONE data type to POSIX time structure.
|
|
|
|
*/
|
2002-09-21 21:52:41 +02:00
|
|
|
static int
|
2005-10-15 04:49:52 +02:00
|
|
|
timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2003-02-13 18:04:19 +01:00
|
|
|
int64 trem = time->time;
|
|
|
|
|
2005-05-24 04:09:45 +02:00
|
|
|
tm->tm_hour = trem / USECS_PER_HOUR;
|
|
|
|
trem -= tm->tm_hour * USECS_PER_HOUR;
|
|
|
|
tm->tm_min = trem / USECS_PER_MINUTE;
|
|
|
|
trem -= tm->tm_min * USECS_PER_MINUTE;
|
|
|
|
tm->tm_sec = trem / USECS_PER_SEC;
|
|
|
|
*fsec = trem - tm->tm_sec * USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2003-02-13 18:04:19 +01:00
|
|
|
double trem = time->time;
|
2002-04-21 21:52:18 +02:00
|
|
|
|
2005-10-09 19:21:47 +02:00
|
|
|
recalc:
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(trem, tm->tm_hour, (double) SECS_PER_HOUR);
|
|
|
|
TMODULO(trem, tm->tm_min, (double) SECS_PER_MINUTE);
|
2005-07-12 17:17:44 +02:00
|
|
|
TMODULO(trem, tm->tm_sec, 1.0);
|
2005-10-09 19:21:47 +02:00
|
|
|
trem = TIMEROUND(trem);
|
|
|
|
/* roundoff may need to propagate to higher-order fields */
|
|
|
|
if (trem >= 1.0)
|
|
|
|
{
|
|
|
|
trem = ceil(time->time);
|
|
|
|
goto recalc;
|
|
|
|
}
|
2002-04-21 21:52:18 +02:00
|
|
|
*fsec = trem;
|
|
|
|
#endif
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
if (tzp != NULL)
|
|
|
|
*tzp = time->zone;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
return 0;
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-10-03 07:29:27 +02:00
|
|
|
/* timetz_scale()
|
|
|
|
* Adjust time type for specified scale factor.
|
|
|
|
* Used by PostgreSQL type system to stuff columns.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_scale(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
int32 typmod = PG_GETARG_INT32(1);
|
|
|
|
TimeTzADT *result;
|
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
|
|
|
result->time = time->time;
|
|
|
|
result->zone = time->zone;
|
|
|
|
|
|
|
|
AdjustTimeForTypmod(&(result->time), typmod);
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
static int
|
|
|
|
timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
|
|
|
|
{
|
2005-04-24 00:53:05 +02:00
|
|
|
/* Primary sort is by true (GMT-equivalent) time */
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
|
|
|
int64 t1,
|
|
|
|
t2;
|
|
|
|
|
2005-05-23 20:56:55 +02:00
|
|
|
t1 = time1->time + (time1->zone * USECS_PER_SEC);
|
|
|
|
t2 = time2->time + (time2->zone * USECS_PER_SEC);
|
2005-04-24 00:53:05 +02:00
|
|
|
#else
|
2001-05-03 21:00:37 +02:00
|
|
|
double t1,
|
|
|
|
t2;
|
|
|
|
|
|
|
|
t1 = time1->time + time1->zone;
|
|
|
|
t2 = time2->time + time2->zone;
|
2005-04-24 00:53:05 +02:00
|
|
|
#endif
|
2001-05-03 21:00:37 +02:00
|
|
|
|
|
|
|
if (t1 > t2)
|
|
|
|
return 1;
|
|
|
|
if (t1 < t2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If same GMT time, sort by timezone; we only want to say that two
|
|
|
|
* timetz's are equal if both the time and zone parts are equal.
|
|
|
|
*/
|
2001-09-28 10:09:14 +02:00
|
|
|
if (time1->zone > time2->zone)
|
|
|
|
return 1;
|
|
|
|
if (time1->zone < time2->zone)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2001-05-03 21:00:37 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_eq(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_ne(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_lt(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_le(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_gt(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_ge(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
2001-05-03 21:00:37 +02:00
|
|
|
PG_RETURN_INT32(timetz_cmp_internal(time1, time2));
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-19 05:55:01 +02:00
|
|
|
/*
|
|
|
|
* timetz, being an unusual size, needs a specialized hash function.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_hash(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *key = PG_GETARG_TIMETZADT_P(0);
|
2000-06-19 05:55:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Specify hash length as sizeof(double) + sizeof(int4), not as
|
2005-10-15 04:49:52 +02:00
|
|
|
* sizeof(TimeTzADT), so that any garbage pad bytes in the structure won't
|
|
|
|
* be included in the hash!
|
2000-06-19 05:55:01 +02:00
|
|
|
*/
|
2002-04-21 21:52:18 +02:00
|
|
|
return hash_any((unsigned char *) key, sizeof(key->time) + sizeof(key->zone));
|
2000-06-19 05:55:01 +02:00
|
|
|
}
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_larger(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2003-08-08 02:10:31 +02:00
|
|
|
TimeTzADT *result;
|
2000-06-09 03:11:16 +02:00
|
|
|
|
2003-08-08 02:10:31 +02:00
|
|
|
if (timetz_cmp_internal(time1, time2) > 0)
|
|
|
|
result = time1;
|
|
|
|
else
|
|
|
|
result = time2;
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
timetz_smaller(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1);
|
2003-08-08 02:10:31 +02:00
|
|
|
TimeTzADT *result;
|
2000-06-09 03:11:16 +02:00
|
|
|
|
2003-08-08 02:10:31 +02:00
|
|
|
if (timetz_cmp_internal(time1, time2) < 0)
|
|
|
|
result = time1;
|
|
|
|
else
|
|
|
|
result = time2;
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2000-06-09 03:11:16 +02:00
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
/* timetz_pl_interval()
|
|
|
|
* Add interval to timetz.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_pl_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeTzADT *result;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifndef HAVE_INT64_TIMESTAMP
|
2000-12-03 15:51:12 +01:00
|
|
|
TimeTzADT time1;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-12-03 15:51:12 +01:00
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result->time = time->time + span->time;
|
|
|
|
result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
if (result->time < INT64CONST(0))
|
2005-05-23 20:56:55 +02:00
|
|
|
result->time += USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 06:41:43 +02:00
|
|
|
result->time = time->time + span->time;
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
|
2000-12-03 15:51:12 +01:00
|
|
|
if (result->time < 0)
|
2005-05-23 23:54:02 +02:00
|
|
|
result->time += SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
result->zone = time->zone;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timetz_mi_interval()
|
|
|
|
* Subtract interval from timetz.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_mi_interval(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
Interval *span = PG_GETARG_INTERVAL_P(1);
|
|
|
|
TimeTzADT *result;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifndef HAVE_INT64_TIMESTAMP
|
2000-12-03 15:51:12 +01:00
|
|
|
TimeTzADT time1;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-12-03 15:51:12 +01:00
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-21 06:41:43 +02:00
|
|
|
result->time = time->time - span->time;
|
|
|
|
result->time -= result->time / USECS_PER_DAY * USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
if (result->time < INT64CONST(0))
|
2005-05-23 20:56:55 +02:00
|
|
|
result->time += USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-07-21 06:41:43 +02:00
|
|
|
result->time = time->time - span->time;
|
2005-10-15 04:49:52 +02:00
|
|
|
TMODULO(result->time, time1.time, (double) SECS_PER_DAY);
|
2000-12-03 15:51:12 +01:00
|
|
|
if (result->time < 0)
|
2005-05-23 23:54:02 +02:00
|
|
|
result->time += SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
|
2000-12-03 15:51:12 +01:00
|
|
|
result->zone = time->zone;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* overlaps_timetz() --- implements the SQL92 OVERLAPS operator.
|
|
|
|
*
|
|
|
|
* Algorithm is per SQL92 spec. This is much harder than you'd think
|
|
|
|
* because the spec requires us to deliver a non-null answer in some cases
|
|
|
|
* where some of the inputs are null.
|
2000-03-15 00:06:59 +01:00
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
|
|
|
overlaps_timetz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The arguments are TimeTzADT *, but we leave them as generic Datums for
|
|
|
|
* convenience of notation --- and to avoid dereferencing nulls.
|
2000-06-09 03:11:16 +02:00
|
|
|
*/
|
|
|
|
Datum ts1 = PG_GETARG_DATUM(0);
|
|
|
|
Datum te1 = PG_GETARG_DATUM(1);
|
|
|
|
Datum ts2 = PG_GETARG_DATUM(2);
|
|
|
|
Datum te2 = PG_GETARG_DATUM(3);
|
2000-12-07 19:38:59 +01:00
|
|
|
bool ts1IsNull = PG_ARGISNULL(0);
|
|
|
|
bool te1IsNull = PG_ARGISNULL(1);
|
|
|
|
bool ts2IsNull = PG_ARGISNULL(2);
|
|
|
|
bool te2IsNull = PG_ARGISNULL(3);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
#define TIMETZ_GT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timetz_gt,t1,t2))
|
|
|
|
#define TIMETZ_LT(t1,t2) \
|
|
|
|
DatumGetBool(DirectFunctionCall2(timetz_lt,t1,t2))
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02: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 (TIMETZ_GT(ts1, te1))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts1;
|
2000-12-07 19:38:59 +01:00
|
|
|
|
|
|
|
ts1 = te1;
|
|
|
|
te1 = tt;
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/* Likewise for interval 2. */
|
|
|
|
if (ts2IsNull)
|
|
|
|
{
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
/* swap null for non-null */
|
2000-03-15 00:06:59 +01:00
|
|
|
ts2 = te2;
|
2000-12-07 19:38:59 +01:00
|
|
|
te2IsNull = true;
|
|
|
|
}
|
|
|
|
else if (!te2IsNull)
|
|
|
|
{
|
|
|
|
if (TIMETZ_GT(ts2, te2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum tt = ts2;
|
2000-12-07 19:38:59 +01:00
|
|
|
|
|
|
|
ts2 = te2;
|
|
|
|
te2 = tt;
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2000-12-07 19:38:59 +01:00
|
|
|
/*
|
|
|
|
* At this point neither ts1 nor ts2 is null, so we can consider three
|
|
|
|
* cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
|
|
|
|
*/
|
|
|
|
if (TIMETZ_GT(ts1, ts2))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This case is ts1 < te2 OR te1 < te2, which may look redundant but
|
|
|
|
* in the presence of nulls it's not quite completely so.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMETZ_LT(ts1, te2))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If te1 is not null then we had ts1 <= te1 above, and we just found
|
|
|
|
* ts1 >= te2, hence te1 >= te2.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
else if (TIMETZ_LT(ts1, ts2))
|
|
|
|
{
|
|
|
|
/* This case is ts2 < te1 OR te2 < te1 */
|
|
|
|
if (te1IsNull)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
if (TIMETZ_LT(ts2, te1))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
if (te2IsNull)
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If te2 is not null then we had ts2 <= te2 above, and we just found
|
|
|
|
* ts2 >= te1, hence te2 >= te1.
|
2000-12-07 19:38:59 +01:00
|
|
|
*/
|
|
|
|
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
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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 TIMETZ_GT
|
|
|
|
#undef TIMETZ_LT
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
Datum
|
|
|
|
timetz_time(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeTzADT *timetz = PG_GETARG_TIMETZADT_P(0);
|
|
|
|
TimeADT result;
|
|
|
|
|
|
|
|
/* swallow the time zone and just return the time */
|
|
|
|
result = timetz->time;
|
|
|
|
|
|
|
|
PG_RETURN_TIMEADT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
time_timetz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
TimeADT time = PG_GETARG_TIMEADT(0);
|
|
|
|
TimeTzADT *result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2001-09-28 10:09:14 +02:00
|
|
|
*tm = &tt;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2001-09-28 10:09:14 +02:00
|
|
|
int tz;
|
|
|
|
|
2002-06-11 15:40:53 +02:00
|
|
|
GetCurrentDateTime(tm);
|
2002-04-21 21:52:18 +02:00
|
|
|
time2tm(time, tm, &fsec);
|
2005-07-23 16:25:34 +02:00
|
|
|
tz = DetermineTimeZoneOffset(tm, global_timezone);
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
|
|
|
result->time = time;
|
|
|
|
result->zone = tz;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* timestamptz_timetz()
|
2000-03-15 00:06:59 +01:00
|
|
|
* Convert timestamp to timetz data type.
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
timestamptz_timetz(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
|
2000-04-12 19:17:23 +02:00
|
|
|
TimeTzADT *result;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2000-03-15 00:06:59 +01:00
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2000-03-15 00:06:59 +01:00
|
|
|
char *tzn;
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
if (TIMESTAMP_NOT_FINITE(timestamp))
|
2001-09-28 10:09:14 +02:00
|
|
|
PG_RETURN_NULL();
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("timestamp out of range")));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
tm2timetz(tm, fsec, tz, result);
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
|
|
|
}
|
2000-03-15 00:06:59 +01:00
|
|
|
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* datetimetz_timestamptz()
|
|
|
|
* Convert date and timetz to timestamp with time zone data type.
|
2000-03-15 00:06:59 +01:00
|
|
|
* Timestamp is stored in GMT, so add the time zone
|
|
|
|
* stored with the timetz to the result.
|
|
|
|
* - thomas 2000-03-10
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
Datum
|
2001-09-28 10:09:14 +02:00
|
|
|
datetimetz_timestamptz(PG_FUNCTION_ARGS)
|
2000-03-15 00:06:59 +01:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
DateADT date = PG_GETARG_DATEADT(0);
|
2000-06-09 03:11:16 +02:00
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
|
2001-10-25 07:50:21 +02:00
|
|
|
TimestampTz result;
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-22 05:46:34 +02:00
|
|
|
result = date * USECS_PER_DAY + time->time + time->zone * USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-10-15 04:49:52 +02:00
|
|
|
result = date * (double) SECS_PER_DAY + time->time + time->zone;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2000-03-15 00:06:59 +01:00
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
PG_RETURN_TIMESTAMP(result);
|
|
|
|
}
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* timetz_text()
|
|
|
|
* Convert timetz to text data type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* Input is a Timetz, but may as well leave it in Datum form */
|
|
|
|
Datum timetz = PG_GETARG_DATUM(0);
|
|
|
|
text *result;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(timetz_out, timetz));
|
|
|
|
|
2005-07-21 06:41:43 +02:00
|
|
|
len = strlen(str) + VARHDRSZ;
|
2000-11-11 20:55:19 +01:00
|
|
|
|
|
|
|
result = palloc(len);
|
|
|
|
|
|
|
|
VARATT_SIZEP(result) = len;
|
|
|
|
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
|
|
|
|
|
|
|
pfree(str);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* text_timetz()
|
|
|
|
* Convert text string to timetz.
|
|
|
|
* Text type is not null terminated, so use temporary string
|
|
|
|
* then call the standard input routine.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
text_timetz(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *str = PG_GETARG_TEXT_P(0);
|
|
|
|
int i;
|
|
|
|
char *sp,
|
|
|
|
*dp,
|
|
|
|
dstr[MAXDATELEN + 1];
|
|
|
|
|
|
|
|
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("invalid input syntax for type time with time zone: \"%s\"",
|
|
|
|
VARDATA(str))));
|
2000-11-11 20:55:19 +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(timetz_in,
|
|
|
|
CStringGetDatum(dstr),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1));
|
2000-11-11 20:55:19 +01:00
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
/* timetz_part()
|
|
|
|
* Extract specified field from time type.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_part(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *units = PG_GETARG_TEXT_P(0);
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
float8 result;
|
|
|
|
int type,
|
|
|
|
val;
|
2004-05-07 02:24:59 +02:00
|
|
|
char *lowunits;
|
2001-10-18 19:30:21 +02:00
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
lowunits = downcase_truncate_identifier(VARDATA(units),
|
|
|
|
VARSIZE(units) - VARHDRSZ,
|
|
|
|
false);
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
type = DecodeUnits(0, lowunits, &val);
|
|
|
|
if (type == UNKNOWN_FIELD)
|
|
|
|
type = DecodeSpecial(0, lowunits, &val);
|
|
|
|
|
|
|
|
if (type == UNITS)
|
|
|
|
{
|
|
|
|
double dummy;
|
|
|
|
int tz;
|
2002-04-21 21:52:18 +02:00
|
|
|
fsec_t fsec;
|
2004-08-29 07:07:03 +02:00
|
|
|
struct pg_tm tt,
|
2001-10-18 19:30:21 +02:00
|
|
|
*tm = &tt;
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
timetz2tm(time, tm, &fsec, &tz);
|
2001-10-18 19:30:21 +02:00
|
|
|
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case DTK_TZ:
|
2003-07-17 02:55:37 +02:00
|
|
|
result = -tz;
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_TZ_MINUTE:
|
2003-07-17 02:55:37 +02:00
|
|
|
result = -tz;
|
2005-07-21 05:56:25 +02:00
|
|
|
result /= SECS_PER_MINUTE;
|
2005-10-15 04:49:52 +02:00
|
|
|
FMODULO(result, dummy, (double) SECS_PER_MINUTE);
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_TZ_HOUR:
|
2003-07-17 02:55:37 +02:00
|
|
|
dummy = -tz;
|
2005-10-15 04:49:52 +02:00
|
|
|
FMODULO(dummy, result, (double) SECS_PER_HOUR);
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MICROSEC:
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec * USECS_PER_SEC + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = (tm->tm_sec + fsec) * 1000000;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MILLISEC:
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec * INT64CONST(1000) + fsec / INT64CONST(1000);
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = (tm->tm_sec + fsec) * 1000;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_SECOND:
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec + fsec / USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = tm->tm_sec + fsec;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-10-18 19:30:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_MINUTE:
|
|
|
|
result = tm->tm_min;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_HOUR:
|
|
|
|
result = tm->tm_hour;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DTK_DAY:
|
|
|
|
case DTK_MONTH:
|
|
|
|
case DTK_QUARTER:
|
|
|
|
case DTK_YEAR:
|
|
|
|
case DTK_DECADE:
|
|
|
|
case DTK_CENTURY:
|
|
|
|
case DTK_MILLENNIUM:
|
|
|
|
default:
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("\"time with time zone\" units \"%s\" not recognized",
|
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(units))))));
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-05-24 04:09:45 +02:00
|
|
|
else if (type == RESERV && val == DTK_EPOCH)
|
2002-04-21 21:52:18 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-07-12 17:17:44 +02:00
|
|
|
result = time->time / 1000000.0 + time->zone;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-05-24 04:09:45 +02:00
|
|
|
result = time->time + time->zone;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
}
|
2001-10-18 19:30:21 +02:00
|
|
|
else
|
|
|
|
{
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("\"time with time zone\" units \"%s\" not recognized",
|
|
|
|
DatumGetCString(DirectFunctionCall1(textout,
|
|
|
|
PointerGetDatum(units))))));
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2001-10-18 19:30:21 +02:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_FLOAT8(result);
|
|
|
|
}
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
/* timetz_zone()
|
|
|
|
* Encode time with time zone type with specified time zone.
|
2005-06-15 02:34:11 +02:00
|
|
|
* Applies DST rules as of the current date.
|
2001-09-28 10:09:14 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_zone(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *zone = PG_GETARG_TEXT_P(0);
|
2005-06-15 02:34:11 +02:00
|
|
|
TimeTzADT *t = PG_GETARG_TIMETZADT_P(1);
|
2001-09-28 10:09:14 +02:00
|
|
|
TimeTzADT *result;
|
|
|
|
int tz;
|
2005-10-15 04:49:52 +02:00
|
|
|
char tzname[TZ_STRLEN_MAX + 1];
|
|
|
|
int len;
|
2005-06-15 02:34:11 +02:00
|
|
|
pg_tz *tzp;
|
|
|
|
|
2005-09-09 04:31:50 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Look up the requested timezone. First we look in the timezone database
|
|
|
|
* (to handle cases like "America/New_York"), and if that fails, we look
|
|
|
|
* in the date token table (to handle cases like "EST").
|
|
|
|
*/
|
2005-09-09 04:31:50 +02:00
|
|
|
len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX);
|
2005-07-22 07:03:09 +02:00
|
|
|
memcpy(tzname, VARDATA(zone), len);
|
2005-09-09 04:31:50 +02:00
|
|
|
tzname[len] = '\0';
|
2005-06-15 02:34:11 +02:00
|
|
|
tzp = pg_tzset(tzname);
|
2005-09-09 04:31:50 +02:00
|
|
|
if (tzp)
|
|
|
|
{
|
|
|
|
/* Get the offset-from-GMT that is valid today for the selected zone */
|
2005-10-15 04:49:52 +02:00
|
|
|
pg_time_t now;
|
2005-09-09 04:31:50 +02:00
|
|
|
struct pg_tm *tm;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
tm = pg_localtime(&now, tzp);
|
|
|
|
tz = -tm->tm_gmtoff;
|
2005-06-15 02:34:11 +02:00
|
|
|
}
|
2005-09-09 04:31:50 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *lowzone;
|
|
|
|
int type,
|
|
|
|
val;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
2005-09-09 04:31:50 +02:00
|
|
|
lowzone = downcase_truncate_identifier(VARDATA(zone),
|
|
|
|
VARSIZE(zone) - VARHDRSZ,
|
|
|
|
false);
|
|
|
|
type = DecodeSpecial(0, lowzone, &val);
|
|
|
|
|
|
|
|
if (type == TZ || type == DTZ)
|
|
|
|
tz = val * 60;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("time zone \"%s\" not recognized", tzname)));
|
|
|
|
tz = 0; /* keep compiler quiet */
|
|
|
|
}
|
2005-06-15 02:34:11 +02:00
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
|
2005-09-09 04:31:50 +02:00
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-06-15 02:34:11 +02:00
|
|
|
result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
|
|
|
|
while (result->time < INT64CONST(0))
|
|
|
|
result->time += USECS_PER_DAY;
|
|
|
|
while (result->time >= USECS_PER_DAY)
|
|
|
|
result->time -= USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2005-06-15 02:34:11 +02:00
|
|
|
result->time = t->time + (t->zone - tz);
|
|
|
|
while (result->time < 0)
|
|
|
|
result->time += SECS_PER_DAY;
|
|
|
|
while (result->time >= SECS_PER_DAY)
|
|
|
|
result->time -= SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
|
2005-06-15 02:34:11 +02:00
|
|
|
result->zone = tz;
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2005-07-22 07:03:09 +02:00
|
|
|
}
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
/* timetz_izone()
|
|
|
|
* Encode time with time zone type with specified time interval as time zone.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
timetz_izone(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Interval *zone = PG_GETARG_INTERVAL_P(0);
|
|
|
|
TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
|
|
|
|
TimeTzADT *result;
|
|
|
|
int tz;
|
|
|
|
|
|
|
|
if (zone->month != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2003-09-26 17:27:37 +02:00
|
|
|
errmsg("\"interval\" time zone \"%s\" not valid",
|
2003-08-04 02:43:34 +02:00
|
|
|
DatumGetCString(DirectFunctionCall1(interval_out,
|
2005-10-15 04:49:52 +02:00
|
|
|
PointerGetDatum(zone))))));
|
2001-09-28 10:09:14 +02:00
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-23 20:56:55 +02:00
|
|
|
tz = -(zone->time / USECS_PER_SEC);
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2001-09-28 10:09:14 +02:00
|
|
|
tz = -(zone->time);
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
2001-09-28 10:09:14 +02:00
|
|
|
|
|
|
|
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
|
|
|
|
2002-04-21 21:52:18 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2005-05-24 04:09:45 +02:00
|
|
|
result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
|
2002-04-21 21:52:18 +02:00
|
|
|
while (result->time < INT64CONST(0))
|
2005-05-23 20:56:55 +02:00
|
|
|
result->time += USECS_PER_DAY;
|
|
|
|
while (result->time >= USECS_PER_DAY)
|
|
|
|
result->time -= USECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#else
|
2002-11-22 00:31:20 +01:00
|
|
|
result->time = time->time + (time->zone - tz);
|
2002-04-21 21:52:18 +02:00
|
|
|
while (result->time < 0)
|
2005-05-23 23:54:02 +02:00
|
|
|
result->time += SECS_PER_DAY;
|
|
|
|
while (result->time >= SECS_PER_DAY)
|
|
|
|
result->time -= SECS_PER_DAY;
|
2002-04-21 21:52:18 +02:00
|
|
|
#endif
|
|
|
|
|
2001-09-28 10:09:14 +02:00
|
|
|
result->zone = tz;
|
|
|
|
|
|
|
|
PG_RETURN_TIMETZADT_P(result);
|
2005-07-22 07:03:09 +02:00
|
|
|
}
|