Datum reltimene(PG_FUNCTION_ARGS) { RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) != 0); }
Datum btreltimecmp(PG_FUNCTION_ARGS) { RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_INT32(reltime_cmp_internal(t1, t2)); }
/* * reltimesend - converts reltime to binary format */ Datum reltimesend(PG_FUNCTION_ARGS) { RelativeTime time = PG_GETARG_RELATIVETIME(0); StringInfoData buf; pq_begintypsend(&buf); pq_sendint(&buf, time, sizeof(time)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); }
Datum tintervallenge(PG_FUNCTION_ARGS) { TimeInterval i = PG_GETARG_TIMEINTERVAL(0); RelativeTime t = PG_GETARG_RELATIVETIME(1); RelativeTime rt; if (i->status == T_INTERVAL_INVAL || t == INVALID_RELTIME) PG_RETURN_BOOL(false); rt = DatumGetRelativeTime(DirectFunctionCall1(tintervalrel, TimeIntervalGetDatum(i))); PG_RETURN_BOOL(rt != INVALID_RELTIME && rt >= t); }
/* * timemi - returns the value of (abstime t1 - reltime t2) */ Datum timemi(PG_FUNCTION_ARGS) { AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); if (AbsoluteTimeIsReal(t1) && RelativeTimeIsValid(t2) && ((t2 > 0 && t1 > NOSTART_ABSTIME + t2) || (t2 <= 0 && t1 < NOEND_ABSTIME + t2))) /* prevent overflow */ PG_RETURN_ABSOLUTETIME(t1 - t2); PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME); }
/* * reltimeout - converts the internal format to a reltime string */ Datum reltimeout(PG_FUNCTION_ARGS) { RelativeTime time = PG_GETARG_RELATIVETIME(0); char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; reltime2tm(time, tm); EncodeInterval(tm, 0, IntervalStyle, buf); result = pstrdup(buf); PG_RETURN_CSTRING(result); }
Datum reltime_interval(PG_FUNCTION_ARGS) { RelativeTime reltime = PG_GETARG_RELATIVETIME(0); Interval *result; int year, month, day; result = (Interval *) palloc(sizeof(Interval)); switch (reltime) { case INVALID_RELTIME: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot convert reltime \"invalid\" to interval"), errOmitLocation(true))); result->time = 0; result->day = 0; result->month = 0; break; default: #ifdef HAVE_INT64_TIMESTAMP year = reltime / SECS_PER_YEAR; reltime -= year * SECS_PER_YEAR; month = reltime / (DAYS_PER_MONTH * SECS_PER_DAY); reltime -= month * (DAYS_PER_MONTH * SECS_PER_DAY); day = reltime / SECS_PER_DAY; reltime -= day * SECS_PER_DAY; result->time = (reltime * USECS_PER_SEC); #else TMODULO(reltime, year, SECS_PER_YEAR); TMODULO(reltime, month, DAYS_PER_MONTH * SECS_PER_DAY); TMODULO(reltime, day, SECS_PER_DAY); result->time = reltime; #endif result->month = MONTHS_PER_YEAR * year + month; result->day = day; break; } PG_RETURN_INTERVAL_P(result); }