Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1f4f453

Browse files
author
Thomas G. Lockhart
committed
Use the standard date/time encoder rather than strftime() for output.
This allows use of the DateStyle session variable.
1 parent01264e8 commit1f4f453

File tree

1 file changed

+63
-122
lines changed

1 file changed

+63
-122
lines changed

‎src/backend/utils/adt/nabstime.c

Lines changed: 63 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.26 1997/05/11 15:11:45 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.27 1997/06/23 14:56:15 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -97,35 +97,67 @@ GetCurrentTime(struct tm *tm)
9797
{
9898
inttz;
9999

100-
abstime2tm(GetCurrentTransactionStartTime(),&tz,tm);
100+
abstime2tm(GetCurrentTransactionStartTime(),&tz,tm,NULL);
101101

102102
return;
103103
}/* GetCurrentTime() */
104104

105105

106106
void
107-
abstime2tm(AbsoluteTimetime,int*tzp,structtm*tm)
107+
abstime2tm(AbsoluteTimetime,int*tzp,structtm*tm,char*tzn)
108108
{
109-
structtm*tt;
109+
#ifdefUSE_POSIX_TIME
110+
structtm*tx;
111+
#else/* ! USE_POSIX_TIME */
112+
structtimebtb;/* the old V7-ism */
110113

111-
#ifFALSE
112-
if (tzp!=NULL)time-=*tzp;
113-
tt=gmtime((time_t*)&time);
114+
(void)ftime(&tb);
114115
#endif
115-
/* XXX HACK to get time behavior compatible with Postgres v6.0 - tgl 97/04/07 */
116+
117+
#ifdefUSE_POSIX_TIME
116118
if (tzp!=NULL) {
117-
tt=localtime((time_t*)&time);
119+
tx=localtime((time_t*)&time);
118120
}else {
119-
tt=gmtime((time_t*)&time);
121+
tx=gmtime((time_t*)&time);
120122
};
123+
#else
124+
#endif
125+
126+
#ifdefDATEDEBUG
127+
#ifdefHAVE_INT_TIMEZONE
128+
printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s %s dst=%d\n",
129+
tx->tm_year,tx->tm_mon,tx->tm_mday,tx->tm_hour,tx->tm_min,tx->tm_sec,
130+
tzname[0],tzname[1],tx->tm_isdst);
131+
#else
132+
printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s dst=%d\n",
133+
tx->tm_year,tx->tm_mon,tx->tm_mday,tx->tm_hour,tx->tm_min,tx->tm_sec,
134+
tx->tm_zone,tx->tm_isdst);
135+
#endif
136+
#else
137+
#endif
121138

122-
tm->tm_year=tt->tm_year+1900;
123-
tm->tm_mon=tt->tm_mon+1;
124-
tm->tm_mday=tt->tm_mday;
125-
tm->tm_hour=tt->tm_hour;
126-
tm->tm_min=tt->tm_min;
127-
tm->tm_sec=tt->tm_sec;
128-
tm->tm_isdst=tt->tm_isdst;
139+
tm->tm_year=tx->tm_year+1900;
140+
tm->tm_mon=tx->tm_mon+1;
141+
tm->tm_mday=tx->tm_mday;
142+
tm->tm_hour=tx->tm_hour;
143+
tm->tm_min=tx->tm_min;
144+
tm->tm_sec=tx->tm_sec;
145+
tm->tm_isdst=tx->tm_isdst;
146+
147+
#ifdefUSE_POSIX_TIME
148+
#ifdefHAVE_INT_TIMEZONE
149+
if (tzp!=NULL)*tzp= (tm->tm_isdst? (timezone-3600):timezone);
150+
if (tzn!=NULL)strcpy(tzn,tzname[tm->tm_isdst]);
151+
#else/* !HAVE_INT_TIMEZONE */
152+
if (tzp!=NULL)*tzp=-tm->tm_gmtoff;/* tm_gmtoff is Sun/DEC-ism */
153+
/* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
154+
if (tzn!=NULL)strcpy(tzn,tm->tm_zone);
155+
#endif
156+
#else/* ! USE_POSIX_TIME */
157+
if (tzp!=NULL)*tzp=tb.timezone*60;
158+
/* XXX does this work to get the local timezone string in V7? - tgl 97/03/18 */
159+
if (tzn!=NULL)strftime(tzn,MAXTZLEN,"%Z",localtime(&now));
160+
#endif
129161

130162
return;
131163
}/* abstime2tm() */
@@ -163,15 +195,6 @@ tm2abstime( struct tm *tm, int tz)
163195
(day==MIN_DAYNUM&&sec>0))
164196
return(INVALID_ABSTIME);
165197

166-
#ifFALSE
167-
/* daylight correction */
168-
if (tm->tm_isdst<0) {/* unknown; find out */
169-
tm->tm_isdst= (CDayLight>0);
170-
};
171-
if (tm->tm_isdst>0)
172-
sec-=60*60;
173-
#endif
174-
175198
/* check for reserved values (e.g. "current" on edge of usual range */
176199
if (!AbsoluteTimeIsReal(sec))
177200
return(INVALID_ABSTIME);
@@ -246,22 +269,18 @@ printf( "nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE
246269
}/* nabstimein() */
247270

248271

249-
/*
272+
/* nabstimeout()
250273
* Given an AbsoluteTime return the English text version of the date
251274
*/
252275
char*
253276
nabstimeout(AbsoluteTimetime)
254277
{
255-
/*
256-
* Fri Jan 28 23:05:29 1994 PST
257-
* 0 1 2
258-
* 12345678901234567890123456789
259-
*
260-
* we allocate some extra -- timezones are usually 3 characters but
261-
* this is not in the POSIX standard...
262-
*/
263-
charbuf[40];
264278
char*result;
279+
inttz;
280+
doublefsec=0;
281+
structtmtt,*tm=&tt;
282+
charbuf[MAXDATELEN+1];
283+
charzone[MAXDATELEN+1],*tzn=zone;
265284

266285
switch (time) {
267286
caseEPOCH_ABSTIME: (void)strcpy(buf,EPOCH);break;
@@ -270,96 +289,18 @@ nabstimeout(AbsoluteTime time)
270289
caseNOEND_ABSTIME: (void)strcpy(buf,LATE);break;
271290
caseNOSTART_ABSTIME: (void)strcpy(buf,EARLY);break;
272291
default:
273-
/* hack -- localtime happens to work for negative times */
274-
(void)strftime(buf,sizeof(buf),"%a %b %d %H:%M:%S %Y %Z",
275-
localtime((time_t*)&time));
292+
abstime2tm(time,&tz,tm,tzn);
293+
#ifDATEDEBUG
294+
#endif
295+
EncodeDateTime(tm,fsec,&tz,&tzn,DateStyle,buf);
276296
break;
277297
}
278-
result= (char*)palloc(strlen(buf)+1);
279-
strcpy(result,buf);
280-
returnresult;
281-
}
282-
283298

284-
/* turn a (struct tm) and a few variables into a time_t, with range checking */
285-
AbsoluteTime
286-
dateconv(registerstructtm*tm,intzone)
287-
{
288-
tm->tm_wday=tm->tm_yday=0;
289-
290-
#ifFALSE
291-
if (tm->tm_year<70) {
292-
tm->tm_year+=2000;
293-
}elseif (tm->tm_year<1000) {
294-
tm->tm_year+=1900;
295-
};
296-
#endif
297-
298-
/* validate, before going out of range on some members */
299-
if (tm->tm_year<1901||tm->tm_year>2038
300-
||tm->tm_mon<1||tm->tm_mon>12
301-
||tm->tm_mday<1||tm->tm_mday>31
302-
||tm->tm_hour<0||tm->tm_hour >=24
303-
||tm->tm_min<0||tm->tm_min>59
304-
||tm->tm_sec<0||tm->tm_sec>59)
305-
returnINVALID_ABSTIME;
306-
307-
/*
308-
* zone should really be -zone, and tz should be set to tp->value, not
309-
* -tp->value. Or the table could be fixed.
310-
*/
311-
tm->tm_sec+=zone;/* mktime lets it be out of range */
312-
313-
/* convert to seconds */
314-
returnqmktime(tm);
315-
}
316-
317-
318-
/*
319-
* near-ANSI qmktime suitable for use by dateconv; not necessarily as paranoid
320-
* as ANSI requires, and it may not canonicalise the struct tm. Ignores tm_wday
321-
* and tm_yday.
322-
*/
323-
time_t
324-
qmktime(structtm*tm)
325-
{
326-
time_tsec;
327-
328-
intday;
329-
330-
#ifFALSE
331-
/* If it was a 2 digit year */
332-
if (tm->tm_year<100)
333-
tm->tm_year+=1900;
334-
#endif
335-
336-
day= (date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(1970,1,1));
337-
338-
/* check for time out of range */
339-
if ((day<MIN_DAYNUM)|| (day>MAX_DAYNUM))
340-
returnINVALID_ABSTIME;
341-
342-
/* convert to seconds */
343-
sec=tm->tm_sec+ (tm->tm_min+(day*24+tm->tm_hour)*60)*60;
344-
345-
/* check for overflow */
346-
if ((day==MAX_DAYNUM&&sec<0)||
347-
(day==MIN_DAYNUM&&sec>0))
348-
returnINVALID_ABSTIME;
349-
350-
/* check for reserved values (e.g. "current" on edge of usual range */
351-
if (!AbsoluteTimeIsReal(sec))
352-
returnINVALID_ABSTIME;
353-
354-
/* daylight correction */
355-
if (tm->tm_isdst<0) {/* unknown; find out */
356-
tm->tm_isdst= (CDayLight>0);
357-
};
358-
if (tm->tm_isdst>0)
359-
sec-=60*60;
299+
result=PALLOC(strlen(buf)+1);
300+
strcpy(result,buf);
360301

361-
returnsec;
362-
}/*qmktime() */
302+
return(result);
303+
}/*nabstimeout() */
363304

364305

365306
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp