|
| 1 | +#include<stdio.h> |
| 2 | +#include<string.h> |
| 3 | +#include<time.h> |
| 4 | +#include"postgres.h" |
| 5 | +#include"utils/builtins.h" |
| 6 | + |
| 7 | +int4 |
| 8 | +timestamp_in(char*timestamp_str) |
| 9 | +{ |
| 10 | +structtminput_time; |
| 11 | +int4result; |
| 12 | + |
| 13 | +memset(&input_time,0,sizeof(input_time)); |
| 14 | +if(sscanf(timestamp_str,"%d%*c%d%*c%d%*c%d%*c%d%*c%d", |
| 15 | +&input_time.tm_year,&input_time.tm_mon,&input_time.tm_mday, |
| 16 | +&input_time.tm_hour,&input_time.tm_min,&input_time.tm_sec)!=6) { |
| 17 | +elog(WARN,"timestamp_in: timestamp \"%s\" not of the form yyyy-mm-dd hh:mm:ss", |
| 18 | +timestamp_str); |
| 19 | + } |
| 20 | + |
| 21 | +/* range checking? bahahahaha.... */ |
| 22 | + |
| 23 | +input_time.tm_year-=1900; |
| 24 | +input_time.tm_mon-=1; |
| 25 | + |
| 26 | +/* use mktime(), but make this GMT, not local time */ |
| 27 | +result=mktime(&input_time); |
| 28 | +result-=timezone; |
| 29 | + |
| 30 | +returnresult; |
| 31 | +} |
| 32 | + |
| 33 | +char* |
| 34 | +timestamp_out(int4timestamp) |
| 35 | +{ |
| 36 | +char*result; |
| 37 | +structtm*time; |
| 38 | + |
| 39 | +time=gmtime((time_t*)×tamp); |
| 40 | +result=palloc(20); |
| 41 | +sprintf(result,"%04d-%02d-%02d %02d:%02d:%02d", |
| 42 | +time->tm_year+1900,time->tm_mon+1,time->tm_mday, |
| 43 | +time->tm_hour,time->tm_min,time->tm_sec); |
| 44 | + |
| 45 | +returnresult; |
| 46 | +} |
| 47 | + |
| 48 | +int4 |
| 49 | +now(void) |
| 50 | +{ |
| 51 | +structtmignore; |
| 52 | +time_tsec; |
| 53 | + |
| 54 | +/* we want the local time here. but 'timezone' doesn't get set */ |
| 55 | +/* until we do a mktime(). so do one. */ |
| 56 | +memset(&ignore,0,sizeof(ignore)); |
| 57 | +mktime(&ignore); |
| 58 | + |
| 59 | +time(&sec); |
| 60 | +sec-=timezone; |
| 61 | +return((int4)sec); |
| 62 | +} |
| 63 | + |
| 64 | +int4 |
| 65 | +timestampeq(int4t1,int4t2) |
| 66 | +{ |
| 67 | +returnt1==t2; |
| 68 | +} |
| 69 | + |
| 70 | +int4 |
| 71 | +timestampne(int4t1,int4t2) |
| 72 | +{ |
| 73 | +returnt1!=t2; |
| 74 | +} |
| 75 | + |
| 76 | +int4 |
| 77 | +timestamplt(int4t1,int4t2) |
| 78 | +{ |
| 79 | +returnt1<t2; |
| 80 | +} |
| 81 | + |
| 82 | +int4 |
| 83 | +timestampgt(int4t1,int4t2) |
| 84 | +{ |
| 85 | +returnt1>t2; |
| 86 | +} |
| 87 | + |
| 88 | +int4 |
| 89 | +timestample(int4t1,int4t2) |
| 90 | +{ |
| 91 | +returnt1 <=t2; |
| 92 | +} |
| 93 | + |
| 94 | +int4 |
| 95 | +timestampge(int4t1,int4t2) |
| 96 | +{ |
| 97 | +returnt1 >=t2; |
| 98 | +} |