11/*-------------------------------------------------------------------------
2+ *
23 * nabstime.c
34 * Utilities for the built-in type "AbsoluteTime".
45 * Functions for the built-in type "RelativeTime".
910 *
1011 *
1112 * IDENTIFICATION
12- * $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.103 2003/02/20 05:24:55 tgl Exp $
13- *
14- * NOTES
13+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.104 2003/02/22 05:57:45 tgl Exp $
1514 *
1615 *-------------------------------------------------------------------------
1716 */
2322#include <float.h>
2423#include <limits.h>
2524
26- #if !(defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE ))
27- #include <sys/timeb.h>
28- #endif
29-
3025#include "access/xact.h"
3126#include "miscadmin.h"
3227#include "utils/builtins.h"
@@ -88,196 +83,78 @@ static int istinterval(char *i_string,
8883
8984
9085/* GetCurrentAbsoluteTime()
91- * Get the current system time. Set timezone parameters if not specified elsewhere.
92- * Define HasCTZSet to allow clients to specify the default timezone.
86+ * Get the current system time.
9387 *
94- * Returns the number of seconds since epoch (January 1 1970 GMT)
88+ * Returns the number of seconds since epoch (January 1 1970 GMT).
9589 */
9690AbsoluteTime
9791GetCurrentAbsoluteTime (void )
9892{
9993time_t now ;
10094
101- #if defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE )
102- struct tm * tm ;
103-
10495now = time (NULL );
105- #else
106- struct timeb tb ;/* the old V7-ism */
107-
108- ftime (& tb );
109- now = tb .time ;
110- #endif
111-
112- if (!HasCTZSet )
113- {
114- #if defined(HAVE_TM_ZONE )
115- tm = localtime (& now );
116-
117- CTimeZone = - tm -> tm_gmtoff ;/* tm_gmtoff is Sun/DEC-ism */
118- CDayLight = (tm -> tm_isdst > 0 );
119-
120- #ifdef NOT_USED
121-
122- /*
123- * XXX is there a better way to get local timezone string w/o
124- * tzname? - tgl 97/03/18
125- */
126- strftime (CTZName ,MAXTZLEN ,"%Z" ,tm );
127- #endif
128-
129- /*
130- * XXX FreeBSD man pages indicate that this should work - thomas
131- * 1998-12-12
132- */
133- StrNCpy (CTZName ,tm -> tm_zone ,MAXTZLEN + 1 );
134-
135- #elif defined(HAVE_INT_TIMEZONE )
136- tm = localtime (& now );
137-
138- CDayLight = tm -> tm_isdst ;
139- CTimeZone = ((tm -> tm_isdst > 0 ) ? (TIMEZONE_GLOBAL - 3600 ) :TIMEZONE_GLOBAL );
140- StrNCpy (CTZName ,tzname [tm -> tm_isdst ],MAXTZLEN + 1 );
141- #else /* neither HAVE_TM_ZONE nor
142- * HAVE_INT_TIMEZONE */
143- CTimeZone = tb .timezone * 60 ;
144- CDayLight = (tb .dstflag != 0 );
145-
146- /*
147- * XXX does this work to get the local timezone string in V7? -
148- * tgl 97/03/18
149- */
150- strftime (CTZName ,MAXTZLEN ,"%Z" ,localtime (& now ));
151- #endif
152- }
153-
15496return (AbsoluteTime )now ;
155- }/* GetCurrentAbsoluteTime() */
97+ }
15698
15799
158100/* GetCurrentAbsoluteTimeUsec()
159- * Get the current system time. Set timezone parameters if not specified elsewhere.
160- * Define HasCTZSet to allow clients to specify the default timezone.
101+ * Get the current system time.
161102 *
162- * Returns the number of seconds since epoch (January 1 1970 GMT)
103+ * Returns the number of seconds since epoch (January 1 1970 GMT),
104+ * and returns fractional seconds (as # of microseconds) into *usec.
163105 */
164106AbsoluteTime
165107GetCurrentAbsoluteTimeUsec (int * usec )
166108{
167109time_t now ;
168110struct timeval tp ;
169111
170- #ifdef NOT_USED
171- struct timezone tpz ;
172- #endif
173- #if defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE )
174- struct tm * tm ;
175-
176- #else
177- struct timeb tb ;/* the old V7-ism */
178- #endif
179-
180112gettimeofday (& tp ,NULL );
181-
182113now = tp .tv_sec ;
183114* usec = tp .tv_usec ;
184-
185- #ifdef NOT_USED
186- #if defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE )
187- now = time (NULL );
188- #else
189- ftime (& tb );
190- now = tb .time ;
191- #endif
192- #endif
193-
194- if (!HasCTZSet )
195- {
196- #if defined(HAVE_TM_ZONE )
197- tm = localtime (& now );
198-
199- CTimeZone = - tm -> tm_gmtoff ;/* tm_gmtoff is Sun/DEC-ism */
200- CDayLight = (tm -> tm_isdst > 0 );
201-
202- #ifdef NOT_USED
203-
204- /*
205- * XXX is there a better way to get local timezone string w/o
206- * tzname? - tgl 97/03/18
207- */
208- strftime (CTZName ,MAXTZLEN ,"%Z" ,tm );
209- #endif
210-
211- /*
212- * XXX FreeBSD man pages indicate that this should work - thomas
213- * 1998-12-12
214- */
215- StrNCpy (CTZName ,tm -> tm_zone ,MAXTZLEN + 1 );
216-
217- #elif defined(HAVE_INT_TIMEZONE )
218- tm = localtime (& now );
219-
220- CDayLight = tm -> tm_isdst ;
221- CTimeZone = ((tm -> tm_isdst > 0 ) ? (TIMEZONE_GLOBAL - 3600 ) :TIMEZONE_GLOBAL );
222- StrNCpy (CTZName ,tzname [tm -> tm_isdst ],MAXTZLEN + 1 );
223- #else /* neither HAVE_TM_ZONE nor
224- * HAVE_INT_TIMEZONE */
225- CTimeZone = tb .timezone * 60 ;
226- CDayLight = (tb .dstflag != 0 );
227-
228- /*
229- * XXX does this work to get the local timezone string in V7? -
230- * tgl 97/03/18
231- */
232- strftime (CTZName ,MAXTZLEN ,"%Z" ,localtime (& now ));
233- #endif
234- };
235-
236115return (AbsoluteTime )now ;
237- }/* GetCurrentAbsoluteTimeUsec() */
116+ }
238117
239118
119+ /* GetCurrentDateTime()
120+ * Get the transaction start time ("now()") broken down as a struct tm.
121+ */
240122void
241123GetCurrentDateTime (struct tm * tm )
242124{
243125int tz ;
244126
245127abstime2tm (GetCurrentTransactionStartTime (),& tz ,tm ,NULL );
246- }/* GetCurrentDateTime() */
247-
128+ }
248129
130+ /* GetCurrentTimeUsec()
131+ * Get the transaction start time ("now()") broken down as a struct tm,
132+ * plus fractional-second and timezone info.
133+ */
249134void
250135GetCurrentTimeUsec (struct tm * tm ,fsec_t * fsec ,int * tzp )
251136{
252137int tz ;
253138int usec ;
254139
255140abstime2tm (GetCurrentTransactionStartTimeUsec (& usec ),& tz ,tm ,NULL );
256- /* Note: don't pass NULL tzpdirectly to abstime2tm */
141+ /* Note: don't pass NULL tzp to abstime2tm; affects behavior */
257142if (tzp != NULL )
258143* tzp = tz ;
259144#ifdef HAVE_INT64_TIMESTAMP
260145* fsec = usec ;
261146#else
262147* fsec = usec * 1.0e-6 ;
263148#endif
264- }/* GetCurrentTimeUsec() */
149+ }
265150
266151
267152void
268153abstime2tm (AbsoluteTime _time ,int * tzp ,struct tm * tm ,char * * tzn )
269154{
270155time_t time = (time_t )_time ;
271-
272- #if defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE )
273156struct tm * tx ;
274157
275- #else
276- struct timeb tb ;/* the old V7-ism */
277-
278- ftime (& tb );
279- #endif
280-
281158/*
282159 * If HasCTZSet is true then we have a brute force time zone
283160 * specified. Go ahead and rotate to the local time zone since we will
@@ -286,7 +163,6 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
286163if (HasCTZSet && (tzp != NULL ))
287164time -= CTimeZone ;
288165
289- #if defined(HAVE_TM_ZONE )|| defined(HAVE_INT_TIMEZONE )
290166if ((!HasCTZSet )&& (tzp != NULL ))
291167tx = localtime ((time_t * )& time );
292168else
@@ -336,7 +212,8 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
336212 */
337213StrNCpy (* tzn ,tm -> tm_zone ,MAXTZLEN + 1 );
338214if (strlen (tm -> tm_zone )> MAXTZLEN )
339- elog (WARNING ,"Invalid timezone \'%s\'" ,tm -> tm_zone );
215+ elog (WARNING ,"Invalid timezone \'%s\'" ,
216+ tm -> tm_zone );
340217}
341218}
342219}
@@ -369,13 +246,13 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
369246 */
370247StrNCpy (* tzn ,tzname [tm -> tm_isdst ],MAXTZLEN + 1 );
371248if (strlen (tzname [tm -> tm_isdst ])> MAXTZLEN )
372- elog (WARNING ,"Invalid timezone \'%s\'" ,tzname [tm -> tm_isdst ]);
249+ elog (WARNING ,"Invalid timezone \'%s\'" ,
250+ tzname [tm -> tm_isdst ]);
373251}
374252}
375253}
376254else
377255tm -> tm_isdst = -1 ;
378- #endif
379256#else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
380257if (tzp != NULL )
381258{
@@ -391,26 +268,16 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
391268}
392269else
393270{
394- * tzp = tb .timezone * 60 ;
395-
396- /*
397- * XXX does this work to get the local timezone string in V7?
398- * - tgl 97/03/18
399- */
271+ /* default to UTC */
272+ * tzp = 0 ;
400273if (tzn != NULL )
401- {
402- strftime (* tzn ,MAXTZLEN ,"%Z" ,localtime (& now ));
403- tzn [MAXTZLEN ]= '\0' ;/* let's just be sure it's
404- * null-terminated */
405- }
274+ * tzn = NULL ;
406275}
407276}
408277else
409278tm -> tm_isdst = -1 ;
410279#endif
411-
412- return ;
413- }/* abstime2tm() */
280+ }
414281
415282
416283/* tm2abstime()
@@ -451,7 +318,7 @@ tm2abstime(struct tm * tm, int tz)
451318return INVALID_ABSTIME ;
452319
453320return sec ;
454- }/* tm2abstime() */
321+ }
455322
456323
457324/* nabstimein()
@@ -888,9 +755,7 @@ reltime2tm(RelativeTime time, struct tm * tm)
888755TMODULO (time ,tm -> tm_hour ,3600 );
889756TMODULO (time ,tm -> tm_min ,60 );
890757TMODULO (time ,tm -> tm_sec ,1 );
891-
892- return ;
893- }/* reltime2tm() */
758+ }
894759
895760
896761/*