From b5e16b1869076ae49e9b313e366664464f8e8989 Mon Sep 17 00:00:00 2001
From: "Marc G. Fournier" <scrappy@hub.org>
Date: Tue, 18 Mar 1997 16:36:50 +0000
Subject: [PATCH] Resync the source tree, commit some things that were missing
 (pqcomprim.c) and bring in Thomas's updates for the date/time code...

---
 src/backend/libpq/pqcomprim.c    | 82 ++++++++++++++++++++++++++++++++
 src/backend/utils/adt/dt.c       | 48 ++++++-------------
 src/backend/utils/adt/nabstime.c | 63 +++++++++++++-----------
 src/backend/utils/init/globals.c |  8 +++-
 src/include/miscadmin.h          | 15 +++++-
 src/include/utils/dt.h           |  4 +-
 6 files changed, 151 insertions(+), 69 deletions(-)
 create mode 100644 src/backend/libpq/pqcomprim.c

diff --git a/src/backend/libpq/pqcomprim.c b/src/backend/libpq/pqcomprim.c
new file mode 100644
index 00000000000..fa1fc866b88
--- /dev/null
+++ b/src/backend/libpq/pqcomprim.c
@@ -0,0 +1,82 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "postgres.h"
+#include "libpq/pqcomm.h"
+
+/* --------------------------------------------------------------------- */
+/* Is the other way around than system ntoh/hton, so we roll our own
+	here */
+	
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define ntoh_s(n) n
+#define ntoh_l(n) n
+#define hton_s(n) n
+#define hton_l(n) n
+#endif
+#if BYTE_ORDER == BIG_ENDIAN
+#define ntoh_s(n) (u_short)(((u_char *) &n)[0] << 8 | ((u_char *) &n)[1]);
+#define ntoh_l(n) (u_long)(((u_char *)&n)[0] << 24 | ((u_char *)&n)[1] << 16 |\
+      	             	   ((u_char *)&n)[2] << 8 | ((u_char *)&n)[3]);
+#define hton_s(n) (ntoh_s(n))
+#define hton_l(n) (ntoh_l(n))
+#endif
+#if BYTE_ORDER == PDP_ENDIAN
+#endif
+#ifndef ntoh_s
+#error Please write byte order macros
+#endif
+
+/* --------------------------------------------------------------------- */
+int pqPutShort(const int integer, FILE *f)
+    {
+    int retval = 0;
+    u_short n;
+		
+    n = hton_s(integer);
+    if(fwrite(&n, sizeof(u_short), 1, f) != 1)
+    	retval = 1;
+    
+    return retval;
+    }
+
+/* --------------------------------------------------------------------- */
+int pqPutLong(const int integer, FILE *f)
+    {
+    int retval = 0;
+    u_long n;
+		
+    n = hton_l(integer);
+    if(fwrite(&n, sizeof(u_long), 1, f) != 1)
+    	retval = 1;
+    
+    return retval;
+    }
+    
+/* --------------------------------------------------------------------- */
+int pqGetShort(int *result, FILE *f)
+    {
+    int retval = 0;
+    u_short n;
+
+    if(fread(&n, sizeof(u_short), 1, f) != 1)
+    	retval = 1;
+			
+    *result = ntoh_s(n);
+    return retval;
+    }
+
+/* --------------------------------------------------------------------- */
+int pqGetLong(int *result, FILE *f)
+    {
+    int retval = 0;
+    u_long n;
+		
+    if(fread(&n, sizeof(u_long), 1, f) != 1)
+    	retval = 1;
+			
+    *result = ntoh_l(n);
+    return retval;
+    }
+
+/* --------------------------------------------------------------------- */
diff --git a/src/backend/utils/adt/dt.c b/src/backend/utils/adt/dt.c
index 815dc9915a6..8439de5adf7 100644
--- a/src/backend/utils/adt/dt.c
+++ b/src/backend/utils/adt/dt.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.6 1997/03/16 19:03:20 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.7 1997/03/18 16:35:17 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,28 +17,16 @@
 #include <string.h>
 #include <sys/types.h>
 #include <errno.h>
-#ifdef HAVE_VALUES_H
-# include <values.h>
-#else
-# include <float.h>
-# ifndef MINDOUBLE
-#  define MINDOUBLE DBL_MIN
-# endif
-#endif
 
 #include "postgres.h"
+#include <miscadmin.h>
+#ifndef USE_POSIX_TIME
+#include <sys/timeb.h>
+#endif
 #include "utils/builtins.h"
 
-extern int EuroDates;
-
-#define MAXDATEFIELDS 25
-
 #define USE_DATE_CACHE 1
 
-extern char *tzname[2];
-extern long int timezone;
-extern int daylight;
-
 #define JTIME_INVALID		(NAN)
 #define DATETIME_INVALID(j)	{*j = JTIME_INVALID;}
 #define DATETIME_IS_INVALID(j)	(isnan(*j))
@@ -114,7 +102,7 @@ printf( "datetime_in- time is %f %02d:%02d:%02d %f\n", time, tm->tm_hour, tm->tm
 	if (tzp != 0) {
 	    *result = dt2local(*result, -tzp);
 	} else {
-	    *result = dt2local(*result, -timezone);
+	    *result = dt2local(*result, -CTimeZone);
 	};
 #ifdef DATEDEBUG
 printf( "datetime_in- date is %f\n", *result);
@@ -168,7 +156,7 @@ datetime_out(DateTime *dt)
 
     } else {
 
-	time = (modf( dt2local( *dt, timezone)/86400, &date)*86400);
+	time = (modf( dt2local( *dt, CTimeZone)/86400, &date)*86400);
 	date += date2j(2000,1,1);
 	if (time < 0) {
 	    time += 86400;
@@ -197,8 +185,8 @@ printf( "datetime_out- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min,
 	tm->tm_isdst = -1;
 
 #ifdef DATEDEBUG
-printf( "datetime_out- timezone is %s/%s; offset is %ld; daylight is %d\n",
- tzname[0], tzname[1], timezone, daylight);
+printf( "datetime_out- timezone is %s; offset is %ld; daylight is %d\n",
+ CTZName, CTimeZone, CDayLight);
 #endif
 
 	EncodePostgresDate(tm, fsec, buf);
@@ -705,12 +693,6 @@ void dt2time(DateTime jd, int *hour, int *min, double *sec)
  * Returns the number of seconds since epoch (J2000)
  */
 
-#ifndef USE_POSIX_TIME
-long int timezone;
-long int daylight;
-#endif
-
-
 /* ParseDateTime()
  * Break string into tokens based on a date/time context.
  */
@@ -845,7 +827,7 @@ DecodeDateTime( char *field[], int ftype[], int nf,
     tm->tm_min = 0;
     tm->tm_sec = 0;
     tm->tm_isdst = -1;	/* don't know daylight savings time status apriori */
-    if (tzp != NULL) *tzp = timezone;
+    if (tzp != NULL) *tzp = CTimeZone;
 
     for (i = 0; i < nf; i++) {
 #ifdef DATEDEBUG
@@ -1828,8 +1810,8 @@ int EncodePostgresDate(struct tm *tm, double fsec, char *str)
     tm->tm_isdst = -1;
 
 #ifdef DATEDEBUG
-printf( "EncodePostgresDate- timezone is %s/%s; offset is %ld; daylight is %d\n",
- tzname[0], tzname[1], timezone, daylight);
+printf( "EncodePostgresDate- timezone is %s; offset is %ld; daylight is %d\n",
+ CTZName, CTimeZone, CDayLight);
 #endif
 
     day = date2j( tm->tm_year, tm->tm_mon, tm->tm_mday);
@@ -1848,15 +1830,15 @@ printf( "EncodePostgresDate- day is %d\n", day);
 
     if (EuroDates) {
 	sprintf( str, "%3s %02d/%02d/%04d %02d:%02d:%02d %s", dabbrev,
-	  tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_hour, tm->tm_min, (int) rint(sec), tzname[0]);
+	  tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_hour, tm->tm_min, (int) rint(sec), CTZName);
 
     } else if (tm->tm_year > 0) {
 #if FALSE
 	sprintf( str, "%3s %3s %02d %02d:%02d:%02d %04d %s", dabbrev,
-	  mabbrev, tm->tm_mday, tm->tm_hour, tm->tm_min, (int) rint(sec), tm->tm_year, tzname[0]);
+	  mabbrev, tm->tm_mday, tm->tm_hour, tm->tm_min, (int) rint(sec), tm->tm_year, CTZName);
 #endif
 	sprintf( str, "%3s %3s %02d %02d:%02d:%5.2f %04d %s", dabbrev,
-	  mabbrev, tm->tm_mday, tm->tm_hour, tm->tm_min, sec, tm->tm_year, tzname[0]);
+	  mabbrev, tm->tm_mday, tm->tm_hour, tm->tm_min, sec, tm->tm_year, CTZName);
 	/* XXX brute-force fill in leading zero on seconds */
 	if (*(str+17) == ' ') *(str+17) = '0';
 
diff --git a/src/backend/utils/adt/nabstime.c b/src/backend/utils/adt/nabstime.c
index e678805c989..58f4109e8c4 100644
--- a/src/backend/utils/adt/nabstime.c
+++ b/src/backend/utils/adt/nabstime.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.14 1997/03/14 23:20:31 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.15 1997/03/18 16:35:20 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,35 +17,24 @@
 #include <sys/types.h>
 
 #include "postgres.h"
+#include <miscadmin.h>
 #ifndef USE_POSIX_TIME
 #include <sys/timeb.h>
 #endif
 #include "access/xact.h"
 
-#if USE_EURODATES
-extern int EuroDates;
-#endif
-
-#if FALSE
-#define MAXDATELEN	47
-#define MAXDATEFIELDS	25
-#endif
 
 #define MIN_DAYNUM -24856			/* December 13, 1901 */
 #define MAX_DAYNUM 24854			/* January 18, 2038 */
 
 
-/*
- * parse and convert absolute date in timestr (the normal interface)
+/* GetCurrentAbsoluteTime()
+ * Get the current system time. Set timezone parameters if not specified elsewhere.
+ * Define HasTZSet to allow clients to specify the default timezone.
  *
  * Returns the number of seconds since epoch (January 1 1970 GMT)
  */
 
-#ifndef USE_POSIX_TIME
-long int timezone;
-long int daylight;
-#endif
-
 AbsoluteTime
 GetCurrentAbsoluteTime(void)
 {
@@ -53,24 +42,40 @@ GetCurrentAbsoluteTime(void)
 
 #ifdef USE_POSIX_TIME
     now = time(NULL);
+#else /* ! USE_POSIX_TIME */
+    struct timeb tbnow;		/* the old V7-ism */
+
+    (void) ftime(&tbnow);
+    now = tbnow.time;
+#endif
 
+    if (! HasCTZSet) {
+#ifdef USE_POSIX_TIME
 #if defined(HAVE_TZSET) && defined(HAVE_INT_TIMEZONE)
-    tzset();
+	tzset();
+	CTimeZone = timezone;
+	CDayLight = daylight;
+	strcpy( CTZName, tzname[0]);
 #else /* !HAVE_TZSET */
-    struct tm *tmnow = localtime(&now);
+	struct tm *tmnow = localtime(&now);
 
-    timezone = - tmnow->tm_gmtoff;	/* tm_gmtoff is Sun/DEC-ism */
-    daylight = (tmnow->tm_isdst > 0);
+	CTimeZone = - tmnow->tm_gmtoff;	/* tm_gmtoff is Sun/DEC-ism */
+	CDayLight = (tmnow->tm_isdst > 0);
+	/* XXX is there a better way to get local timezone string in V7? - tgl 97/03/18 */
+	strftime( CTZName, "%Z", localtime(&now));
 #endif
-
 #else /* ! USE_POSIX_TIME */
-    struct timeb tbnow;		/* the old V7-ism */
-
-    (void) ftime(&tbnow);
-    now = tbnow.time;
-    timezone = tbnow.timezone * 60;
-    daylight = (tbnow.dstflag != 0);
+	CTimeZone = tbnow.timezone * 60;
+	CDayLight = (tbnow.dstflag != 0);
+	/* XXX does this work to get the local timezone string in V7? - tgl 97/03/18 */
+	strftime( CTZName, "%Z", localtime(&now));
 #endif 
+    };
+
+#ifdef DATEDEBUG
+printf( "GetCurrentAbsoluteTime- timezone is %s -> %d seconds from UTC\n",
+ CTZName, CTimeZone);
+#endif
 
     return((AbsoluteTime) now);
 } /* GetCurrentAbsoluteTime() */
@@ -157,7 +162,7 @@ printf( "nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE
 
 	/* daylight correction */
 	if (tm->tm_isdst < 0) {		/* unknown; find out */
-	    tm->tm_isdst = (daylight > 0);
+	    tm->tm_isdst = (CDayLight > 0);
 	};
 	if (tm->tm_isdst > 0)
 	    sec -= 60*60;
@@ -298,7 +303,7 @@ qmktime(struct tm *tm)
 
     /* daylight correction */
     if (tm->tm_isdst < 0) {		/* unknown; find out */
-	tm->tm_isdst = (daylight > 0);
+	tm->tm_isdst = (CDayLight > 0);
     };
     if (tm->tm_isdst > 0)
 	sec -= 60*60;
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index 8ab5fa82f91..65ea4b10dd4 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.5 1997/01/26 15:31:29 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.6 1997/03/18 16:35:46 scrappy Exp $
  *
  * NOTES
  *    Globals used all over the place should be declared here and not
@@ -65,7 +65,11 @@ bool		IsPostmaster = false;
 
 short		DebugLvl = 0;
 
-int             EuroDates = 0;
+bool		EuroDates = false;
+bool		HasCTZSet = false;
+bool		CDayLight = false;
+int		CTimeZone = 0;
+char		CTZName[8] = "";
 
 char *IndexedCatalogNames[] = {
     AttributeRelationName,
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index 5fab595f175..81fcece3463 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -11,7 +11,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: miscadmin.h,v 1.5 1997/01/26 15:32:06 scrappy Exp $
+ * $Id: miscadmin.h,v 1.6 1997/03/18 16:36:23 scrappy Exp $
  *
  * NOTES
  *    some of the information in this file will be moved to
@@ -57,7 +57,18 @@ extern bool	    IsPostmaster;
 
 extern short	    DebugLvl;
 
-extern int          EuroDates;
+/* Date/Time Configuration
+ * HasCTZSet if client timezone is specified by client.
+ * EuroDates if client prefers dates interpreted and written w/European conventions.
+ * CTimeZone is the timezone offset in seconds.
+ * CTZName is the timezone label.
+ */
+
+extern bool	    EuroDates;
+extern bool	    HasCTZSet;
+extern bool	    CDayLight;
+extern int	    CTimeZone;
+extern char	    CTZName[];
 
 extern Oid	    LastOidProcessed;	/* for query rewrite */
 
diff --git a/src/include/utils/dt.h b/src/include/utils/dt.h
index 4adda5e8928..b9f20b03aa8 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.1 1997/03/14 23:33:23 scrappy Exp $
+ * $Id: dt.h,v 1.2 1997/03/18 16:36:50 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -174,8 +174,6 @@ typedef struct {
     char value;		/* this may be unsigned, alas */
 } datetkn;
 
-
-extern int EuroDates;
 extern void GetCurrentTime(struct tm *tm);
 
 /*
-- 
GitLab