1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*-------------------------------------------------------------------------
*
* nabstime.c--
* parse almost any absolute date getdate(3) can (& some it can't)
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.14 1997/03/14 23:20:31 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include "postgres.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)
*
* 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)
{
time_t now;
#ifdef USE_POSIX_TIME
now = time(NULL);
#if defined(HAVE_TZSET) && defined(HAVE_INT_TIMEZONE)
tzset();
#else /* !HAVE_TZSET */
struct tm *tmnow = localtime(&now);
timezone = - tmnow->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
daylight = (tmnow->tm_isdst > 0);
#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);
#endif
return((AbsoluteTime) now);
} /* GetCurrentAbsoluteTime() */
void
GetCurrentTime(struct tm *tm)
{
time_t now;
struct tm *tt;
now = GetCurrentTransactionStartTime();
tt = gmtime( &now);
tm->tm_year = tt->tm_year+1900;
tm->tm_mon = tt->tm_mon+1;
tm->tm_mday = tt->tm_mday;
tm->tm_hour = tt->tm_hour;
tm->tm_min = tt->tm_min;
tm->tm_sec = tt->tm_sec;
tm->tm_isdst = tt->tm_isdst;
return;
} /* GetCurrentTime() */
/* nabstimein()
* Decode date/time string and return abstime.
*/
AbsoluteTime
nabstimein(char* str)
{
int sec;
double fsec;
int day, tz = 0;
struct tm date, *tm = &date;
char *field[MAXDATEFIELDS];
char lowstr[MAXDATELEN+1];
int dtype;
int nf, ftype[MAXDATEFIELDS];
if (!PointerIsValid(str))
elog(WARN,"Bad (null) abstime external representation",NULL);
if (strlen(str) > MAXDATELEN)
elog( WARN, "Bad (length) abstime external representation '%s'",str);
if ((ParseDateTime( str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeDateTime( field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
elog( WARN, "Bad abstime external representation '%s'",str);
#ifdef DATEDEBUG
printf( "nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE);
#endif
switch (dtype) {
case DTK_DATE:
#if FALSE
return(dateconv( &date, tz));
#endif
/* validate, before going out of range on some members */
if (tm->tm_year < 1901 || tm->tm_year > 2038
|| tm->tm_mon < 1 || tm->tm_mon > 12
|| tm->tm_mday < 1 || tm->tm_mday > 31
|| tm->tm_hour < 0 || tm->tm_hour >= 24
|| tm->tm_min < 0 || tm->tm_min > 59
|| tm->tm_sec < 0 || tm->tm_sec > 59)
return INVALID_ABSTIME;
day = (date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j( 1970, 1, 1));
/* check for time out of range */
if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
return INVALID_ABSTIME;
/* convert to seconds */
sec = tm->tm_sec + tz + (tm->tm_min +(day*24 + tm->tm_hour)*60)*60;
/* check for overflow */
if ((day == MAX_DAYNUM && sec < 0) ||
(day == MIN_DAYNUM && sec > 0))
return INVALID_ABSTIME;
/* daylight correction */
if (tm->tm_isdst < 0) { /* unknown; find out */
tm->tm_isdst = (daylight > 0);
};
if (tm->tm_isdst > 0)
sec -= 60*60;
/* check for reserved values (e.g. "current" on edge of usual range */
if (!AbsoluteTimeIsReal(sec))
return INVALID_ABSTIME;
return sec;
case DTK_EPOCH:
return EPOCH_ABSTIME;
case DTK_CURRENT:
return CURRENT_ABSTIME;
case DTK_LATE:
return NOEND_ABSTIME;
case DTK_EARLY:
return NOSTART_ABSTIME;
case DTK_INVALID:
return INVALID_ABSTIME;
default:
};
elog(WARN,"Bad abstime (internal coding error) '%s'",str);
return INVALID_ABSTIME;
} /* nabstimein() */
/*
* Given an AbsoluteTime return the English text version of the date
*/
char *
nabstimeout(AbsoluteTime time)
{
/*
* Fri Jan 28 23:05:29 1994 PST
* 0 1 2
* 12345678901234567890123456789
*
* we allocate some extra -- timezones are usually 3 characters but
* this is not in the POSIX standard...
*/
char buf[40];
char* result;
switch (time) {
case EPOCH_ABSTIME: (void) strcpy(buf, EPOCH); break;
case INVALID_ABSTIME: (void) strcpy(buf, INVALID); break;
case CURRENT_ABSTIME: (void) strcpy(buf, DCURRENT); break;
case NOEND_ABSTIME: (void) strcpy(buf, LATE); break;
case NOSTART_ABSTIME: (void) strcpy(buf, EARLY); break;
default:
/* hack -- localtime happens to work for negative times */
(void) strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y %Z",
localtime((time_t *) &time));
break;
}
result = (char*)palloc(strlen(buf) + 1);
strcpy(result, buf);
return result;
}
/* turn a (struct tm) and a few variables into a time_t, with range checking */
AbsoluteTime
dateconv(register struct tm *tm, int zone)
{
tm->tm_wday = tm->tm_yday = 0;
#if FALSE
if (tm->tm_year < 70) {
tm->tm_year += 2000;
} else if (tm->tm_year < 1000) {
tm->tm_year += 1900;
};
#endif
/* validate, before going out of range on some members */
if (tm->tm_year < 1901 || tm->tm_year > 2038
|| tm->tm_mon < 1 || tm->tm_mon > 12
|| tm->tm_mday < 1 || tm->tm_mday > 31
|| tm->tm_hour < 0 || tm->tm_hour >= 24
|| tm->tm_min < 0 || tm->tm_min > 59
|| tm->tm_sec < 0 || tm->tm_sec > 59)
return INVALID_ABSTIME;
/*
* zone should really be -zone, and tz should be set to tp->value, not
* -tp->value. Or the table could be fixed.
*/
tm->tm_sec += zone; /* mktime lets it be out of range */
/* convert to seconds */
return qmktime(tm);
}
/*
* near-ANSI qmktime suitable for use by dateconv; not necessarily as paranoid
* as ANSI requires, and it may not canonicalise the struct tm. Ignores tm_wday
* and tm_yday.
*/
time_t
qmktime(struct tm *tm)
{
time_t sec;
int day;
#if FALSE
/* If it was a 2 digit year */
if (tm->tm_year < 100)
tm->tm_year += 1900;
#endif
day = (date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j( 1970, 1, 1));
/* check for time out of range */
if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
return INVALID_ABSTIME;
/* convert to seconds */
sec = tm->tm_sec + (tm->tm_min +(day*24 + tm->tm_hour)*60)*60;
/* check for overflow */
if ((day == MAX_DAYNUM && sec < 0) ||
(day == MIN_DAYNUM && sec > 0))
return INVALID_ABSTIME;
/* check for reserved values (e.g. "current" on edge of usual range */
if (!AbsoluteTimeIsReal(sec))
return INVALID_ABSTIME;
/* daylight correction */
if (tm->tm_isdst < 0) { /* unknown; find out */
tm->tm_isdst = (daylight > 0);
};
if (tm->tm_isdst > 0)
sec -= 60*60;
return sec;
} /* qmktime() */
/*
* AbsoluteTimeIsBefore -- true iff time1 is before time2.
* AbsoluteTimeIsBefore -- true iff time1 is after time2.
*/
bool
AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
{
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
return (time1 < time2);
}
bool
AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
{
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
return (time1 > time2);
}
/*
* abstimeeq - returns 1, iff arguments are equal
* abstimene - returns 1, iff arguments are not equal
* abstimelt - returns 1, iff t1 less than t2
* abstimegt - returns 1, iff t1 greater than t2
* abstimele - returns 1, iff t1 less than or equal to t2
* abstimege - returns 1, iff t1 greater than or equal to t2
*/
bool
abstimeeq(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 == t2);
}
bool
abstimene(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 != t2);
}
bool
abstimelt(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 < t2);
}
bool
abstimegt(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 > t2);
}
bool
abstimele(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 <= t2);
}
bool
abstimege(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return 0;
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 >= t2);
}
|