nabstime.c

来自「PostgreSQL7.4.6 for Linux」· C语言 代码 · 共 1,745 行 · 第 1/3 页

C
1,745
字号
	AbsoluteTime t2 = interval->data[1];	if (interval->status != T_INTERVAL_VALID)		PG_RETURN_RELATIVETIME(INVALID_RELTIME);	if (AbsoluteTimeIsReal(t1) &&		AbsoluteTimeIsReal(t2))		PG_RETURN_RELATIVETIME(t2 - t1);	PG_RETURN_RELATIVETIME(INVALID_RELTIME);}/* *		timenow			- returns  time "now", internal format * *		Now AbsoluteTime is time since Jan 1 1970 -mer 7 Feb 1992 */Datumtimenow(PG_FUNCTION_ARGS){	time_t		sec;	if (time(&sec) < 0)		PG_RETURN_ABSOLUTETIME(INVALID_ABSTIME);	PG_RETURN_ABSOLUTETIME((AbsoluteTime) sec);}/* * reltime comparison routines */static intreltime_cmp_internal(RelativeTime a, RelativeTime b){	/*	 * 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.	 */	if (a == INVALID_RELTIME)	{		if (b == INVALID_RELTIME)			return 0;			/* INVALID = INVALID */		else			return 1;			/* INVALID > non-INVALID */	}	if (b == INVALID_RELTIME)		return -1;				/* non-INVALID < INVALID */	if (a > b)		return 1;	else if (a == b)		return 0;	else		return -1;}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 interval i1 is same as interval 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 intervals, 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 interval i is equal to *								reltime t *		tintervallenne	- returns true iff length of interval i is not equal *								to reltime t *		tintervallenlt	- returns true iff length of interval i is less than *								reltime t *		tintervallengt	- returns true iff length of interval i is greater *								than reltime t *		tintervallenle	- returns true iff length of interval i is less or *								equal than reltime t *		tintervallenge	- returns true iff length of interval 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 interval i1 contains interval 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 interval 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 interval 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 interval 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														 * *****************************************************************************//* *		istinterval		- returns 1, iff i_string is a valid interval descr. *								  0, iff i_string is NOT a valid interval desc. *								  2, iff any time is INVALID_ABSTIME * *		output parameter: *				i_start, i_end: interval 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 intistinterval(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 != '[')			return 0;			/* syntax error */		else			break;	}	p++;	/* skip leading blanks up to '"' */	while ((c = *p) != '\0')	{		if (IsSpace(c))			p++;		else if (c != '"')			return 0;			/* syntax error */		else			break;	}	p++;	if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)		return 0;				/* undefined range, handled like a syntax								 * err. */	/* search for the end of the first date and change it to a NULL */	p1 = p;	while ((c = *p1) != '\0')	{		if (c == '"')		{			*p1 = '\0';			break;		}		p1++;	}	/* get the first date */	*i_start = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,													CStringGetDatum(p)));	/* rechange NULL at the end of the first date to a '"' */	*p1 = '"';	p = ++p1;	/* skip blanks up to '"', beginning of second date */	while ((c = *p) != '\0')	{		if (IsSpace(c))			p++;		else if (c != '"')			return 0;			/* syntax error */		else			break;	}	p++;	/* search for the end of the second date and change it to a NULL */	p1 = p;	while ((c = *p1) != '\0')	{		if (c == '"')		{			*p1 = '\0';			break;		}		p1++;	}	/* get the second date */	*i_end = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,													CStringGetDatum(p)));	/* rechange NULL at the end of the first date to a '"' */	*p1 = '"';	p = ++p1;	/* skip blanks up to ']' */	while ((c = *p) != '\0')	{		if (IsSpace(c))			p++;		else if (c != ']')			return 0;			/* syntax error */		else			break;	}	p++;	c = *p;	if (c != '\0')		return 0;				/* syntax error */	/* it seems to be a valid interval */	return 1;}/***************************************************************************** * *****************************************************************************//* * 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;	struct timezone tpz;	char		templ[100];	char		buf[100];	text	   *result;	int			len;	time_t		tt;	gettimeofday(&tp, &tpz);	tt = (time_t) tp.tv_sec;	strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",			 localtime(&tt));	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 + =
减小字号Ctrl + -
显示快捷键?