Skip to content

Commit a1f229b

Browse files
committed
From: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at>
Subject: [HACKERS] Patch: set date to euro/us postgres/iso/sql Here a patch that implements a SET date for use by the datetime stuff. The syntax is SET date TO 'val[,val,...]' where val is us (us dates), euro (european dates), postgres, iso or sql. Thomas is working on the integration in his datetime module. I just needed to get the patch out before it went stale :)
1 parent cfe0d67 commit a1f229b

File tree

3 files changed

+187
-2
lines changed

3 files changed

+187
-2
lines changed

src/backend/tcop/variable.c

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,152 @@
1+
/*
2+
* Routines for handling of SET var TO statements
3+
*
4+
* $Id: variable.c,v 1.3 1997/04/17 13:50:30 scrappy Exp $
5+
*
6+
* $Log: variable.c,v $
7+
* Revision 1.3 1997/04/17 13:50:30 scrappy
8+
* From: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at>
9+
* Subject: [HACKERS] Patch: set date to euro/us postgres/iso/sql
10+
*
11+
* Here a patch that implements a SET date for use by the datetime
12+
* stuff. The syntax is
13+
*
14+
* SET date TO 'val[,val,...]'
15+
*
16+
* where val is us (us dates), euro (european dates), postgres,
17+
* iso or sql.
18+
*
19+
* Thomas is working on the integration in his datetime module.
20+
* I just needed to get the patch out before it went stale :)
21+
*
22+
* Revision 1.1 1997/04/10 16:52:07 mjl
23+
* Initial revision
24+
*/
25+
/*-----------------------------------------------------------------------*/
26+
27+
#include <string.h>
128
#include "postgres.h"
229
#include "tcop/variable.h"
330

31+
/*-----------------------------------------------------------------------*/
32+
#if USE_EURODATES
33+
#define DATE_EURO TRUE
34+
#else
35+
#define DATE_EURO FALSE
36+
#endif
37+
38+
/*-----------------------------------------------------------------------*/
39+
struct PGVariables PGVariables =
40+
{
41+
{ DATE_EURO, Date_Postgres }
42+
};
43+
44+
/*-----------------------------------------------------------------------*/
45+
static const char *get_token(char *buf, int size, const char *str)
46+
{
47+
if(!*str)
48+
return NULL;
49+
50+
/* skip white space */
51+
while(*str && (*str == ' ' || *str == '\t'))
52+
str++;
53+
54+
/* copy until we hit white space or comma or end of string */
55+
while(*str && *str != ' ' && *str != '\t' && *str != ',' && size-- > 1)
56+
*buf++ = *str++;
57+
58+
*buf = '\0';
59+
60+
/* skip white space and comma*/
61+
while(*str && (*str == ' ' || *str == '\t' || *str == ','))
62+
str++;
63+
64+
return str;
65+
}
66+
67+
/*-----------------------------------------------------------------------*/
68+
static bool parse_null(const char *value)
69+
{
70+
return TRUE;
71+
}
72+
73+
static bool parse_date(const char *value)
74+
{
75+
char tok[32];
76+
int dcnt = 0, ecnt = 0;
77+
78+
while(value = get_token(tok, sizeof(tok), value))
79+
{
80+
/* Ugh. Somebody ought to write a table driven version -- mjl */
81+
82+
if(!strcasecmp(tok, "iso"))
83+
{
84+
PGVariables.date.format = Date_ISO;
85+
dcnt++;
86+
}
87+
else if(!strcasecmp(tok, "sql"))
88+
{
89+
PGVariables.date.format = Date_SQL;
90+
dcnt++;
91+
}
92+
else if(!strcasecmp(tok, "postgres"))
93+
{
94+
PGVariables.date.format = Date_Postgres;
95+
dcnt++;
96+
}
97+
else if(!strcasecmp(tok, "euro"))
98+
{
99+
PGVariables.date.euro = TRUE;
100+
ecnt++;
101+
}
102+
else if(!strcasecmp(tok, "us"))
103+
{
104+
PGVariables.date.euro = FALSE;
105+
ecnt++;
106+
}
107+
else
108+
{
109+
elog(WARN, "Bad value for date (%s)", tok);
110+
}
111+
}
112+
113+
if(dcnt > 1 || ecnt > 1)
114+
elog(NOTICE, "Conflicting settings for date");
115+
116+
return TRUE;
117+
}
118+
119+
/*-----------------------------------------------------------------------*/
120+
struct VariableParsers
121+
{
122+
const char *name;
123+
bool (*parser)(const char *);
124+
} VariableParsers[] =
125+
{
126+
{ "date", parse_date },
127+
{ "timezone", parse_null },
128+
{ NULL }
129+
};
130+
131+
/*-----------------------------------------------------------------------*/
4132
bool SetPGVariable(const char *name, const char *value)
5133
{
6-
elog(NOTICE, "Variable %s set to \"%s\"", name, value);
134+
struct VariableParsers *vp;
135+
136+
for(vp = VariableParsers; vp->name; vp++)
137+
{
138+
if(!strcasecmp(vp->name, name))
139+
return (vp->parser)(value);
140+
}
141+
142+
elog(NOTICE, "No such variable %s", name);
7143

8144
return TRUE;
9145
}
10146

147+
/*-----------------------------------------------------------------------*/
11148
const char *GetPGVariable(const char *varName)
12149
{
13150
return NULL;
14151
}
152+
/*-----------------------------------------------------------------------*/

src/backend/utils/adt/datetime.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.3 1997/04/04 08:55:29 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.4 1997/04/17 13:50:34 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include <stdio.h> /* for sprintf() */
1515
#include <string.h>
16+
#include <limits.h>
1617

1718
#include "postgres.h"
1819
#ifdef HAVE_FLOAT_H

src/include/tcop/variable.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
1+
/*
2+
* Headers for handling of SET var TO statements
3+
*
4+
* $Id: variable.h,v 1.2 1997/04/17 13:50:57 scrappy Exp $
5+
*
6+
* $Log: variable.h,v $
7+
* Revision 1.2 1997/04/17 13:50:57 scrappy
8+
* From: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at>
9+
* Subject: [HACKERS] Patch: set date to euro/us postgres/iso/sql
10+
*
11+
* Here a patch that implements a SET date for use by the datetime
12+
* stuff. The syntax is
13+
*
14+
* SET date TO 'val[,val,...]'
15+
*
16+
* where val is us (us dates), euro (european dates), postgres,
17+
* iso or sql.
18+
*
19+
* Thomas is working on the integration in his datetime module.
20+
* I just needed to get the patch out before it went stale :)
21+
*
22+
* Revision 1.1 1997/04/10 16:53:30 mjl
23+
* Initial revision
24+
*
25+
*/
26+
/*-----------------------------------------------------------------------*/
27+
28+
enum DateFormat { Date_Postgres, Date_SQL, Date_ISO };
29+
30+
/*-----------------------------------------------------------------------*/
31+
struct PGVariables
32+
{
33+
struct
34+
{
35+
bool euro;
36+
enum DateFormat format;
37+
} date;
38+
};
39+
40+
extern struct PGVariables PGVariables;
41+
42+
/*-----------------------------------------------------------------------*/
43+
bool SetPGVariable(const char *, const char *);
44+
const char *GetPGVariable(const char *);
45+
46+
/*-----------------------------------------------------------------------*/
147
bool SetPGVariable(const char *, const char *);
248
const char *GetPGVariable(const char *);

0 commit comments

Comments
 (0)