1
1
/*-------------------------------------------------------------------------
2
+ *
2
3
* nabstime.c
3
4
* Utilities for the built-in type "AbsoluteTime".
4
5
* Functions for the built-in type "RelativeTime".
9
10
*
10
11
*
11
12
* 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 $
15
14
*
16
15
*-------------------------------------------------------------------------
17
16
*/
23
22
#include <float.h>
24
23
#include <limits.h>
25
24
26
- #if !(defined(HAVE_TM_ZONE ) || defined(HAVE_INT_TIMEZONE ))
27
- #include <sys/timeb.h>
28
- #endif
29
-
30
25
#include "access/xact.h"
31
26
#include "miscadmin.h"
32
27
#include "utils/builtins.h"
@@ -88,196 +83,78 @@ static int istinterval(char *i_string,
88
83
89
84
90
85
/* 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.
93
87
*
94
- * Returns the number of seconds since epoch (January 1 1970 GMT)
88
+ * Returns the number of seconds since epoch (January 1 1970 GMT).
95
89
*/
96
90
AbsoluteTime
97
91
GetCurrentAbsoluteTime (void )
98
92
{
99
93
time_t now ;
100
94
101
- #if defined(HAVE_TM_ZONE ) || defined(HAVE_INT_TIMEZONE )
102
- struct tm * tm ;
103
-
104
95
now = 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
-
154
96
return (AbsoluteTime ) now ;
155
- } /* GetCurrentAbsoluteTime() */
97
+ }
156
98
157
99
158
100
/* 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.
161
102
*
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.
163
105
*/
164
106
AbsoluteTime
165
107
GetCurrentAbsoluteTimeUsec (int * usec )
166
108
{
167
109
time_t now ;
168
110
struct timeval tp ;
169
111
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
-
180
112
gettimeofday (& tp , NULL );
181
-
182
113
now = tp .tv_sec ;
183
114
* 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
-
236
115
return (AbsoluteTime ) now ;
237
- } /* GetCurrentAbsoluteTimeUsec() */
116
+ }
238
117
239
118
119
+ /* GetCurrentDateTime()
120
+ * Get the transaction start time ("now()") broken down as a struct tm.
121
+ */
240
122
void
241
123
GetCurrentDateTime (struct tm * tm )
242
124
{
243
125
int tz ;
244
126
245
127
abstime2tm (GetCurrentTransactionStartTime (), & tz , tm , NULL );
246
- } /* GetCurrentDateTime() */
247
-
128
+ }
248
129
130
+ /* GetCurrentTimeUsec()
131
+ * Get the transaction start time ("now()") broken down as a struct tm,
132
+ * plus fractional-second and timezone info.
133
+ */
249
134
void
250
135
GetCurrentTimeUsec (struct tm * tm , fsec_t * fsec , int * tzp )
251
136
{
252
137
int tz ;
253
138
int usec ;
254
139
255
140
abstime2tm (GetCurrentTransactionStartTimeUsec (& usec ), & tz , tm , NULL );
256
- /* Note: don't pass NULL tzp directly to abstime2tm */
141
+ /* Note: don't pass NULL tzp to abstime2tm; affects behavior */
257
142
if (tzp != NULL )
258
143
* tzp = tz ;
259
144
#ifdef HAVE_INT64_TIMESTAMP
260
145
* fsec = usec ;
261
146
#else
262
147
* fsec = usec * 1.0e-6 ;
263
148
#endif
264
- } /* GetCurrentTimeUsec() */
149
+ }
265
150
266
151
267
152
void
268
153
abstime2tm (AbsoluteTime _time , int * tzp , struct tm * tm , char * * tzn )
269
154
{
270
155
time_t time = (time_t ) _time ;
271
-
272
- #if defined(HAVE_TM_ZONE ) || defined(HAVE_INT_TIMEZONE )
273
156
struct tm * tx ;
274
157
275
- #else
276
- struct timeb tb ; /* the old V7-ism */
277
-
278
- ftime (& tb );
279
- #endif
280
-
281
158
/*
282
159
* If HasCTZSet is true then we have a brute force time zone
283
160
* 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)
286
163
if (HasCTZSet && (tzp != NULL ))
287
164
time -= CTimeZone ;
288
165
289
- #if defined(HAVE_TM_ZONE ) || defined(HAVE_INT_TIMEZONE )
290
166
if ((!HasCTZSet ) && (tzp != NULL ))
291
167
tx = localtime ((time_t * ) & time );
292
168
else
@@ -336,7 +212,8 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
336
212
*/
337
213
StrNCpy (* tzn , tm -> tm_zone , MAXTZLEN + 1 );
338
214
if (strlen (tm -> tm_zone ) > MAXTZLEN )
339
- elog (WARNING , "Invalid timezone \'%s\'" , tm -> tm_zone );
215
+ elog (WARNING , "Invalid timezone \'%s\'" ,
216
+ tm -> tm_zone );
340
217
}
341
218
}
342
219
}
@@ -369,13 +246,13 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
369
246
*/
370
247
StrNCpy (* tzn , tzname [tm -> tm_isdst ], MAXTZLEN + 1 );
371
248
if (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 ]);
373
251
}
374
252
}
375
253
}
376
254
else
377
255
tm -> tm_isdst = -1 ;
378
- #endif
379
256
#else /* not (HAVE_TM_ZONE || HAVE_INT_TIMEZONE) */
380
257
if (tzp != NULL )
381
258
{
@@ -391,26 +268,16 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn)
391
268
}
392
269
else
393
270
{
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 ;
400
273
if (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 ;
406
275
}
407
276
}
408
277
else
409
278
tm -> tm_isdst = -1 ;
410
279
#endif
411
-
412
- return ;
413
- } /* abstime2tm() */
280
+ }
414
281
415
282
416
283
/* tm2abstime()
@@ -451,7 +318,7 @@ tm2abstime(struct tm * tm, int tz)
451
318
return INVALID_ABSTIME ;
452
319
453
320
return sec ;
454
- } /* tm2abstime() */
321
+ }
455
322
456
323
457
324
/* nabstimein()
@@ -888,9 +755,7 @@ reltime2tm(RelativeTime time, struct tm * tm)
888
755
TMODULO (time , tm -> tm_hour , 3600 );
889
756
TMODULO (time , tm -> tm_min , 60 );
890
757
TMODULO (time , tm -> tm_sec , 1 );
891
-
892
- return ;
893
- } /* reltime2tm() */
758
+ }
894
759
895
760
896
761
/*
0 commit comments