Skip to content

Commit 2ab34df

Browse files
committed
From: Thomas Lockhart <Thomas.G.Lockhart@jpl.nasa.gov>
Subject: [HACKERS] More date time functions Here are some additional patches mostly related to the date and time data types. It includes some type conversion routines to move between the different date types and some other date manipulation routines such as date_part(units,datetime). I noticed Edmund Mergl et al's neat trick for getting function overloading for builtin functions, so started to use that for the date and time stuff. Later, if someone figures out how to get function overloading directly for internal C code, then we can move to that technique. These patches include documentation updates (don't faint!) for the built-in man page. Doesn't yet include mention of timestamp, since I don't know much about it and since it may change a bit to become a _real_ ANSI timestamp which would include parser support for the declaration syntax (what do you think, Dan?). The patches were developed on the 970330 release, but have been rebuilt off of the 970402 release. The first patch below is to get libpq to compile, on my Linux box, but is not related to the rest of the patches and you can choose not to apply that one at this time. Thanks in advance, scrappy!
1 parent 920c58d commit 2ab34df

File tree

12 files changed

+1006
-349
lines changed

12 files changed

+1006
-349
lines changed

src/backend/utils/adt/date.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.6 1997/03/14 23:19:57 scrappy Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.7 1997/04/02 18:33:09 scrappy Exp $
1313
*
1414
* NOTES
1515
* This code is actually (almost) unused.
@@ -32,15 +32,14 @@
3232

3333
#include "postgres.h"
3434
#include "miscadmin.h"
35+
#ifdef HAVE_LIMITS_H
36+
# include <limits.h>
37+
#endif
3538
#include "access/xact.h"
3639
#include "utils/builtins.h" /* where function declarations go */
3740
#include "utils/palloc.h"
3841
#include "utils/dt.h"
3942

40-
#ifndef USE_NEW_TIME_CODE
41-
#define USE_NEW_TIME_CODE 1
42-
#endif
43-
4443
#define INVALID_RELTIME_STR "Undefined RelTime"
4544
#define INVALID_RELTIME_STR_LEN (sizeof(INVALID_RELTIME_STR)-1)
4645
#define RELTIME_LABEL '@'
@@ -235,6 +234,55 @@ char *tintervalout(TimeInterval interval)
235234
* PUBLIC ROUTINES *
236235
*****************************************************************************/
237236

237+
RelativeTime
238+
timespan_reltime(TimeSpan *timespan)
239+
{
240+
RelativeTime time;
241+
double span;
242+
243+
if (!PointerIsValid(timespan))
244+
time = INVALID_RELTIME;
245+
246+
if (TIMESPAN_IS_INVALID(*timespan)) {
247+
time = INVALID_RELTIME;
248+
249+
} else {
250+
span = ((((double) 30*86400)*timespan->month) + timespan->time);
251+
252+
#ifdef DATEDEBUG
253+
printf( "timespan_reltime- convert m%d s%f to %f [%d %d]\n",
254+
timespan->month, timespan->time, span, INT_MIN, INT_MAX);
255+
#endif
256+
257+
time = (((span > INT_MIN) && (span < INT_MAX))? span: INVALID_RELTIME);
258+
};
259+
260+
return(time);
261+
} /* timespan_reltime() */
262+
263+
264+
TimeSpan *
265+
reltime_timespan(RelativeTime reltime)
266+
{
267+
TimeSpan *result;
268+
269+
if (!PointerIsValid(result = PALLOCTYPE(TimeSpan)))
270+
elog(WARN,"Memory allocation failed, can't convert reltime to timespan",NULL);
271+
272+
switch(reltime) {
273+
case INVALID_RELTIME:
274+
TIMESPAN_INVALID(*result);
275+
break;
276+
277+
default:
278+
result->time = reltime;
279+
result->month = 0;
280+
};
281+
282+
return(result);
283+
} /* reltime_timespan() */
284+
285+
238286
/*
239287
* mktinterval - creates a time interval with endpoints t1 and t2
240288
*/

0 commit comments

Comments
 (0)