in src/backend/utils/adt/datetime.c [3196:3606]
case INTERVAL_MASK(YEAR):
type = DTK_YEAR;
break;
case INTERVAL_MASK(MONTH):
case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
type = DTK_MONTH;
break;
case INTERVAL_MASK(DAY):
type = DTK_DAY;
break;
case INTERVAL_MASK(HOUR):
case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
type = DTK_HOUR;
break;
case INTERVAL_MASK(MINUTE):
case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
type = DTK_MINUTE;
break;
case INTERVAL_MASK(SECOND):
case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
type = DTK_SECOND;
break;
default:
type = DTK_SECOND;
break;
}
}
errno = 0;
val = strtoint(field[i], &cp, 10);
if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW;
if (*cp == '-')
{
/* SQL "years-months" syntax */
int val2;
val2 = strtoint(cp + 1, &cp, 10);
if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
return DTERR_FIELD_OVERFLOW;
if (*cp != '\0')
return DTERR_BAD_FORMAT;
type = DTK_MONTH;
if (*field[i] == '-')
val2 = -val2;
if (((double) val * MONTHS_PER_YEAR + val2) > INT_MAX ||
((double) val * MONTHS_PER_YEAR + val2) < INT_MIN)
return DTERR_FIELD_OVERFLOW;
val = val * MONTHS_PER_YEAR + val2;
fval = 0;
}
else if (*cp == '.')
{
errno = 0;
fval = strtod(cp, &cp);
if (*cp != '\0' || errno != 0)
return DTERR_BAD_FORMAT;
if (*field[i] == '-')
fval = -fval;
}
else if (*cp == '\0')
fval = 0;
else
return DTERR_BAD_FORMAT;
tmask = 0; /* DTK_M(type); */
switch (type)
{
case DTK_MICROSEC:
*fsec += rint(val + fval);
tmask = DTK_M(MICROSECOND);
break;
case DTK_MILLISEC:
/* avoid overflowing the fsec field */
tm->tm_sec += val / 1000;
val -= (val / 1000) * 1000;
*fsec += rint((val + fval) * 1000);
tmask = DTK_M(MILLISECOND);
break;
case DTK_SECOND:
tm->tm_sec += val;
*fsec += rint(fval * 1000000);
/*
* If any subseconds were specified, consider this
* microsecond and millisecond input as well.
*/
if (fval == 0)
tmask = DTK_M(SECOND);
else
tmask = DTK_ALL_SECS_M;
break;
case DTK_MINUTE:
tm->tm_min += val;
AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
tmask = DTK_M(MINUTE);
break;
case DTK_HOUR:
tm->tm_hour += val;
AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
tmask = DTK_M(HOUR);
type = DTK_DAY; /* set for next field */
break;
case DTK_DAY:
tm->tm_mday += val;
AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
tmask = DTK_M(DAY);
break;
case DTK_WEEK:
tm->tm_mday += val * 7;
AdjustFractDays(fval, tm, fsec, 7);
tmask = DTK_M(WEEK);
break;
case DTK_MONTH:
tm->tm_mon += val;
AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
tmask = DTK_M(MONTH);
break;
case DTK_YEAR:
tm->tm_year += val;
if (fval != 0)
tm->tm_mon += fval * MONTHS_PER_YEAR;
tmask = DTK_M(YEAR);
break;
case DTK_DECADE:
tm->tm_year += val * 10;
if (fval != 0)
tm->tm_mon += fval * MONTHS_PER_YEAR * 10;
tmask = DTK_M(DECADE);
break;
case DTK_CENTURY:
tm->tm_year += val * 100;
if (fval != 0)
tm->tm_mon += fval * MONTHS_PER_YEAR * 100;
tmask = DTK_M(CENTURY);
break;
case DTK_MILLENNIUM:
tm->tm_year += val * 1000;
if (fval != 0)
tm->tm_mon += fval * MONTHS_PER_YEAR * 1000;
tmask = DTK_M(MILLENNIUM);
break;
default:
return DTERR_BAD_FORMAT;
}
break;
case DTK_STRING:
case DTK_SPECIAL:
type = DecodeUnits(i, field[i], &val);
if (type == IGNORE_DTF)
continue;
tmask = 0; /* DTK_M(type); */
switch (type)
{
case UNITS:
type = val;
break;
case AGO:
is_before = true;
type = val;
break;
case RESERV:
tmask = (DTK_DATE_M | DTK_TIME_M);
*dtype = val;
break;
default:
return DTERR_BAD_FORMAT;
}
break;
default:
return DTERR_BAD_FORMAT;
}
if (tmask & fmask)
return DTERR_BAD_FORMAT;
fmask |= tmask;
}
/* ensure that at least one time field has been found */
if (fmask == 0)
return DTERR_BAD_FORMAT;
/* ensure fractional seconds are fractional */
if (*fsec != 0)
{
int sec;
sec = *fsec / USECS_PER_SEC;
*fsec -= sec * USECS_PER_SEC;
tm->tm_sec += sec;
}
/*----------
* The SQL standard defines the interval literal
* '-1 1:00:00'
* to mean "negative 1 days and negative 1 hours", while Postgres
* traditionally treats this as meaning "negative 1 days and positive
* 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
* to all fields if there are no other explicit signs.
*
* We leave the signs alone if there are additional explicit signs.
* This protects us against misinterpreting postgres-style dump output,
* since the postgres-style output code has always put an explicit sign on
* all fields following a negative field. But note that SQL-spec output
* is ambiguous and can be misinterpreted on load! (So it's best practice
* to dump in postgres style, not SQL style.)
*----------
*/
if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
{
/* Check for additional explicit signs */
bool more_signs = false;
for (i = 1; i < nf; i++)
{
if (*field[i] == '-' || *field[i] == '+')
{
more_signs = true;
break;
}
}
if (!more_signs)
{
/*
* Rather than re-determining which field was field[0], just force
* 'em all negative.
*/
if (*fsec > 0)
*fsec = -(*fsec);
if (tm->tm_sec > 0)
tm->tm_sec = -tm->tm_sec;
if (tm->tm_min > 0)
tm->tm_min = -tm->tm_min;
if (tm->tm_hour > 0)
tm->tm_hour = -tm->tm_hour;
if (tm->tm_mday > 0)
tm->tm_mday = -tm->tm_mday;
if (tm->tm_mon > 0)
tm->tm_mon = -tm->tm_mon;
if (tm->tm_year > 0)
tm->tm_year = -tm->tm_year;
}
}
/* finally, AGO negates everything */
if (is_before)
{
*fsec = -(*fsec);
tm->tm_sec = -tm->tm_sec;
tm->tm_min = -tm->tm_min;
tm->tm_hour = -tm->tm_hour;
tm->tm_mday = -tm->tm_mday;
tm->tm_mon = -tm->tm_mon;
tm->tm_year = -tm->tm_year;
}
return 0;
}
/*
* Helper functions to avoid duplicated code in DecodeISO8601Interval.
*
* Parse a decimal value and break it into integer and fractional parts.
* Returns 0 or DTERR code.
*/
static int
ParseISO8601Number(char *str, char **endptr, int *ipart, double *fpart)
{
double val;
if (!(isdigit((unsigned char) *str) || *str == '-' || *str == '.'))
return DTERR_BAD_FORMAT;
errno = 0;
val = strtod(str, endptr);
/* did we not see anything that looks like a double? */
if (*endptr == str || errno != 0)
return DTERR_BAD_FORMAT;
/* watch out for overflow */
if (val < INT_MIN || val > INT_MAX)
return DTERR_FIELD_OVERFLOW;
/* be very sure we truncate towards zero (cf dtrunc()) */
if (val >= 0)
*ipart = (int) floor(val);
else
*ipart = (int) -floor(-val);
*fpart = val - *ipart;
return 0;
}
/*
* Determine number of integral digits in a valid ISO 8601 number field
* (we should ignore sign and any fraction part)
*/
static int
ISO8601IntegerWidth(char *fieldstart)
{
/* We might have had a leading '-' */
if (*fieldstart == '-')
fieldstart++;
return strspn(fieldstart, "0123456789");
}
/* DecodeISO8601Interval()
* Decode an ISO 8601 time interval of the "format with designators"
* (section 4.4.3.2) or "alternative format" (section 4.4.3.3)
* Examples: P1D for 1 day
* PT1H for 1 hour
* P2Y6M7DT1H30M for 2 years, 6 months, 7 days 1 hour 30 min
* P0002-06-07T01:30:00 the same value in alternative format
*
* Returns 0 if successful, DTERR code if bogus input detected.
* Note: error code should be DTERR_BAD_FORMAT if input doesn't look like
* ISO8601, otherwise this could cause unexpected error messages.
* dtype, tm, fsec are output parameters.
*
* A couple exceptions from the spec:
* - a week field ('W') may coexist with other units
* - allows decimals in fields other than the least significant unit.
*/
int
DecodeISO8601Interval(char *str,
int *dtype, struct pg_tm *tm, fsec_t *fsec)
{
bool datepart = true;
bool havefield = false;
*dtype = DTK_DELTA;
ClearPgTm(tm, fsec);
if (strlen(str) < 2 || str[0] != 'P')
return DTERR_BAD_FORMAT;
str++;
while (*str)
{
char *fieldstart;
int val;
double fval;
char unit;
int dterr;
if (*str == 'T') /* T indicates the beginning of the time part */
{
datepart = false;
havefield = false;
str++;
continue;
}
fieldstart = str;
dterr = ParseISO8601Number(str, &str, &val, &fval);
if (dterr)
return dterr;
/*
* Note: we could step off the end of the string here. Code below
* *must* exit the loop if unit == '\0'.
*/
unit = *str++;
if (datepart)
{
switch (unit) /* before T: Y M W D */
{
case 'Y':
tm->tm_year += val;
tm->tm_mon += (fval * MONTHS_PER_YEAR);
break;
case 'M':
tm->tm_mon += val;
AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
break;
case 'W':
tm->tm_mday += val * 7;
AdjustFractDays(fval, tm, fsec, 7);
break;
case 'D':
tm->tm_mday += val;
AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
break;
case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
case '\0':
if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
{