summaryrefslogtreecommitdiff
path: root/src/include/utils
diff options
context:
space:
mode:
authorMarc G. Fournier1997-03-25 08:11:24 +0000
committerMarc G. Fournier1997-03-25 08:11:24 +0000
commitdfe04753629d6b2203b9b4db8a299d451fe5b387 (patch)
treeca6cdc67c96085342f5cd625685b939088dd29b6 /src/include/utils
parentd2a386d6e35c1791ffcc8f09fe162b0591184f95 (diff)
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More patches for date/time I have accumulated several patches to add functionality to the datetime and timespan data types as well as to fix reported porting bugs on non-BSD machines. These patches are: dt.c.patch - add datetime_part(), fix bugs dt.h.patch - add quarter and timezone support, add prototypes globals.c.patch - add time and timezone variables miscadmin.h.patch - add time and timezone variables nabstime.c.patch - add datetime conversion routine nabstime.h.patch - add prototypes pg_operator.h.patch - add datetime operators, clean up formatting pg_proc.h.patch - add datetime functions, reassign conflicting date OIDs pg_type.h.patch - add datetime and timespan data types The dt.c and pg_proc.h patches are fairly large; the latter mostly because I tried to get some columns for existing entries to line up.
Diffstat (limited to 'src/include/utils')
-rw-r--r--src/include/utils/dt.h121
-rw-r--r--src/include/utils/nabstime.h8
2 files changed, 106 insertions, 23 deletions
diff --git a/src/include/utils/dt.h b/src/include/utils/dt.h
index b9f20b03aa8..fbc8d061b3d 100644
--- a/src/include/utils/dt.h
+++ b/src/include/utils/dt.h
@@ -8,7 +8,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: dt.h,v 1.2 1997/03/18 16:36:50 scrappy Exp $
+ * $Id: dt.h,v 1.3 1997/03/25 08:11:18 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,19 +16,23 @@
#define DT_H
#include <time.h>
+#include <math.h>
/*
* DateTime represents absolute time.
- * TimeSpan represents delta time.
+ * TimeSpan represents delta time. Keep track of months (and years)
+ * separately since the elapsed time spanned is unknown until instantiated
+ * relative to an absolute time.
+ *
* Note that Postgres uses "time interval" to mean a bounded interval,
- * consisting of a beginning and ending time, not a time span.
+ * consisting of a beginning and ending time, not a time span - tgl 97/03/20
*/
typedef double DateTime;
typedef struct {
double time; /* all time units other than months and years */
- int4 month; /* months and years */
+ int4 month; /* months and years, after time for alignment */
} TimeSpan;
@@ -45,6 +49,7 @@ typedef struct {
* Other alternate forms are hardcoded into token tables in dt.c.
* ----------------------------------------------------------------
*/
+
#define DAGO "ago"
#define DCURRENT "current"
#define EPOCH "epoch"
@@ -65,12 +70,14 @@ typedef struct {
#define DDAY "day"
#define DWEEK "week"
#define DMONTH "month"
+#define DQUARTER "quarter"
#define DYEAR "year"
#define DDECADE "decade"
#define DCENTURY "century"
#define DMILLENIUM "millenium"
#define DA_D "ad"
#define DB_C "bc"
+#define DTIMEZONE "timezone"
/*
* Fundamental time field definitions for parsing.
@@ -78,6 +85,7 @@ typedef struct {
* Meridian: am, pm, or 24-hour style.
* Millenium: ad, bc
*/
+
#define AM 0
#define PM 1
#define HR24 2
@@ -90,6 +98,7 @@ typedef struct {
* Can't have more of these than there are bits in an unsigned int
* since these are turned into bit masks during parsing and decoding.
*/
+
#define RESERV 0
#define MONTH 1
#define YEAR 2
@@ -116,6 +125,7 @@ typedef struct {
* These need to fit into the datetkn table type.
* At the moment, that means keep them within [-127,127].
*/
+
#define DTK_NUMBER 0
#define DTK_STRING 1
@@ -143,17 +153,19 @@ typedef struct {
#define DTK_DAY 36
#define DTK_WEEK 37
#define DTK_MONTH 38
-#define DTK_YEAR 39
-#define DTK_DECADE 40
-#define DTK_CENTURY 41
-#define DTK_MILLENIUM 42
-#define DTK_MILLISEC 43
-#define DTK_MICROSEC 44
-#define DTK_AGO 45
+#define DTK_QUARTER 39
+#define DTK_YEAR 40
+#define DTK_DECADE 41
+#define DTK_CENTURY 42
+#define DTK_MILLENIUM 43
+#define DTK_MILLISEC 44
+#define DTK_MICROSEC 45
+#define DTK_AGO 46
/*
* Bit mask definitions for time parsing.
*/
+
#define DTK_M(t) (0x01 << t)
#define DTK_DATE_M (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
@@ -174,21 +186,95 @@ typedef struct {
char value; /* this may be unsigned, alas */
} datetkn;
-extern void GetCurrentTime(struct tm *tm);
+#ifdef NAN
+#define DT_INVALID (NAN)
+#else
+#define DT_INVALID (DBL_MIN+DBL_MIN)
+#endif
+#ifdef HUGE_VAL
+#define DT_NOBEGIN (-HUGE_VAL)
+#define DT_NOEND (HUGE_VAL)
+#else
+#define DT_NOBEGIN (-DBL_MAX)
+#define DT_NOEND (DBL_MAX)
+#endif
+#define DT_CURRENT (DBL_MIN)
+#define DT_EPOCH (-DBL_MIN)
+
+#define DATETIME_INVALID(j) {j = DT_INVALID;}
+#ifdef NAN
+#define DATETIME_IS_INVALID(j) (isnan(j))
+#else
+#define DATETIME_IS_INVALID(j) (j == DT_INVALID)
+#endif
+
+#define DATETIME_NOBEGIN(j) {j = DT_NOBEGIN;}
+#define DATETIME_IS_NOBEGIN(j) (j == DT_NOBEGIN)
+
+#define DATETIME_NOEND(j) {j = DT_NOEND;}
+#define DATETIME_IS_NOEND(j) (j == DT_NOEND)
+
+#define DATETIME_CURRENT(j) {j = DT_CURRENT;}
+#define DATETIME_IS_CURRENT(j) (j == DT_CURRENT)
+
+#define DATETIME_EPOCH(j) {j = DT_EPOCH;}
+#define DATETIME_IS_EPOCH(j) (j == DT_EPOCH)
+
+#define DATETIME_IS_RELATIVE(j) (DATETIME_IS_CURRENT(j) || DATETIME_IS_EPOCH(j))
+#define DATETIME_NOT_FINITE(j) (DATETIME_IS_INVALID(j) \
+ || DATETIME_IS_NOBEGIN(j) || DATETIME_IS_NOEND(j))
+#define DATETIME_IS_RESERVED(j) (DATETIME_IS_RELATIVE(j) || DATETIME_NOT_FINITE(j))
+
+#define TIMESPAN_INVALID(j) {j->time = DT_INVALID;}
+#ifdef NAN
+#define TIMESPAN_IS_INVALID(j) (isnan((j).time))
+#else
+#define TIMESPAN_IS_INVALID(j) ((j).time == DT_INVALID)
+#endif
+
+#define TIME_PREC 1e-6
+#define JROUND(j) (rint(((double) j)/TIME_PREC)*TIME_PREC)
/*
* dt.c prototypes
*/
+
extern DateTime *datetime_in( char *str);
extern char *datetime_out( DateTime *dt);
+extern bool datetime_eq(DateTime *dt1, DateTime *dt2);
+extern bool datetime_ne(DateTime *dt1, DateTime *dt2);
+extern bool datetime_lt(DateTime *dt1, DateTime *dt2);
+extern bool datetime_le(DateTime *dt1, DateTime *dt2);
+extern bool datetime_ge(DateTime *dt1, DateTime *dt2);
+extern bool datetime_gt(DateTime *dt1, DateTime *dt2);
+
extern TimeSpan *timespan_in(char *str);
extern char *timespan_out(TimeSpan *span);
+extern bool timespan_eq(TimeSpan *span1, TimeSpan *span2);
+extern bool timespan_ne(TimeSpan *span1, TimeSpan *span2);
+extern bool timespan_lt(TimeSpan *span1, TimeSpan *span2);
+extern bool timespan_le(TimeSpan *span1, TimeSpan *span2);
+extern bool timespan_ge(TimeSpan *span1, TimeSpan *span2);
+extern bool timespan_gt(TimeSpan *span1, TimeSpan *span2);
+
+float64 datetime_part(text *units, DateTime *datetime);
+float64 timespan_part(text *units, TimeSpan *timespan);
+
+extern TimeSpan *timespan_um(TimeSpan *span);
+extern TimeSpan *timespan_add(TimeSpan *span1, TimeSpan *span2);
+extern TimeSpan *timespan_sub(TimeSpan *span1, TimeSpan *span2);
extern TimeSpan *datetime_sub(DateTime *dt1, DateTime *dt2);
extern DateTime *datetime_add_span(DateTime *dt, TimeSpan *span);
extern DateTime *datetime_sub_span(DateTime *dt, TimeSpan *span);
-extern TimeSpan *timespan_add(TimeSpan *span1, TimeSpan *span2);
-extern TimeSpan *timespan_sub(TimeSpan *span1, TimeSpan *span2);
+
+extern void GetCurrentTime(struct tm *tm);
+DateTime SetDateTime(DateTime datetime);
+DateTime tm2datetime(struct tm *tm, double fsec, int tzp);
+int datetime2tm( DateTime dt, struct tm *tm, double *fsec);
+
+int timespan2tm(TimeSpan span, struct tm *tm, float8 *fsec);
+int tm2timespan(struct tm *tm, double fsec, TimeSpan *span);
extern DateTime dt2local( DateTime dt, int timezone);
@@ -199,12 +285,8 @@ extern int j2day( int jd);
extern double time2t(const int hour, const int min, const double sec);
extern void dt2time(DateTime dt, int *hour, int *min, double *sec);
-/*
-extern void GetCurrentTime(struct tm *tm);
-*/
extern int ParseDateTime( char *timestr, char *lowstr,
char *field[], int ftype[], int maxfields, int *numfields);
-
extern int DecodeDateTime( char *field[], int ftype[],
int nf, int *dtype, struct tm *tm, double *fsec, int *tzp);
extern int DecodeDate(char *str, int fmask, int *tmask, struct tm *tm);
@@ -223,11 +305,10 @@ extern int DecodeDateDelta( char *field[], int ftype[],
int nf, int *dtype, struct tm *tm, double *fsec);
extern int DecodeUnits(int field, char *lowtoken, int *val);
-extern int EncodeSpecialDateTime(DateTime *dt, char *str);
+extern int EncodeSpecialDateTime(DateTime dt, char *str);
extern int EncodePostgresDate(struct tm *tm, double fsec, char *str);
extern int EncodePostgresSpan(struct tm *tm, double fsec, char *str);
-
extern datetkn *datebsearch(char *key, datetkn *base, unsigned int nel);
#endif /* DT_H */
diff --git a/src/include/utils/nabstime.h b/src/include/utils/nabstime.h
index 8a0c92b2574..e8691f72731 100644
--- a/src/include/utils/nabstime.h
+++ b/src/include/utils/nabstime.h
@@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: nabstime.h,v 1.5 1997/03/14 23:33:29 scrappy Exp $
+ * $Id: nabstime.h,v 1.6 1997/03/25 08:11:24 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
@@ -115,8 +115,6 @@ extern AbsoluteTime GetCurrentAbsoluteTime(void);
extern AbsoluteTime nabstimein(char *timestr);
extern char *nabstimeout(AbsoluteTime time);
-extern bool AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2);
-extern bool AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2);
extern bool abstimeeq(AbsoluteTime t1, AbsoluteTime t2);
extern bool abstimene(AbsoluteTime t1, AbsoluteTime t2);
extern bool abstimelt(AbsoluteTime t1, AbsoluteTime t2);
@@ -124,6 +122,10 @@ extern bool abstimegt(AbsoluteTime t1, AbsoluteTime t2);
extern bool abstimele(AbsoluteTime t1, AbsoluteTime t2);
extern bool abstimege(AbsoluteTime t1, AbsoluteTime t2);
+extern AbsoluteTime datetime_abstime(DateTime *datetime);
+
+extern bool AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2);
+extern bool AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2);
extern AbsoluteTime dateconv(struct tm *tm, int zone);
extern time_t qmktime(struct tm *tp);