Add conversion from datetime to time data type.
Rename date+time conversion to datetime to ensure less than 16 characters in routine name (required to fit in pg_proc table).
This commit is contained in:
parent
0d6facbad6
commit
9f99e4c6b6
@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.16 1997/09/08 21:48:22 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.17 1997/10/25 05:16:09 thomas Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -123,25 +123,11 @@ date_out(DateADT date)
|
||||
*tm = &tt;
|
||||
char buf[MAXDATELEN + 1];
|
||||
|
||||
#if FALSE
|
||||
int year,
|
||||
month,
|
||||
day;
|
||||
|
||||
#endif
|
||||
|
||||
j2date((date + date2j(2000, 1, 1)),
|
||||
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
||||
|
||||
EncodeDateOnly(tm, DateStyle, buf);
|
||||
|
||||
#if FALSE
|
||||
if (EuroDates == 1) /* Output European-format dates */
|
||||
sprintf(buf, "%02d-%02d-%04d", day, month, year);
|
||||
else
|
||||
sprintf(buf, "%02d-%02d-%04d", month, day, year);
|
||||
#endif
|
||||
|
||||
result = PALLOC(strlen(buf) + 1);
|
||||
|
||||
strcpy(result, buf);
|
||||
@ -211,7 +197,8 @@ date_smaller(DateADT dateVal1, DateADT dateVal2)
|
||||
return (date_lt(dateVal1, dateVal2) ? dateVal1 : dateVal2);
|
||||
} /* date_smaller() */
|
||||
|
||||
/* Compute difference between two dates in days. */
|
||||
/* Compute difference between two dates in days.
|
||||
*/
|
||||
int4
|
||||
date_mi(DateADT dateVal1, DateADT dateVal2)
|
||||
{
|
||||
@ -219,14 +206,16 @@ date_mi(DateADT dateVal1, DateADT dateVal2)
|
||||
} /* date_mi() */
|
||||
|
||||
/* Add a number of days to a date, giving a new date.
|
||||
Must handle both positive and negative numbers of days. */
|
||||
* Must handle both positive and negative numbers of days.
|
||||
*/
|
||||
DateADT
|
||||
date_pli(DateADT dateVal, int4 days)
|
||||
{
|
||||
return (dateVal + days);
|
||||
} /* date_pli() */
|
||||
|
||||
/* Subtract a number of days from a date, giving a new date. */
|
||||
/* Subtract a number of days from a date, giving a new date.
|
||||
*/
|
||||
DateADT
|
||||
date_mii(DateADT dateVal, int4 days)
|
||||
{
|
||||
@ -388,11 +377,6 @@ date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn)
|
||||
tm->tm_year = tx->tm_year + 1900;
|
||||
tm->tm_mon = tx->tm_mon + 1;
|
||||
tm->tm_mday = tx->tm_mday;
|
||||
#if FALSE
|
||||
tm->tm_hour = tx->tm_hour;
|
||||
tm->tm_min = tx->tm_min;
|
||||
tm->tm_sec = tx->tm_sec;
|
||||
#endif
|
||||
tm->tm_isdst = tx->tm_isdst;
|
||||
|
||||
#ifdef HAVE_INT_TIMEZONE
|
||||
@ -420,13 +404,6 @@ date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn)
|
||||
}
|
||||
else
|
||||
{
|
||||
#if FALSE
|
||||
j2date((dateVal + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
|
||||
tm->tm_hour = 0;
|
||||
tm->tm_min = 0;
|
||||
tm->tm_sec = 0;
|
||||
#endif
|
||||
|
||||
#ifdef DATEDEBUG
|
||||
printf("date2tm- convert %d-%d-%d %d:%d%d to datetime\n",
|
||||
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
@ -491,12 +468,6 @@ time_out(TimeADT *time)
|
||||
struct tm tt,
|
||||
*tm = &tt;
|
||||
|
||||
#if FALSE
|
||||
int hour,
|
||||
min,
|
||||
sec;
|
||||
|
||||
#endif
|
||||
double fsec;
|
||||
char buf[MAXDATELEN + 1];
|
||||
|
||||
@ -511,25 +482,6 @@ time_out(TimeADT *time)
|
||||
|
||||
EncodeTimeOnly(tm, fsec, DateStyle, buf);
|
||||
|
||||
#if FALSE
|
||||
if (sec == 0.0)
|
||||
{
|
||||
sprintf(buf, "%02d:%02d", hour, min);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fsec == 0)
|
||||
{
|
||||
sprintf(buf, "%02d:%02d:%02d", hour, min, sec);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buf, "%02d:%02d:%05.2f", hour, min, (sec + fsec));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
result = PALLOC(strlen(buf) + 1);
|
||||
|
||||
strcpy(result, buf);
|
||||
@ -599,11 +551,56 @@ time_cmp(TimeADT *time1, TimeADT *time2)
|
||||
} /* time_cmp() */
|
||||
|
||||
|
||||
/* datetime_datetime()
|
||||
/* datetime_time()
|
||||
* Convert datetime to time data type.
|
||||
*/
|
||||
TimeADT *
|
||||
datetime_time(DateTime *datetime)
|
||||
{
|
||||
TimeADT *result;
|
||||
struct tm tt,
|
||||
*tm = &tt;
|
||||
int tz;
|
||||
double fsec;
|
||||
char *tzn;
|
||||
|
||||
if (!PointerIsValid(datetime))
|
||||
elog(WARN, "Unable to convert null datetime to date", NULL);
|
||||
|
||||
if (DATETIME_NOT_FINITE(*datetime))
|
||||
elog(WARN, "Unable to convert datetime to date", NULL);
|
||||
|
||||
if (DATETIME_IS_EPOCH(*datetime))
|
||||
{
|
||||
datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
|
||||
|
||||
}
|
||||
else if (DATETIME_IS_CURRENT(*datetime))
|
||||
{
|
||||
datetime2tm(SetDateTime(*datetime), &tz, tm, &fsec, &tzn);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (datetime2tm(*datetime, &tz, tm, &fsec, &tzn) != 0)
|
||||
elog(WARN, "Unable to convert datetime to date", NULL);
|
||||
}
|
||||
|
||||
result = PALLOCTYPE(TimeADT);
|
||||
|
||||
*result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
|
||||
|
||||
return (result);
|
||||
} /* datetime_time() */
|
||||
|
||||
|
||||
/* datet_datetime()
|
||||
* Convert date and time to datetime data type.
|
||||
* Should be called datetime_datetime()
|
||||
* but need <= 16 characters for function names.
|
||||
*/
|
||||
DateTime *
|
||||
datetime_datetime(DateADT date, TimeADT *time)
|
||||
datet_datetime(DateADT date, TimeADT *time)
|
||||
{
|
||||
DateTime *result;
|
||||
|
||||
@ -611,16 +608,13 @@ datetime_datetime(DateADT date, TimeADT *time)
|
||||
{
|
||||
result = PALLOCTYPE(DateTime);
|
||||
DATETIME_INVALID(*result);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
result = date_datetime(date);
|
||||
*result += *time;
|
||||
}
|
||||
|
||||
return (result);
|
||||
} /* datetime_datetime() */
|
||||
} /* datet_datetime() */
|
||||
|
||||
|
||||
int32 /* RelativeTime */
|
||||
|
Loading…
x
Reference in New Issue
Block a user