/* timestamptz_abstime() * Convert timestamp with time zone to abstime. */ Datum timestamptz_abstime(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMP(0); AbsoluteTime result; fsec_t fsec; struct pg_tm tt, *tm = &tt; if (TIMESTAMP_IS_NOBEGIN(timestamp)) result = NOSTART_ABSTIME; else if (TIMESTAMP_IS_NOEND(timestamp)) result = NOEND_ABSTIME; else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0) result = tm2abstime(tm, 0); else { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); result = INVALID_ABSTIME; } PG_RETURN_ABSOLUTETIME(result); }
/* timestamp_abstime() * Convert timestamp to abstime. */ Datum timestamp_abstime(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); AbsoluteTime result; fsec_t fsec; int tz; struct pg_tm tt, *tm = &tt; if (TIMESTAMP_IS_NOBEGIN(timestamp)) result = NOSTART_ABSTIME; else if (TIMESTAMP_IS_NOEND(timestamp)) result = NOEND_ABSTIME; else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0) { tz = DetermineTimeZoneOffset(tm, session_timezone); result = tm2abstime(tm, tz); } else { ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"), errOmitLocation(true))); result = INVALID_ABSTIME; } PG_RETURN_ABSOLUTETIME(result); }
/* abstimein() * Decode date/time string and return abstime. */ Datum abstimein(PG_FUNCTION_ARGS) { char *str = PG_GETARG_CSTRING(0); AbsoluteTime result; fsec_t fsec; int tz = 0; struct pg_tm date, *tm = &date; int dterr; char *field[MAXDATEFIELDS]; char workbuf[MAXDATELEN + 1]; int dtype; int nf, ftype[MAXDATEFIELDS]; dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field, ftype, MAXDATEFIELDS, &nf); if (dterr == 0) dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz); if (dterr != 0) DateTimeParseError(dterr, str, "abstime"); switch (dtype) { case DTK_DATE: result = tm2abstime(tm, tz); break; case DTK_EPOCH: /* * Don't bother retaining this as a reserved value, but instead * just set to the actual epoch time (1970-01-01) */ result = 0; break; case DTK_LATE: result = NOEND_ABSTIME; break; case DTK_EARLY: result = NOSTART_ABSTIME; break; case DTK_INVALID: result = INVALID_ABSTIME; break; default: elog(ERROR, "unexpected dtype %d while parsing abstime \"%s\"", dtype, str); result = INVALID_ABSTIME; break; }; PG_RETURN_ABSOLUTETIME(result); }