1997-03-14 06:58:13 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
time_t
|
|
|
|
timestamp_in(const char *timestamp_str)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
|
|
|
struct tm input_time;
|
|
|
|
int4 result;
|
|
|
|
|
|
|
|
memset(&input_time, 0, sizeof(input_time));
|
|
|
|
if(sscanf(timestamp_str, "%d%*c%d%*c%d%*c%d%*c%d%*c%d",
|
|
|
|
&input_time.tm_year, &input_time.tm_mon, &input_time.tm_mday,
|
|
|
|
&input_time.tm_hour, &input_time.tm_min, &input_time.tm_sec) != 6) {
|
|
|
|
elog(WARN, "timestamp_in: timestamp \"%s\" not of the form yyyy-mm-dd hh:mm:ss",
|
|
|
|
timestamp_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* range checking? bahahahaha.... */
|
|
|
|
|
|
|
|
input_time.tm_year -= 1900;
|
|
|
|
input_time.tm_mon -= 1;
|
|
|
|
|
|
|
|
/* use mktime(), but make this GMT, not local time */
|
|
|
|
result = mktime(&input_time);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1997-03-25 10:25:33 +01:00
|
|
|
timestamp_out(time_t timestamp)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
struct tm *time;
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
time = localtime((time_t *)×tamp);
|
1997-03-14 06:58:13 +01:00
|
|
|
result = palloc(20);
|
|
|
|
sprintf(result, "%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
time->tm_year+1900, time->tm_mon+1, time->tm_mday,
|
|
|
|
time->tm_hour, time->tm_min, time->tm_sec);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
time_t
|
1997-03-14 06:58:13 +01:00
|
|
|
now(void)
|
|
|
|
{
|
|
|
|
time_t sec;
|
|
|
|
|
|
|
|
time(&sec);
|
1997-03-25 10:25:33 +01:00
|
|
|
return(sec);
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestampeq(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) == 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestampne(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) != 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestamplt(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) > 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestampgt(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) < 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestample(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) >= 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|
|
|
|
|
1997-03-25 10:25:33 +01:00
|
|
|
bool
|
|
|
|
timestampge(time_t t1, time_t t2)
|
1997-03-14 06:58:13 +01:00
|
|
|
{
|
1997-03-25 10:25:33 +01:00
|
|
|
return difftime(t1, t2) <= 0;
|
1997-03-14 06:58:13 +01:00
|
|
|
}
|