1997-03-15 00:21:12 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* datetime.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* implements DATE and TIME data types specified in SQL-92 standard
|
1997-03-15 00:21:12 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994-5, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-12-31 17:30:59 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.26 1998/12/31 16:30:56 thomas Exp $
|
1997-03-15 00:21:12 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <stdio.h> /* for sprintf() */
|
1997-03-15 00:21:12 +01:00
|
|
|
#include <string.h>
|
1997-04-17 15:50:57 +02:00
|
|
|
#include <limits.h>
|
1997-03-15 00:21:12 +01:00
|
|
|
|
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
|
|
|
#include "postgres.h"
|
1997-04-04 10:55:29 +02:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
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
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/nabstime.h"
|
|
|
|
#include "utils/datetime.h"
|
|
|
|
#include "access/xact.h"
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static int date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static int day_tab[2][12] = {
|
1997-09-07 07:04:48 +02:00
|
|
|
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
|
|
|
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
#define isleap(y) (((y % 4) == 0 && (y % 100) != 0) || (y % 400) == 0)
|
|
|
|
|
1997-05-16 09:19:50 +02:00
|
|
|
#define UTIME_MINYEAR (1901)
|
|
|
|
#define UTIME_MINMONTH (12)
|
|
|
|
#define UTIME_MINDAY (14)
|
|
|
|
#define UTIME_MAXYEAR (2038)
|
|
|
|
#define UTIME_MAXMONTH (01)
|
|
|
|
#define UTIME_MAXDAY (18)
|
|
|
|
|
|
|
|
#define IS_VALID_UTIME(y,m,d) (((y > UTIME_MINYEAR) \
|
|
|
|
|| ((y == UTIME_MINYEAR) && ((m > UTIME_MINMONTH) \
|
|
|
|
|| ((m == UTIME_MINMONTH) && (d >= UTIME_MINDAY))))) \
|
|
|
|
&& ((y < UTIME_MAXYEAR) \
|
|
|
|
|| ((y == UTIME_MAXYEAR) && ((m < UTIME_MAXMONTH) \
|
|
|
|
|| ((m == UTIME_MAXMONTH) && (d <= UTIME_MAXDAY))))))
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* Date ADT
|
1997-03-15 00:21:12 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* date_in()
|
|
|
|
* Given date text string, convert to internal date format.
|
|
|
|
*/
|
|
|
|
DateADT
|
|
|
|
date_in(char *str)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DateADT date;
|
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tzp;
|
|
|
|
int dtype;
|
|
|
|
int nf;
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int ftype[MAXDATEFIELDS];
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!PointerIsValid(str))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Bad (null) date external representation", NULL);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
#ifdef DATEDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("date_in- input string is %s\n", str);
|
1997-03-15 00:21:12 +01:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
|
|
|
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp) != 0))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Bad date external representation %s", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (dtype)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case DTK_DATE:
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case DTK_CURRENT:
|
|
|
|
GetCurrentTime(tm);
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case DTK_EPOCH:
|
|
|
|
tm->tm_year = 1970;
|
|
|
|
tm->tm_mon = 1;
|
|
|
|
tm->tm_mday = 1;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unrecognized date external representation %s", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tm->tm_year < 0 || tm->tm_year > 32767)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "date_in: year must be limited to values 0 through 32767 in '%s'", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tm->tm_mon < 1 || tm->tm_mon > 12)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "date_in: month must be limited to values 1 through 12 in '%s'", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tm->tm_mday < 1 || tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "date_in: day must be limited to values 1 through %d in '%s'",
|
1997-09-07 07:04:48 +02:00
|
|
|
day_tab[isleap(tm->tm_year)][tm->tm_mon - 1], str);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
date = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1));
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return date;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_in() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
/* date_out()
|
|
|
|
* Given internal format date, convert to text string.
|
|
|
|
*/
|
1997-10-25 07:16:09 +02:00
|
|
|
char *
|
1997-03-15 00:21:12 +01:00
|
|
|
date_out(DateADT date)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
char buf[MAXDATELEN + 1];
|
1997-07-01 02:22:46 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
j2date((date + date2j(2000, 1, 1)),
|
|
|
|
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
EncodeDateOnly(tm, DateStyle, buf);
|
1997-07-01 02:22:46 +02:00
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
result = palloc(strlen(buf) + 1);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
strcpy(result, buf);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_out() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
date_eq(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 == dateVal2;
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
date_ne(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 != dateVal2;
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
date_lt(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 < dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_lt() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
date_le(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 <= dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_le() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
date_gt(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 > dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_gt() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
date_ge(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 >= dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_ge() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
date_cmp(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (dateVal1 < dateVal2)
|
|
|
|
return -1;
|
|
|
|
else if (dateVal1 > dateVal2)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_cmp() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
DateADT
|
|
|
|
date_larger(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return date_gt(dateVal1, dateVal2) ? dateVal1 : dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_larger() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
DateADT
|
|
|
|
date_smaller(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return date_lt(dateVal1, dateVal2) ? dateVal1 : dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_smaller() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-10-25 07:16:09 +02:00
|
|
|
/* Compute difference between two dates in days.
|
|
|
|
*/
|
1997-03-15 00:21:12 +01:00
|
|
|
int4
|
|
|
|
date_mi(DateADT dateVal1, DateADT dateVal2)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal1 - dateVal2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_mi() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
/* Add a number of days to a date, giving a new date.
|
1997-10-25 07:16:09 +02:00
|
|
|
* Must handle both positive and negative numbers of days.
|
|
|
|
*/
|
1997-03-15 00:21:12 +01:00
|
|
|
DateADT
|
|
|
|
date_pli(DateADT dateVal, int4 days)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return dateVal + days;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_pli() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-10-25 07:16:09 +02:00
|
|
|
/* Subtract a number of days from a date, giving a new date.
|
|
|
|
*/
|
1997-03-15 00:21:12 +01:00
|
|
|
DateADT
|
|
|
|
date_mii(DateADT dateVal, int4 days)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return date_pli(dateVal, -days);
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_mii() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
/* date_datetime()
|
|
|
|
* Convert date to datetime data type.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
DateTime *
|
1997-05-16 09:19:50 +02:00
|
|
|
date_datetime(DateADT dateVal)
|
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
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DateTime *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
double fsec = 0;
|
|
|
|
char *tzn;
|
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
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
result = palloc(sizeof(DateTime));
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (date2tm(dateVal, &tz, tm, &fsec, &tzn) != 0)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert date to datetime", NULL);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
|
|
|
#ifdef DATEDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("date_datetime- date is %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
|
|
|
|
printf("date_datetime- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
|
1997-05-16 09:19:50 +02:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tm2datetime(tm, fsec, &tz, result) != 0)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Datetime out of range", NULL);
|
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
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date_datetime() */
|
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
|
|
|
|
|
|
|
|
|
|
|
/* datetime_date()
|
|
|
|
* Convert datetime to date data type.
|
|
|
|
*/
|
|
|
|
DateADT
|
1997-09-08 23:56:23 +02:00
|
|
|
datetime_date(DateTime *datetime)
|
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
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DateADT result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(datetime))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert null datetime to date", NULL);
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (DATETIME_NOT_FINITE(*datetime))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert datetime to date", NULL);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (DATETIME_IS_EPOCH(*datetime))
|
|
|
|
{
|
|
|
|
datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else if (DATETIME_IS_CURRENT(*datetime))
|
|
|
|
{
|
|
|
|
datetime2tm(SetDateTime(*datetime), &tz, tm, &fsec, &tzn);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (datetime2tm(*datetime, &tz, tm, &fsec, &tzn) != 0)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert datetime to date", NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1));
|
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions
Here are some additional patches mostly related to the date and time
data types. It includes some type conversion routines to move between
the different date types and some other date manipulation routines such
as date_part(units,datetime).
I noticed Edmund Mergl et al's neat trick for getting function overloading
for builtin functions, so started to use that for the date and time stuff.
Later, if someone figures out how to get function overloading directly
for internal C code, then we can move to that technique.
These patches include documentation updates (don't faint!) for the built-in
man page. Doesn't yet include mention of timestamp, since I don't know
much about it and since it may change a bit to become a _real_ ANSI timestamp
which would include parser support for the declaration syntax (what do you
think, Dan?).
The patches were developed on the 970330 release, but have been rebuilt
off of the 970402 release. The first patch below is to get libpq to compile,
on my Linux box, but is not related to the rest of the patches and you can
choose not to apply that one at this time. Thanks in advance, scrappy!
1997-04-02 20:36:24 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* datetime_date() */
|
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
|
|
|
|
|
|
|
|
|
|
|
/* abstime_date()
|
|
|
|
* Convert abstime to date data type.
|
|
|
|
*/
|
|
|
|
DateADT
|
|
|
|
abstime_date(AbsoluteTime abstime)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DateADT result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (abstime)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case INVALID_ABSTIME:
|
|
|
|
case NOSTART_ABSTIME:
|
|
|
|
case NOEND_ABSTIME:
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert reserved abstime value to date", NULL);
|
1997-09-08 04:41:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pretend to drop through to make compiler think that result
|
|
|
|
* will be set
|
|
|
|
*/
|
|
|
|
|
|
|
|
case EPOCH_ABSTIME:
|
|
|
|
result = date2j(1970, 1, 1) - date2j(2000, 1, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURRENT_ABSTIME:
|
|
|
|
GetCurrentTime(tm);
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abstime2tm(abstime, &tz, tm, NULL);
|
|
|
|
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
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
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* abstime_date() */
|
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
|
|
|
|
1997-05-16 09:19:50 +02:00
|
|
|
|
|
|
|
/* date2tm()
|
|
|
|
* Convert date to time structure.
|
|
|
|
* Note that date is an implicit local time, but the system calls assume
|
1997-09-07 07:04:48 +02:00
|
|
|
* that everything is GMT. So, convert to GMT, rotate to local time,
|
|
|
|
* and then convert again to try to get the time zones correct.
|
1997-05-16 09:19:50 +02:00
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static int
|
1998-12-31 17:30:59 +01:00
|
|
|
date2tm(DateADT dateVal, int *tzp, struct tm *tm, double *fsec, char **tzn)
|
1997-05-16 09:19:50 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
struct tm *tx;
|
|
|
|
time_t utime;
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
*fsec = 0;
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
j2date((dateVal + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
|
|
|
tm->tm_hour = 0;
|
|
|
|
tm->tm_min = 0;
|
|
|
|
tm->tm_sec = 0;
|
|
|
|
tm->tm_isdst = -1;
|
|
|
|
|
|
|
|
if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
|
|
|
|
{
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* convert to system time */
|
|
|
|
utime = ((dateVal + (date2j(2000, 1, 1) - date2j(1970, 1, 1))) * 86400);
|
1998-12-31 17:30:59 +01:00
|
|
|
/* rotate to noon to get the right day in time zone */
|
|
|
|
utime += (12 * 60 * 60);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
|
|
|
#ifdef USE_POSIX_TIME
|
1997-09-07 07:04:48 +02:00
|
|
|
tx = localtime(&utime);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
|
|
|
#ifdef DATEDEBUG
|
1998-12-31 17:30:59 +01:00
|
|
|
#if defined(HAVE_TM_ZONE)
|
|
|
|
printf("date2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s dst=%d\n",
|
|
|
|
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, (double) tm->tm_sec,
|
|
|
|
tx->tm_zone, tx->tm_isdst);
|
|
|
|
#elif defined(HAVE_INT_TIMEZONE)
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("date2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s %s dst=%d\n",
|
|
|
|
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, (double) tm->tm_sec,
|
|
|
|
tzname[0], tzname[1], tx->tm_isdst);
|
1997-05-30 17:02:51 +02:00
|
|
|
#endif
|
1997-05-16 09:19:50 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
tm->tm_year = tx->tm_year + 1900;
|
|
|
|
tm->tm_mon = tx->tm_mon + 1;
|
|
|
|
tm->tm_mday = tx->tm_mday;
|
|
|
|
tm->tm_isdst = tx->tm_isdst;
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1998-12-31 17:30:59 +01:00
|
|
|
#if defined(HAVE_TM_ZONE)
|
1997-09-07 07:04:48 +02:00
|
|
|
tm->tm_gmtoff = tx->tm_gmtoff;
|
|
|
|
tm->tm_zone = tx->tm_zone;
|
|
|
|
|
1998-12-31 17:30:59 +01:00
|
|
|
/* tm_gmtoff is Sun/DEC-ism */
|
|
|
|
*tzp = -(tm->tm_gmtoff);
|
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = (char *)tm->tm_zone;
|
|
|
|
#elif defined(HAVE_INT_TIMEZONE)
|
|
|
|
*tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tzn != NULL)
|
1998-12-31 17:30:59 +01:00
|
|
|
*tzn = tzname[(tm->tm_isdst > 0)];
|
|
|
|
#else
|
|
|
|
#error USE_POSIX_TIME is defined but neither HAVE_TM_ZONE or HAVE_INT_TIMEZONE are defined
|
1997-05-16 09:19:50 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
#else /* !USE_POSIX_TIME */
|
|
|
|
*tzp = CTimeZone; /* V7 conventions; don't know timezone? */
|
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = CTZName;
|
1997-05-16 09:19:50 +02:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* otherwise, outside of timezone range so convert to GMT... */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-05-16 09:19:50 +02:00
|
|
|
#ifdef DATEDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("date2tm- convert %d-%d-%d %d:%d%d to datetime\n",
|
|
|
|
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
1997-05-16 09:19:50 +02:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
*tzp = 0;
|
|
|
|
tm->tm_isdst = 0;
|
|
|
|
if (tzn != NULL)
|
|
|
|
*tzn = NULL;
|
|
|
|
}
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1998-12-31 17:30:59 +01:00
|
|
|
#ifdef DATEDEBUG
|
|
|
|
#if defined(HAVE_TM_ZONE)
|
|
|
|
printf("date2tm- %d.%02d.%02d %02d:%02d:%02.0f (%d %s) dst=%d\n",
|
|
|
|
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, (double) tm->tm_sec,
|
|
|
|
*tzp, tm->tm_zone, tm->tm_isdst);
|
|
|
|
#elif defined(HAVE_INT_TIMEZONE)
|
|
|
|
printf("date2tm- %d.%02d.%02d %02d:%02d:%02.0f (%d %s %s) dst=%d\n",
|
|
|
|
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, (double) tm->tm_sec,
|
|
|
|
*tzp, tzname[0], tzname[1], tm->tm_isdst);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return 0;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* date2tm() */
|
1997-05-16 09:19:50 +02:00
|
|
|
|
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
|
|
|
|
1997-03-15 00:21:12 +01:00
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* Time ADT
|
1997-03-15 00:21:12 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
TimeADT *
|
1997-03-15 00:21:12 +01:00
|
|
|
time_in(char *str)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TimeADT *time;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
double fsec;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
int nf;
|
|
|
|
char lowstr[MAXDATELEN + 1];
|
|
|
|
char *field[MAXDATEFIELDS];
|
|
|
|
int dtype;
|
|
|
|
int ftype[MAXDATEFIELDS];
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(str))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Bad (null) time external representation", NULL);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|
|
|
|
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec) != 0))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Bad time external representation '%s'", str);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Hour must be limited to values 0 through 23 in '%s'", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((tm->tm_min < 0) || (tm->tm_min > 59))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Minute must be limited to values 0 through 59 in '%s'", str);
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((tm->tm_sec < 0) || ((tm->tm_sec + fsec) >= 60))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Second must be limited to values 0 through < 60 in '%s'", str);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
time = palloc(sizeof(TimeADT));
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
*time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return time;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_in() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
|
1997-10-25 07:16:09 +02:00
|
|
|
char *
|
1997-09-08 23:56:23 +02:00
|
|
|
time_out(TimeADT *time)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result;
|
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
double fsec;
|
|
|
|
char buf[MAXDATELEN + 1];
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time))
|
|
|
|
return NULL;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
tm->tm_hour = (*time / (60 * 60));
|
|
|
|
tm->tm_min = (((int) (*time / 60)) % 60);
|
|
|
|
tm->tm_sec = (((int) *time) % 60);
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
fsec = 0;
|
1997-05-16 09:19:50 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
EncodeTimeOnly(tm, fsec, DateStyle, buf);
|
1997-07-01 02:22:46 +02:00
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
result = palloc(strlen(buf) + 1);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
strcpy(result, buf);
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_out() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_eq(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 == *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_ne(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 != *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_lt(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 < *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_le(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 <= *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_gt(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 > *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
time_ge(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time1) || !PointerIsValid(time2))
|
1998-09-01 05:29:17 +02:00
|
|
|
return FALSE;
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return *time1 >= *time2;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_eq() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
time_cmp(TimeADT *time1, TimeADT *time2)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return (*time1 < *time2) ? -1 : (((*time1 > *time2) ? 1 : 0));
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* time_cmp() */
|
1997-03-15 00:21:12 +01:00
|
|
|
|
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
|
|
|
|
1997-10-25 07:16:09 +02:00
|
|
|
/* datetime_time()
|
|
|
|
* Convert datetime to time data type.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
TimeADT *
|
1997-10-25 07:16:09 +02:00
|
|
|
datetime_time(DateTime *datetime)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
TimeADT *result;
|
1997-10-25 07:16:09 +02:00
|
|
|
struct tm tt,
|
|
|
|
*tm = &tt;
|
|
|
|
int tz;
|
|
|
|
double fsec;
|
|
|
|
char *tzn;
|
|
|
|
|
|
|
|
if (!PointerIsValid(datetime))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert null datetime to date", NULL);
|
1997-10-25 07:16:09 +02:00
|
|
|
|
|
|
|
if (DATETIME_NOT_FINITE(*datetime))
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert datetime to date", NULL);
|
1997-10-25 07:16:09 +02:00
|
|
|
|
|
|
|
if (DATETIME_IS_EPOCH(*datetime))
|
|
|
|
{
|
|
|
|
datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (DATETIME_IS_CURRENT(*datetime))
|
|
|
|
{
|
|
|
|
datetime2tm(SetDateTime(*datetime), &tz, tm, &fsec, &tzn);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (datetime2tm(*datetime, &tz, tm, &fsec, &tzn) != 0)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "Unable to convert datetime to date", NULL);
|
1997-10-25 07:16:09 +02:00
|
|
|
}
|
|
|
|
|
1998-01-07 19:47:07 +01:00
|
|
|
result = palloc(sizeof(TimeADT));
|
1997-10-25 07:16:09 +02:00
|
|
|
|
|
|
|
*result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* datetime_time() */
|
1997-10-25 07:16:09 +02:00
|
|
|
|
|
|
|
|
1997-12-23 20:23:09 +01:00
|
|
|
/* datetime_datetime()
|
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
|
|
|
* Convert date and time to datetime data type.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
DateTime *
|
1997-12-23 20:23:09 +01:00
|
|
|
datetime_datetime(DateADT date, TimeADT *time)
|
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
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
DateTime *result;
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!PointerIsValid(time))
|
|
|
|
{
|
1998-01-07 19:47:07 +01:00
|
|
|
result = palloc(sizeof(DateTime));
|
1997-09-07 07:04:48 +02:00
|
|
|
DATETIME_INVALID(*result);
|
1998-02-26 05:46:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
result = date_datetime(date);
|
|
|
|
*result += *time;
|
|
|
|
}
|
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
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-02-26 05:46:47 +01:00
|
|
|
} /* datetime_datetime() */
|
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
|
|
|
|
1997-03-15 00:21:12 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
int32 /* RelativeTime */
|
1998-08-19 04:04:17 +02:00
|
|
|
int4reltime(int32 timevalue)
|
1997-03-15 00:21:12 +01:00
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
return timevalue;
|
1997-03-15 00:21:12 +01:00
|
|
|
}
|