postgresql/src/backend/utils/adt/timestamp.c

125 lines
2.0 KiB
C
Raw Normal View History

1997-03-14 06:58:13 +01:00
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
1997-03-14 06:58:13 +01:00
#include "postgres.h"
#include "miscadmin.h"
1997-03-14 06:58:13 +01:00
#include "utils/builtins.h"
#include "access/xact.h"
1997-03-14 06:58:13 +01:00
time_t
timestamp_in(const char *timestamp_str)
1997-03-14 06:58:13 +01:00
{
int4 result;
result = nabstimein((char *) timestamp_str);
1997-03-14 06:58:13 +01:00
return result;
1997-03-14 06:58:13 +01:00
}
char *
timestamp_out(time_t timestamp)
1997-03-14 06:58:13 +01:00
{
char *result;
int tz;
double fsec = 0;
struct tm tt,
*tm = &tt;
char buf[MAXDATELEN + 1];
char zone[MAXDATELEN + 1],
*tzn = zone;
1997-03-14 06:58:13 +01:00
switch (timestamp)
{
case EPOCH_ABSTIME:
strcpy(buf, EPOCH);
break;
case INVALID_ABSTIME:
strcpy(buf, INVALID);
break;
case CURRENT_ABSTIME:
strcpy(buf, DCURRENT);
break;
case NOEND_ABSTIME:
strcpy(buf, LATE);
break;
case NOSTART_ABSTIME:
strcpy(buf, EARLY);
break;
default:
abstime2tm(timestamp, &tz, tm, tzn);
EncodeDateTime(tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);
break;
}
result = palloc(strlen(buf) + 1);
strcpy(result, buf);
return result;
} /* timestamp_out() */
1997-03-14 06:58:13 +01:00
time_t
1997-03-14 06:58:13 +01:00
now(void)
{
time_t sec;
1997-03-14 06:58:13 +01:00
sec = GetCurrentTransactionStartTime();
return (sec);
1997-03-14 06:58:13 +01:00
}
bool
timestampeq(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimeeq(t1,t2));
1997-03-14 06:58:13 +01:00
}
bool
timestampne(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimene(t1,t2));
1997-03-14 06:58:13 +01:00
}
bool
timestamplt(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimelt(t1,t2));
1997-03-14 06:58:13 +01:00
}
bool
timestampgt(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimegt(t1,t2));
1997-03-14 06:58:13 +01:00
}
bool
timestample(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimele(t1,t2));
1997-03-14 06:58:13 +01:00
}
bool
timestampge(time_t t1, time_t t2)
1997-03-14 06:58:13 +01:00
{
return(abstimege(t1,t2));
1997-03-14 06:58:13 +01:00
}
DateTime *
timestamp_datetime(time_t timestamp)
{
DateTime *result;
double fsec = 0;
struct tm *tm;
if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
elog(WARN, "Memory allocation failed, can't convert timestamp to datetime", NULL);
tm = localtime((time_t *) &timestamp);
tm->tm_year += 1900;
tm->tm_mon += 1;
if (tm2datetime(tm, fsec, NULL, result) != 0)
elog(WARN, "Unable to convert timestamp to datetime", timestamp_out(timestamp));
return (result);
} /* timestamp_datetime() */