*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.77 1999/12/09 05:02:24 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.78 2000/01/02 01:37:27 momjian Exp $
*
*-------------------------------------------------------------------------
*/
case DTK_DATE:
if (tm2datetime(tm, fsec, &tz, result) != 0)
elog(ERROR, "Datetime out of range '%s'", str);
-
-#ifdef DATEDEBUG
- printf("datetime_in- date is %f\n", *result);
-#endif
-
break;
case DTK_EPOCH:
tm->tm_min = t0->tm_min;
tm->tm_sec = t0->tm_sec;
- if (tm->tm_year < 1900)
- tm->tm_year += 1900;
+ tm->tm_year += 1900;
tm->tm_mon++;
-#ifdef DATEDEBUG
- printf("GetEpochTime- %04d-%02d-%02d %02d:%02d:%02d\n",
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
-
return;
} /* GetEpochTime() */
GetCurrentTime(&tt);
tm2datetime(&tt, 0, NULL, &dt);
dt = dt2local(dt, -CTimeZone);
-
-#ifdef DATEDEBUG
- printf("SetDateTime- current time is %f\n", dt);
-#endif
}
else
{ /* if (DATETIME_IS_EPOCH(dt1)) */
GetEpochTime(&tt);
tm2datetime(&tt, 0, NULL, &dt);
-#ifdef DATEDEBUG
- printf("SetDateTime- epoch time is %f\n", dt);
-#endif
}
return dt;
if (DATETIME_IS_RELATIVE(dt2))
dt2 = SetDateTime(dt2);
-#ifdef DATEDEBUG
- printf("datetime_gt- %f %s greater than %f\n", dt1, ((dt1 > dt2) ? "is" : "is not"), dt2);
-#endif
return dt1 > dt2;
} /* datetime_gt() */
if (DATETIME_IS_RELATIVE(dt2))
dt2 = SetDateTime(dt2);
-#ifdef DATEDEBUG
- printf("datetime_mi- evaluate %f - %f\n", dt1, dt2);
-#endif
-
if (DATETIME_IS_INVALID(dt1)
|| DATETIME_IS_INVALID(dt2))
{
result = palloc(sizeof(DateTime));
-#ifdef DATEDEBUG
- printf("datetime_pl_span- add %f to %d %f\n", *datetime, span->month, span->time);
-#endif
-
if (DATETIME_NOT_FINITE(*datetime))
{
*result = *datetime;
if (datetime2tm(dt, &tz, tm, &fsec, &tzn) == 0)
{
-#ifdef DATEDEBUG
- printf("datetime_pl_span- date was %04d-%02d-%02d %02d:%02d:%02d\n",
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
tm->tm_mon += span->month;
if (tm->tm_mon > 12)
{
if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
-#ifdef DATEDEBUG
- printf("datetime_pl_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n",
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
if (tm2datetime(tm, fsec, &tz, &dt) != 0)
elog(ERROR, "Unable to add datetime and timespan", NULL);
if (timespan2->month != 0)
span2 += (timespan2->month * (30.0 * 86400));
-#ifdef DATEDEBUG
- printf("timespan_smaller- months %d %d times %f %f spans %f %f\n",
- timespan1->month, timespan2->month, timespan1->time, timespan2->time, span1, span2);
-#endif
-
if (span2 < span1)
{
result->time = timespan2->time;
if (timespan2->month != 0)
span2 += (timespan2->month * (30.0 * 86400));
-#ifdef DATEDEBUG
- printf("timespan_larger- months %d %d times %f %f spans %f %f\n",
- timespan1->month, timespan2->month, timespan1->time, timespan2->time, span1, span2);
-#endif
-
if (span2 > span1)
{
result->time = timespan2->time;
type = DecodeUnits(0, lowunits, &val);
-#ifdef DATEDEBUG
- if (type == IGNORE)
- strcpy(lowunits, "(unknown)");
- printf("datetime_trunc- units %s type=%d value=%d\n", lowunits, type, val);
-#endif
-
if (DATETIME_NOT_FINITE(*datetime))
{
#if NOT_USED
type = DecodeUnits(0, lowunits, &val);
-#ifdef DATEDEBUG
- if (type == IGNORE)
- strcpy(lowunits, "(unknown)");
- printf("timespan_trunc- units %s type=%d value=%d\n", lowunits, type, val);
-#endif
-
if (TIMESPAN_IS_INVALID(*timespan))
{
#if NOT_USED
if (type == IGNORE)
type = DecodeSpecial(0, lowunits, &val);
-#ifdef DATEDEBUG
- if (type == IGNORE)
- strcpy(lowunits, "(unknown)");
- printf("datetime_part- units %s type=%d value=%d\n", lowunits, type, val);
-#endif
-
if (DATETIME_NOT_FINITE(*datetime))
{
#if NOT_USED
if (type == IGNORE)
type = DecodeSpecial(0, lowunits, &val);
-#ifdef DATEDEBUG
- if (type == IGNORE)
- strcpy(lowunits, "(unknown)");
- printf("timespan_part- units %s type=%d value=%d\n", lowunits, type, val);
-#endif
-
if (TIMESPAN_IS_INVALID(*timespan))
{
#if NOT_USED
type = DecodeSpecial(0, lowzone, &val);
-#ifdef DATEDEBUG
- if (type == IGNORE)
- strcpy(lowzone, "(unknown)");
- printf("datetime_zone- zone %s type=%d value=%d\n", lowzone, type, val);
-#endif
-
if (DATETIME_NOT_FINITE(*datetime))
{
/* add offset to go from J2000 back to standard Julian date */
date += date0;
-#ifdef DATEDEBUG
- printf("datetime2tm- date is %f (%f %f)\n", dt, date, time);
-#endif
-
j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
dt2time(time, &tm->tm_hour, &tm->tm_min, &sec);
-#ifdef DATEDEBUG
- printf("datetime2tm- date is %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
- printf("datetime2tm- time is %02d:%02d:%02.0f\n", tm->tm_hour, tm->tm_min, sec);
-#endif
-
*fsec = JROUND(sec);
TMODULO(*fsec, tm->tm_sec, 1e0);
-#ifdef DATEDEBUG
- printf("datetime2tm- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec);
-#endif
-
if (tzp != NULL)
{
if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
#ifdef USE_POSIX_TIME
tx = localtime(&utime);
-#ifdef DATEDEBUG
-#if defined(HAVE_TM_ZONE)
- printf("datetime2tm- (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, sec,
- tx->tm_zone, tx->tm_isdst);
-#elif defined(HAVE_INT_TIMEZONE)
- printf("datetime2tm- (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, sec,
- tzname[0], tzname[1], tx->tm_isdst);
-#else
-#error USE_POSIX_TIME is defined but neither HAVE_TM_ZONE or HAVE_INT_TIMEZONE are defined
-#endif
#endif
tm->tm_year = tx->tm_year + 1900;
tm->tm_mon = tx->tm_mon + 1;
*tzn = NULL;
}
-#ifdef DATEDEBUG
- printf("datetime2tm- date is %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
- printf("datetime2tm- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec);
-#endif
-
-#ifdef DATEDEBUG
-#ifdef USE_POSIX_TIME
-#if defined(HAVE_TM_ZONE)
- printf("datetime2tm- timezone is %s; offset is %d\n",
- tm->tm_zone, ((tzp != NULL) ? *tzp : 0));
-#elif defined(HAVE_INT_TIMEZONE)
- printf("datetime2tm- timezone is %s; offset is %d (%d); daylight is %d\n",
- tzname[tm->tm_isdst != 0], ((tzp != NULL) ? *tzp : 0), CTimeZone, CDayLight);
-#endif
-#endif
-#endif
-
return 0;
} /* datetime2tm() */
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
time = time2t(tm->tm_hour, tm->tm_min, (tm->tm_sec + fsec));
*result = (date * 86400 + time);
-#ifdef DATEDEBUG
- printf("tm2datetime- date is %f (%f %f %d)\n", *result, date, time, (((tm->tm_hour * 60) + tm->tm_min) * 60 + tm->tm_sec));
- printf("tm2datetime- time is %f %02d:%02d:%02d %f\n", time, tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
-#endif
if (tzp != NULL)
*result = dt2local(*result, -(*tzp));
TMODULO(time, tm->tm_sec, 1e0);
*fsec = time;
-#ifdef DATEDEBUG
- printf("timespan2tm- %d %f = %04d-%02d-%02d %02d:%02d:%02d %.2f\n", span.month, span.time,
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec);
-#endif
-
return 0;
} /* timespan2tm() */
span->time = ((((((tm->tm_mday * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60) + tm->tm_sec);
span->time = JROUND(span->time + fsec);
-#ifdef DATEDEBUG
- printf("tm2timespan- %d %f = %04d-%02d-%02d %02d:%02d:%02d %.2f\n", span->month, span->time,
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
-#endif
-
return 0;
} /* tm2timespan() */
char *cp = timestr;
char *lp = lowstr;
-#ifdef DATEDEBUG
- printf("ParseDateTime- input string is %s\n", timestr);
-#endif
/* outer loop through fields */
while (*cp != '\0')
{
nf++;
if (nf > MAXDATEFIELDS)
return -1;
-#ifdef DATEDEBUG
- printf("ParseDateTime- set field[%d] to %s type %d\n", (nf - 1), field[nf - 1], ftype[nf - 1]);
-#endif
}
*numfields = nf;
for (i = 0; i < nf; i++)
{
-#ifdef DATEDEBUG
- printf("DecodeDateTime- field[%d] is %s (type %d)\n", i, field[i], ftype[i]);
-#endif
switch (ftype[i])
{
case DTK_DATE:
case DTK_STRING:
case DTK_SPECIAL:
type = DecodeSpecial(i, field[i], &val);
-#ifdef DATEDEBUG
- printf("DecodeDateTime- special field[%d] %s type=%d value=%d\n", i, field[i], type, val);
-#endif
if (type == IGNORE)
continue;
switch (type)
{
case RESERV:
-#ifdef DATEDEBUG
- printf("DecodeDateTime- RESERV field %s value is %d\n", field[i], val);
-#endif
switch (val)
{
case DTK_NOW:
break;
case MONTH:
-#ifdef DATEDEBUG
- printf("DecodeDateTime- month field %s value is %d\n", field[i], val);
-#endif
-
/*
* already have a (numeric) month? then see if we
* can substitute...
{
tm->tm_mday = tm->tm_mon;
tmask = DTK_M(DAY);
-#ifdef DATEDEBUG
- printf("DecodeNumber- misidentified month previously; assign as day %d\n", tm->tm_mday);
-#endif
}
haveTextMonth = TRUE;
tm->tm_mon = val;
return -1;
}
-#ifdef DATEDEBUG
- printf("DecodeDateTime- field[%d] %s (%08x/%08x) value is %d\n",
- i, field[i], fmask, tmask, val);
-#endif
-
if (tmask & fmask)
return -1;
fmask |= tmask;
else if ((mer == PM) && (tm->tm_hour != 12))
tm->tm_hour += 12;
-#ifdef DATEDEBUG
- printf("DecodeDateTime- mask %08x (%08x)", fmask, DTK_DATE_M);
- printf(" set y%04d m%02d d%02d", tm->tm_year, tm->tm_mon, tm->tm_mday);
- printf(" %02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
-
/* do additional checking for full date specs... */
if (*dtype == DTK_DATE)
{
for (i = 0; i < nf; i++)
{
-#ifdef DATEDEBUG
- printf("DecodeTimeOnly- field[%d] is %s (type %d)\n", i, field[i], ftype[i]);
-#endif
switch (ftype[i])
{
case DTK_TIME:
case DTK_STRING:
case DTK_SPECIAL:
type = DecodeSpecial(i, field[i], &val);
-#ifdef DATEDEBUG
- printf("DecodeTimeOnly- special field[%d] %s type=%d value=%d\n", i, field[i], type, val);
-#endif
if (type == IGNORE)
continue;
switch (type)
{
case RESERV:
-#ifdef DATEDEBUG
- printf("DecodeTimeOnly- RESERV field %s value is %d\n", field[i], val);
-#endif
switch (val)
{
case DTK_NOW:
if (tmask & fmask)
return -1;
fmask |= tmask;
-
-#ifdef DATEDEBUG
- printf("DecodeTimeOnly- field[%d] %s value is %d\n", i, field[i], val);
-#endif
}
-#ifdef DATEDEBUG
- printf("DecodeTimeOnly- mask %08x (%08x)", fmask, DTK_TIME_M);
- printf(" %02d:%02d:%02d (%f)\n", tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec);
-#endif
-
if ((mer != HR24) && (tm->tm_hour > 12))
return -1;
if ((mer == AM) && (tm->tm_hour == 12))
switch (type)
{
case MONTH:
-#ifdef DATEDEBUG
- printf("DecodeDate- month field %s value is %d\n", field[i], val);
-#endif
tm->tm_mon = val;
break;
break;
default:
-#ifdef DATEDEBUG
- printf("DecodeDate- illegal field %s value is %d\n", field[i], val);
-#endif
return -1;
}
if (fmask & dmask)
return -1;
}
-#ifdef DATEDEBUG
- printf("DecodeNumber- %s is %d fmask=%08x tmask=%08x\n", str, val, fmask, *tmask);
-#endif
-
/* Special case day of year? */
if ((flen == 3) && (fmask & DTK_M(YEAR))
&& ((val >= 1) && (val <= 366)))
*/
else if (flen > 2)
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- match %d (%s) as year\n", val, str);
-#endif
*tmask = DTK_M(YEAR);
/* already have a year? then see if we can substitute... */
{
tm->tm_mday = tm->tm_year;
*tmask = DTK_M(DAY);
-#ifdef DATEDEBUG
- printf("DecodeNumber- misidentified year previously; assign as day %d\n", tm->tm_mday);
-#endif
}
tm->tm_year = val;
else if ((fmask & DTK_M(YEAR)) && (!(fmask & DTK_M(MONTH)))
&& ((val >= 1) && (val <= 12)))
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- match %d (%s) as month\n", val, str);
-#endif
*tmask = DTK_M(MONTH);
tm->tm_mon = val;
&& (!(fmask & DTK_M(YEAR)) && !(fmask & DTK_M(DAY)))
&& ((val >= 1) && (val <= 31)))
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- match %d (%s) as day\n", val, str);
-#endif
*tmask = DTK_M(DAY);
tm->tm_mday = val;
else if ((!(fmask & DTK_M(MONTH)))
&& ((val >= 1) && (val <= 12)))
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- (2) match %d (%s) as month\n", val, str);
-#endif
*tmask = DTK_M(MONTH);
tm->tm_mon = val;
else if ((!(fmask & DTK_M(DAY)))
&& ((val >= 1) && (val <= 31)))
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- (2) match %d (%s) as day\n", val, str);
-#endif
*tmask = DTK_M(DAY);
tm->tm_mday = val;
-
}
else if (!(fmask & DTK_M(YEAR)))
{
-#ifdef DATEDEBUG
- printf("DecodeNumber- (2) match %d (%s) as year\n", val, str);
-#endif
*tmask = DTK_M(YEAR);
tm->tm_year = val;
/* adjust ONLY if exactly two digits... */
*is2digits = (flen == 2);
-
}
else
return -1;
/* yyyymmdd? */
if (len == 8)
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is 8 character date fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
-
*tmask = DTK_DATE_M;
tm->tm_mday = atoi(str + 6);
}
else if (len == 6)
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is 6 characters fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
if (fmask & DTK_DATE_M)
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is time field fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
*tmask = DTK_TIME_M;
tm->tm_sec = atoi(str + 4);
*(str + 4) = '\0';
tm->tm_min = atoi(str + 2);
*(str + 2) = '\0';
tm->tm_hour = atoi(str + 0);
-
}
else
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is date field fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
*tmask = DTK_DATE_M;
tm->tm_mday = atoi(str + 4);
*(str + 4) = '\0';
}
else if ((len == 5) && !(fmask & DTK_DATE_M))
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is 5 characters fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
*tmask = DTK_DATE_M;
tm->tm_mday = atoi(str + 2);
*(str + 2) = '\0';
}
else if (strchr(str, '.') != NULL)
{
-#ifdef DATEDEBUG
- printf("DecodeNumberField- %s is time field fmask=%08x tmask=%08x\n", str, fmask, *tmask);
-#endif
*tmask = DTK_TIME_M;
tm->tm_sec = strtod((str + 4), &cp);
if (cp == (str + 4))
/* read through list backwards to pick up units before values */
for (i = nf - 1; i >= 0; i--)
{
-#ifdef DATEDEBUG
- printf("DecodeDateDelta- field[%d] is %s (type %d)\n", i, field[i], ftype[i]);
-#endif
switch (ftype[i])
{
case DTK_TIME:
case DTK_STRING:
case DTK_SPECIAL:
type = DecodeUnits(i, field[i], &val);
-#ifdef DATEDEBUG
- printf("DecodeDateDelta- special field[%d] %s type=%d value=%d\n", i, field[i], type, val);
-#endif
if (type == IGNORE)
continue;
switch (type)
{
case UNITS:
-#ifdef DATEDEBUG
- printf("DecodeDateDelta- UNITS field %s value is %d\n", field[i], val);
-#endif
type = val;
break;
return -1;
}
-#ifdef DATEDEBUG
- printf("DecodeDateDelta- (%08x/%08x) field[%d] %s value is %d\n",
- fmask, tmask, i, field[i], val);
-#endif
-
if (tmask & fmask)
return -1;
fmask |= tmask;
tm->tm_year = -(tm->tm_year);
}
-#ifdef DATEDEBUG
- printf("DecodeDateDelta- mask %08x (%08x)", fmask, DTK_DATE_M);
- printf(" set y%04d m%02d d%02d", tm->tm_year, tm->tm_mon, tm->tm_mday);
- printf(" %02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
-
/* ensure that at least one time field has been found */
return (fmask != 0) ? 0 : -1;
} /* DecodeDateDelta() */
if (DATETIME_IS_RESERVED(dt))
{
if (DATETIME_IS_INVALID(dt))
- {
strcpy(str, INVALID);
-
- }
else if (DATETIME_IS_NOBEGIN(dt))
- {
strcpy(str, EARLY);
-
- }
else if (DATETIME_IS_NOEND(dt))
- {
strcpy(str, LATE);
-
- }
else if (DATETIME_IS_CURRENT(dt))
- {
strcpy(str, DCURRENT);
-
- }
else if (DATETIME_IS_EPOCH(dt))
- {
strcpy(str, EPOCH);
-
- }
else
- {
-#ifdef DATEDEBUG
- printf("EncodeSpecialDateTime- unrecognized date\n");
-#endif
strcpy(str, INVALID);
- }
return TRUE;
}
break;
}
-#ifdef DATEDEBUG
- printf("EncodeDateOnly- date result is %s\n", str);
-#endif
-
return TRUE;
} /* EncodeDateOnly() */
sprintf(str, "%02d:%02d:", tm->tm_hour, tm->tm_min);
sprintf((str + 6), ((fsec != 0) ? "%05.2f" : "%02.0f"), sec);
-#ifdef DATEDEBUG
- printf("EncodeTimeOnly- time result is %s\n", str);
-#endif
-
return TRUE;
} /* EncodeTimeOnly() */
sec = (tm->tm_sec + fsec);
-#ifdef DATEDEBUG
-#ifdef USE_POSIX_TIME
-#if defined(HAVE_TM_ZONE)
- printf("EncodeDateTime- timezone is %s (%s); offset is %ld (%d); daylight is %d (%d)\n",
- *tzn, tm->tm_zone, (-tm->tm_gmtoff), CTimeZone, tm->tm_isdst, CDayLight);
-#elif defined(HAVE_INT_TIMEZONE)
- printf("EncodeDateTime- timezone is %s (%s); offset is %d (%d); daylight is %d (%d)\n",
- *tzn, tzname[0], *tzp, CTimeZone, tm->tm_isdst, CDayLight);
-#else
-#error USE_POSIX_TIME is defined but neither HAVE_TM_ZONE or HAVE_INT_TIMEZONE are defined
-#endif
-#else
- printf("EncodeDateTime- timezone is %s (%s); offset is %d; daylight is %d\n",
- *tzn, CTZName, CTimeZone, CDayLight);
-#endif
-#endif
-
switch (style)
{
/* compatible with ISO date formats */
case USE_POSTGRES_DATES:
default:
day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
-#ifdef DATEDEBUG
- printf("EncodeDateTime- day is %d\n", day);
-#endif
tm->tm_wday = j2day(day);
strncpy(str, days[tm->tm_wday], 3);
break;
}
-#ifdef DATEDEBUG
- printf("EncodeDateTime- date result is %s\n", str);
-#endif
-
return TRUE;
} /* EncodeDateTime() */
cp += strlen(cp);
}
-#ifdef DATEDEBUG
- printf("EncodeTimeSpan- result is %s\n", str);
-#endif
-
return 0;
} /* EncodeTimeSpan() */