📄 nabstime.c
字号:
Datumreltimeeq(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) == 0);}Datumreltimene(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) != 0);}Datumreltimelt(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) < 0);}Datumreltimegt(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) > 0);}Datumreltimele(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) <= 0);}Datumreltimege(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_BOOL(reltime_cmp_internal(t1, t2) >= 0);}Datumbtreltimecmp(PG_FUNCTION_ARGS){ RelativeTime t1 = PG_GETARG_RELATIVETIME(0); RelativeTime t2 = PG_GETARG_RELATIVETIME(1); PG_RETURN_INT32(reltime_cmp_internal(t1, t2));}/* * tintervalsame - returns true iff tinterval i1 is same as tinterval i2 * Check begin and end time. */Datumtintervalsame(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL) PG_RETURN_BOOL(false); if (DatumGetBool(DirectFunctionCall2(abstimeeq, AbsoluteTimeGetDatum(i1->data[0]), AbsoluteTimeGetDatum(i2->data[0]))) && DatumGetBool(DirectFunctionCall2(abstimeeq, AbsoluteTimeGetDatum(i1->data[1]), AbsoluteTimeGetDatum(i2->data[1])))) PG_RETURN_BOOL(true); PG_RETURN_BOOL(false);}/* * tinterval comparison routines * * Note: comparison is based on the lengths of the tintervals, not on * endpoint value. This is pretty bogus, but since it's only a legacy * datatype I'm not going to propose changing it. */static inttinterval_cmp_internal(TimeInterval a, TimeInterval b){ bool a_invalid; bool b_invalid; AbsoluteTime a_len; AbsoluteTime b_len; /* * We consider all INVALIDs to be equal and larger than any non-INVALID. * This is somewhat arbitrary; the important thing is to have a consistent * sort order. */ a_invalid = a->status == T_INTERVAL_INVAL || a->data[0] == INVALID_ABSTIME || a->data[1] == INVALID_ABSTIME; b_invalid = b->status == T_INTERVAL_INVAL || b->data[0] == INVALID_ABSTIME || b->data[1] == INVALID_ABSTIME; if (a_invalid) { if (b_invalid) return 0; /* INVALID = INVALID */ else return 1; /* INVALID > non-INVALID */ } if (b_invalid) return -1; /* non-INVALID < INVALID */ a_len = a->data[1] - a->data[0]; b_len = b->data[1] - b->data[0]; if (a_len > b_len) return 1; else if (a_len == b_len) return 0; else return -1;}Datumtintervaleq(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) == 0);}Datumtintervalne(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) != 0);}Datumtintervallt(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) < 0);}Datumtintervalle(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) <= 0);}Datumtintervalgt(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) > 0);}Datumtintervalge(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_BOOL(tinterval_cmp_internal(i1, i2) >= 0);}Datumbttintervalcmp(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); PG_RETURN_INT32(tinterval_cmp_internal(i1, i2));}/* * tintervalleneq - returns true iff length of tinterval i is equal to * reltime t * tintervallenne - returns true iff length of tinterval i is not equal * to reltime t * tintervallenlt - returns true iff length of tinterval i is less than * reltime t * tintervallengt - returns true iff length of tinterval i is greater * than reltime t * tintervallenle - returns true iff length of tinterval i is less or * equal than reltime t * tintervallenge - returns true iff length of tinterval i is greater or * equal than reltime t */Datumtintervalleneq(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);}Datumtintervallenne(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);}Datumtintervallenlt(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);}Datumtintervallengt(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);}Datumtintervallenle(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);}Datumtintervallenge(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);}/* * tintervalct - returns true iff tinterval i1 contains tinterval i2 */Datumtintervalct(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL) PG_RETURN_BOOL(false); if (DatumGetBool(DirectFunctionCall2(abstimele, AbsoluteTimeGetDatum(i1->data[0]), AbsoluteTimeGetDatum(i2->data[0]))) && DatumGetBool(DirectFunctionCall2(abstimege, AbsoluteTimeGetDatum(i1->data[1]), AbsoluteTimeGetDatum(i2->data[1])))) PG_RETURN_BOOL(true); PG_RETURN_BOOL(false);}/* * tintervalov - returns true iff tinterval i1 (partially) overlaps i2 */Datumtintervalov(PG_FUNCTION_ARGS){ TimeInterval i1 = PG_GETARG_TIMEINTERVAL(0); TimeInterval i2 = PG_GETARG_TIMEINTERVAL(1); if (i1->status == T_INTERVAL_INVAL || i2->status == T_INTERVAL_INVAL) PG_RETURN_BOOL(false); if (DatumGetBool(DirectFunctionCall2(abstimelt, AbsoluteTimeGetDatum(i1->data[1]), AbsoluteTimeGetDatum(i2->data[0]))) || DatumGetBool(DirectFunctionCall2(abstimegt, AbsoluteTimeGetDatum(i1->data[0]), AbsoluteTimeGetDatum(i2->data[1])))) PG_RETURN_BOOL(false); PG_RETURN_BOOL(true);}/* * tintervalstart - returns the start of tinterval i */Datumtintervalstart(PG_FUNCTION_ARGS){ TimeInterval i = PG_GETARG_TIMEINTERVAL(0); if (i->status == T_INTERVAL_INVAL) PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME); PG_RETURN_ABSOLUTETIME(i->data[0]);}/* * tintervalend - returns the end of tinterval i */Datumtintervalend(PG_FUNCTION_ARGS){ TimeInterval i = PG_GETARG_TIMEINTERVAL(0); if (i->status == T_INTERVAL_INVAL) PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME); PG_RETURN_ABSOLUTETIME(i->data[1]);}/***************************************************************************** * PRIVATE ROUTINES * *****************************************************************************//* * parsetinterval -- parse a tinterval string * * output parameters: * i_start, i_end: tinterval margins * * Time interval: * `[' {` '} `'' <AbsTime> `'' {` '} `'' <AbsTime> `'' {` '} `]' * * OR `Undefined Range' (see also INVALID_INTERVAL_STR) * * where <AbsTime> satisfies the syntax of absolute time. * * e.g. [ ' Jan 18 1902' 'Jan 1 00:00:00 1970'] */static voidparsetinterval(char *i_string, AbsoluteTime *i_start, AbsoluteTime *i_end){ char *p, *p1; char c; p = i_string; /* skip leading blanks up to '[' */ while ((c = *p) != '\0') { if (IsSpace(c)) p++; else if (c != '[') goto bogus; /* syntax error */ else break; } if (c == '\0') goto bogus; /* syntax error */ p++; /* skip leading blanks up to '"' */ while ((c = *p) != '\0') { if (IsSpace(c)) p++; else if (c != '"') goto bogus; /* syntax error */ else break; } if (c == '\0') goto bogus; /* syntax error */ p++; if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0) goto bogus; /* undefined range, handled like a syntax err. */ /* search for the end of the first date and change it to a \0 */ p1 = p; while ((c = *p1) != '\0') { if (c == '"') break; p1++; } if (c == '\0') goto bogus; /* syntax error */ *p1 = '\0'; /* get the first date */ *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein, CStringGetDatum(p))); /* undo change to \0 */ *p1 = c; p = ++p1; /* skip blanks up to '"', beginning of second date */ while ((c = *p) != '\0') { if (IsSpace(c)) p++; else if (c != '"') goto bogus; /* syntax error */ else break; } if (c == '\0') goto bogus; /* syntax error */ p++; /* search for the end of the second date and change it to a \0 */ p1 = p; while ((c = *p1) != '\0') { if (c == '"') break; p1++; } if (c == '\0') goto bogus; /* syntax error */ *p1 = '\0'; /* get the second date */ *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein, CStringGetDatum(p))); /* undo change to \0 */ *p1 = c; p = ++p1; /* skip blanks up to ']' */ while ((c = *p) != '\0') { if (IsSpace(c)) p++; else if (c != ']') goto bogus; /* syntax error */ else break; } if (c == '\0') goto bogus; /* syntax error */ p++; c = *p; if (c != '\0') goto bogus; /* syntax error */ /* it seems to be a valid tinterval */ return;bogus: ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT), errmsg("invalid input syntax for type tinterval: \"%s\"", i_string))); *i_start = *i_end = INVALID_ABSTIME; /* keep compiler quiet */}/***************************************************************************** * *****************************************************************************//* * timeofday - * returns the current time as a text. similar to timenow() but returns * seconds with more precision (up to microsecs). (I need this to compare * the Wisconsin benchmark with Illustra whose TimeNow() shows current * time with precision up to microsecs.) - ay 3/95 */Datumtimeofday(PG_FUNCTION_ARGS){ struct timeval tp; char templ[128]; char buf[128]; text *result; int len; pg_time_t tt; gettimeofday(&tp, NULL); tt = (pg_time_t) tp.tv_sec; pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z", pg_localtime(&tt, global_timezone)); snprintf(buf, sizeof(buf), templ, tp.tv_usec); len = VARHDRSZ + strlen(buf); result = (text *) palloc(len); VARATT_SIZEP(result) = len; memcpy(VARDATA(result), buf, strlen(buf)); PG_RETURN_TEXT_P(result);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -