55#include "postgres.h"
66#include "miscadmin.h"
77#include "utils/builtins.h"
8+ #include "access/xact.h"
89
9- /* sunos doesn't have this function */
10- #if defined(sunos4 )
11- #define difftime (time1 ,time0 ) ((time1) - (time0))
12- #endif
13-
14- #if FALSE
15- /* copy the next part of the string into a buffer */
16- static const char *
17- cpstr (const char * s ,char * buf )
18- {
19- char in = 0 ;
20-
21- while (isspace (* s ))
22- s ++ ;
23-
24- for (;* s && !isspace (* s );s ++ )
25- {
26- if (strchr ("-,:/" ,* s ))
27- {
28- buf [in ]= 0 ;
29- return (s + 1 );
30- }
31-
32- if (in < 16 )
33- buf [in ++ ]= tolower (* s );
34- }
35-
36- buf [in ]= 0 ;
37- return s ;
38- }
39-
40- #endif
41-
42- /* assumes dd/mm/yyyy unless first item is month in word form */
4310time_t
4411timestamp_in (const char * timestamp_str )
4512{
4613int4 result ;
4714
48- #if FALSE
49- struct tm input_time ;
50- char buf [18 ];
51- const char * p ;
52- static const char * mstr []= {
53- "january" ,"february" ,"march" ,"april" ,"may" ,"june" ,
54- "july" ,"august" ,"september" ,"october" ,"november" ,"december"
55- };
56-
57- memset (& input_time ,0 ,sizeof (input_time ));
58- p = cpstr (timestamp_str ,buf );
59- if (isdigit (buf [0 ]))/* must be dd/mm/yyyy */
60- {
61- input_time .tm_mday = atoi (buf );
62- p = cpstr (p ,buf );
63- if (!buf [0 ])
64- elog (WARN ,"timestamp_in: timestamp \"%s\" not a proper date" ,
65- timestamp_str );
66- if (isdigit (buf [0 ]))
67- {
68- input_time .tm_mon = atoi (buf )- 1 ;
69- if (input_time .tm_mon < 0 || input_time .tm_mon > 11 )
70- elog (WARN ,"timestamp_in: timestamp \"%s\" invalid month" ,
71- timestamp_str );
72- }
73- else
74- {
75- int i ;
76-
77- for (i = 0 ;i < 12 ;i ++ )
78- if (strncmp (mstr [i ],buf ,strlen (buf ))== 0 )
79- break ;
80- if (1 > 11 )
81- elog (WARN ,"timestamp_in: timestamp \"%s\" invalid month" ,
82- timestamp_str );
83- input_time .tm_mon = i ;
84- }
85- }
86- else
87- /* must be month/dd/yyyy */
88- {
89- int i ;
90-
91- for (i = 0 ;i < 12 ;i ++ )
92- if (strncmp (mstr [i ],buf ,strlen (buf ))== 0 )
93- break ;
94- if (1 > 11 )
95- elog (WARN ,"timestamp_in: timestamp \"%s\" invalid month" ,
96- timestamp_str );
97- input_time .tm_mon = i ;
98- p = cpstr (p ,buf );
99- input_time .tm_mday = atoi (buf );
100- if (!input_time .tm_mday || input_time .tm_mday > 31 )
101- elog (WARN ,"timestamp_in: timestamp \"%s\" not a proper date" ,
102- timestamp_str );
103- }
104-
105- p = cpstr (p ,buf );
106- if (!buf [0 ]|| !isdigit (buf [0 ]))
107- elog (WARN ,"timestamp_in: timestamp \"%s\" not a proper date" ,
108- timestamp_str );
109- if ((input_time .tm_year = atoi (buf ))< 1900 )
110- input_time .tm_year += 1900 ;
111-
112- /* now get the time */
113- p = cpstr (p ,buf );
114- input_time .tm_hour = atoi (buf );
115- p = cpstr (p ,buf );
116- input_time .tm_min = atoi (buf );
117- p = cpstr (p ,buf );
118- input_time .tm_sec = atoi (buf );
119-
120- /* use mktime(), but make this GMT, not local time */
121- result = mktime (& input_time );
122- #endif
123-
12415result = nabstimein ((char * )timestamp_str );
12516
12617return result ;
12718}
12819
129- char *
20+ char *
13021timestamp_out (time_t timestamp )
13122{
13223char * result ;
@@ -138,13 +29,6 @@ timestamp_out(time_t timestamp)
13829char zone [MAXDATELEN + 1 ],
13930* tzn = zone ;
14031
141- #if FALSE
142- time = localtime (& timestamp );
143-
144- sprintf (result ,"%04d-%02d-%02d %02d:%02d:%02d" ,
145- time -> tm_year + 1900 ,time -> tm_mon + 1 ,time -> tm_mday ,
146- time -> tm_hour ,time -> tm_min ,time -> tm_sec );
147- #endif
14832abstime2tm (timestamp ,& tz ,tm ,tzn );
14933EncodeDateTime (tm ,fsec ,& tz ,& tzn ,USE_ISO_DATES ,buf );
15034
@@ -158,44 +42,62 @@ now(void)
15842{
15943time_t sec ;
16044
161- time ( & sec );
45+ sec = GetCurrentTransactionStartTime ( );
16246return (sec );
16347}
16448
16549bool
16650timestampeq (time_t t1 ,time_t t2 )
16751{
168- return difftime (t1 ,t2 )== 0 ;
52+ #if FALSE
53+ return (t1 == t2 );
54+ #endif
55+ return (abstimeeq (t1 ,t2 ));
16956}
17057
17158bool
17259timestampne (time_t t1 ,time_t t2 )
17360{
174- return difftime (t1 ,t2 )!= 0 ;
61+ #if FALSE
62+ return (t1 != t2 );
63+ #endif
64+ return (abstimene (t1 ,t2 ));
17565}
17666
17767bool
17868timestamplt (time_t t1 ,time_t t2 )
17969{
180- return difftime (t1 ,t2 )> 0 ;
70+ #if FALSE
71+ return (t1 > t2 );
72+ #endif
73+ return (abstimelt (t1 ,t2 ));
18174}
18275
18376bool
18477timestampgt (time_t t1 ,time_t t2 )
18578{
186- return difftime (t1 ,t2 )< 0 ;
79+ #if FALSE
80+ return (t1 < t2 );
81+ #endif
82+ return (abstimegt (t1 ,t2 ));
18783}
18884
18985bool
19086timestample (time_t t1 ,time_t t2 )
19187{
192- return difftime (t1 ,t2 ) >=0 ;
88+ #if FALSE
89+ return (t1 >=t2 );
90+ #endif
91+ return (abstimele (t1 ,t2 ));
19392}
19493
19594bool
19695timestampge (time_t t1 ,time_t t2 )
19796{
198- return difftime (t1 ,t2 ) <=0 ;
97+ #if FALSE
98+ return (t1 <=t2 );
99+ #endif
100+ return (abstimege (t1 ,t2 ));
199101}
200102
201103DateTime *
@@ -217,4 +119,4 @@ timestamp_datetime(time_t timestamp)
217119elog (WARN ,"Unable to convert timestamp to datetime" ,timestamp_out (timestamp ));
218120
219121return (result );
220- }/* timestamp_datetime() */
122+ }/* timestamp_datetime() */