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

Commitd685417

Browse files
committed
Avoid repeated computation of the constants date2j(1970, 1, 1) and
date2j(2000, 1, 1). Should make for some marginal speed improvementin date/time operations.
1 parent3b4ca4c commitd685417

File tree

7 files changed

+91
-72
lines changed

7 files changed

+91
-72
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.79 2003/02/13 17:04:19 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.80 2003/04/0404:50:44 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -91,7 +91,7 @@ date_in(PG_FUNCTION_ARGS)
9191
elog(ERROR,"Unrecognized date external representation '%s'",str);
9292
}
9393

94-
date=(date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(2000,1,1));
94+
date=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-POSTGRES_EPOCH_JDATE;
9595

9696
PG_RETURN_DATEADT(date);
9797
}
@@ -108,7 +108,7 @@ date_out(PG_FUNCTION_ARGS)
108108
*tm=&tt;
109109
charbuf[MAXDATELEN+1];
110110

111-
j2date((date+date2j(2000,1,1)),
111+
j2date(date+POSTGRES_EPOCH_JDATE,
112112
&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
113113

114114
EncodeDateOnly(tm,DateStyle,buf);
@@ -256,9 +256,10 @@ date_pl_interval(PG_FUNCTION_ARGS)
256256

257257
if (span->month!=0)
258258
{
259-
j2date((dateVal+date2j(2000,1,1)),&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
259+
j2date(dateVal+POSTGRES_EPOCH_JDATE,
260+
&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
260261
tm->tm_mon+=span->month;
261-
dateVal=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(2000,1,1);
262+
dateVal=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-POSTGRES_EPOCH_JDATE;
262263
}
263264
if (span->time!=0)
264265
dateVal+= (span->time /86400e0);
@@ -279,9 +280,10 @@ date_mi_interval(PG_FUNCTION_ARGS)
279280

280281
if (span->month!=0)
281282
{
282-
j2date((dateVal+date2j(2000,1,1)),&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
283+
j2date(dateVal+POSTGRES_EPOCH_JDATE,
284+
&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
283285
tm->tm_mon-=span->month;
284-
dateVal=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(2000,1,1);
286+
dateVal=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-POSTGRES_EPOCH_JDATE;
285287
}
286288
if (span->time!=0)
287289
dateVal-= (span->time /86400e0);
@@ -346,7 +348,7 @@ date_timestamptz(PG_FUNCTION_ARGS)
346348
structtmtt,
347349
*tm=&tt;
348350

349-
j2date((dateVal+date2j(2000,1,1)),
351+
j2date(dateVal+POSTGRES_EPOCH_JDATE,
350352
&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday));
351353

352354
if (IS_VALID_UTIME(tm->tm_year,tm->tm_mon,tm->tm_mday))
@@ -399,7 +401,7 @@ timestamptz_date(PG_FUNCTION_ARGS)
399401
if (timestamp2tm(timestamp,&tz,tm,&fsec,&tzn)!=0)
400402
elog(ERROR,"Unable to convert timestamp to date");
401403

402-
result=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(2000,1,1);
404+
result=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-POSTGRES_EPOCH_JDATE;
403405

404406
PG_RETURN_DATEADT(result);
405407
}
@@ -431,7 +433,7 @@ abstime_date(PG_FUNCTION_ARGS)
431433

432434
default:
433435
abstime2tm(abstime,&tz,tm,NULL);
434-
result=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(2000,1,1);
436+
result=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-POSTGRES_EPOCH_JDATE;
435437
break;
436438
}
437439

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.102 2003/02/22 05:57:44 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.103 2003/04/04 04:50:44 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -648,6 +648,14 @@ j2date(int jd, int *year, int *month, int *day)
648648
return;
649649
}/* j2date() */
650650

651+
652+
/*
653+
* j2day - convert Julian date to day-of-week (0..6 == Sun..Sat)
654+
*
655+
* Note: various places use the locution j2day(date - 1) to produce a
656+
* result according to the convention 0..6 = Mon..Sun. This is a bit of
657+
* a crock, but will work as long as the computation here is just a modulo.
658+
*/
651659
int
652660
j2day(intdate)
653661
{
@@ -1261,7 +1269,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
12611269
*dtype=DTK_DATE;
12621270
GetCurrentDateTime(tm);
12631271
j2date((date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-1),
1264-
&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
1272+
&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
12651273
tm->tm_hour=0;
12661274
tm->tm_min=0;
12671275
tm->tm_sec=0;
@@ -1281,7 +1289,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
12811289
*dtype=DTK_DATE;
12821290
GetCurrentDateTime(tm);
12831291
j2date((date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)+1),
1284-
&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
1292+
&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
12851293
tm->tm_hour=0;
12861294
tm->tm_min=0;
12871295
tm->tm_sec=0;
@@ -1546,8 +1554,7 @@ DetermineLocalTimeZone(struct tm * tm)
15461554
delta2;
15471555
time_tmytime;
15481556

1549-
day= (date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-
1550-
date2j(1970,1,1));
1557+
day=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-UNIX_EPOCH_JDATE;
15511558
mysec=tm->tm_sec+ (tm->tm_min+ (day*24+tm->tm_hour)*60)*60;
15521559
mytime= (time_t)mysec;
15531560

@@ -1556,8 +1563,8 @@ DetermineLocalTimeZone(struct tm * tm)
15561563
* and reassemble to get a representation of local time.
15571564
*/
15581565
tmp=localtime(&mytime);
1559-
day=(date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1560-
date2j(1970,1,1));
1566+
day=date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1567+
UNIX_EPOCH_JDATE;
15611568
locsec=tmp->tm_sec+ (tmp->tm_min+ (day*24+tmp->tm_hour)*60)*60;
15621569

15631570
/*
@@ -1586,17 +1593,17 @@ DetermineLocalTimeZone(struct tm * tm)
15861593
mysec+=delta1;
15871594
mytime= (time_t)mysec;
15881595
tmp=localtime(&mytime);
1589-
day=(date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1590-
date2j(1970,1,1));
1596+
day=date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1597+
UNIX_EPOCH_JDATE;
15911598
locsec=tmp->tm_sec+ (tmp->tm_min+ (day*24+tmp->tm_hour)*60)*60;
15921599
delta2=mysec-locsec;
15931600
if (delta2!=delta1)
15941601
{
15951602
mysec+= (delta2-delta1);
15961603
mytime= (time_t)mysec;
15971604
tmp=localtime(&mytime);
1598-
day=(date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1599-
date2j(1970,1,1));
1605+
day=date2j(tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_mday)-
1606+
UNIX_EPOCH_JDATE;
16001607
locsec=tmp->tm_sec+ (tmp->tm_min+ (day*24+tmp->tm_hour)*60)*60;
16011608
delta2=mysec-locsec;
16021609
}
@@ -2360,7 +2367,7 @@ DecodeNumber(int flen, char *str, int fmask,
23602367
{
23612368
*tmask= (DTK_M(DOY) |DTK_M(MONTH) |DTK_M(DAY));
23622369
tm->tm_yday=val;
2363-
j2date((date2j(tm->tm_year,1,1)+tm->tm_yday-1),
2370+
j2date(date2j(tm->tm_year,1,1)+tm->tm_yday-1,
23642371
&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
23652372
}
23662373

@@ -3702,6 +3709,9 @@ CheckDateTokenTables(void)
37023709
{
37033710
boolok= true;
37043711

3712+
Assert(UNIX_EPOCH_JDATE==date2j(1970,1,1));
3713+
Assert(POSTGRES_EPOCH_JDATE==date2j(2000,1,1));
3714+
37053715
ok &=CheckDateTokenTable("datetktbl",datetktbl,szdatetktbl);
37063716
ok &=CheckDateTokenTable("deltatktbl",deltatktbl,szdeltatktbl);
37073717
ok &=CheckDateTokenTable("australian_datetktbl",

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.105 2003/03/20 03:34:56 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.106 2003/04/04 04:50:44 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -85,10 +85,7 @@ static int istinterval(char *i_string,
8585
/*
8686
* GetCurrentAbsoluteTime()
8787
*
88-
* Get the current system time. Set timezone parameters if not specified
89-
* elsewhere. Define HasCTZSet to allow clients to specify the default
90-
* timezone.
91-
*
88+
* Get the current system time (relative to Unix epoch).
9289
*/
9390
AbsoluteTime
9491
GetCurrentAbsoluteTime(void)
@@ -100,11 +97,11 @@ GetCurrentAbsoluteTime(void)
10097
}
10198

10299

103-
/* GetCurrentAbsoluteTimeUsec()
104-
*Get the current system time.
100+
/*
101+
*GetCurrentAbsoluteTimeUsec()
105102
*
106-
*Returns thenumber of seconds since epoch (January 1 1970 GMT),
107-
*and returns fractionalseconds(as# ofmicroseconds) into *usec.
103+
*Get thecurrent system time (relative to Unix epoch), including fractional
104+
* secondsexpressedas microseconds.
108105
*/
109106
AbsoluteTime
110107
GetCurrentAbsoluteTimeUsec(int*usec)
@@ -119,7 +116,31 @@ GetCurrentAbsoluteTimeUsec(int *usec)
119116
}
120117

121118

122-
/* GetCurrentDateTime()
119+
/*
120+
* AbsoluteTimeUsecToTimestampTz()
121+
*
122+
* Convert system time including microseconds to TimestampTz representation.
123+
*/
124+
TimestampTz
125+
AbsoluteTimeUsecToTimestampTz(AbsoluteTimesec,intusec)
126+
{
127+
TimestampTzresult;
128+
129+
#ifdefHAVE_INT64_TIMESTAMP
130+
result= ((sec- ((POSTGRES_EPOCH_JDATE-UNIX_EPOCH_JDATE)*86400))
131+
*INT64CONST(1000000))+usec;
132+
#else
133+
result=sec- ((POSTGRES_EPOCH_JDATE-UNIX_EPOCH_JDATE)*86400)
134+
+ (usec /1000000.0);
135+
#endif
136+
137+
returnresult;
138+
}
139+
140+
141+
/*
142+
* GetCurrentDateTime()
143+
*
123144
* Get the transaction start time ("now()") broken down as a struct tm.
124145
*/
125146
void
@@ -131,13 +152,10 @@ GetCurrentDateTime(struct tm * tm)
131152
}
132153

133154
/*
134-
* GetCurrentAbsoluteTimeUsec()
135-
*
136-
* Get the current system time. Set timezone parameters if not specified
137-
* elsewhere. Define HasCTZSet to allow clients to specify the default
138-
* timezone.
155+
* GetCurrentTimeUsec()
139156
*
140-
* Returns the number of seconds since epoch (January 1 1970 GMT)
157+
* Get the transaction start time ("now()") broken down as a struct tm,
158+
* including fractional seconds and timezone offset.
141159
*/
142160
void
143161
GetCurrentTimeUsec(structtm*tm,fsec_t*fsec,int*tzp)
@@ -152,7 +170,7 @@ GetCurrentTimeUsec(struct tm * tm, fsec_t *fsec, int *tzp)
152170
#ifdefHAVE_INT64_TIMESTAMP
153171
*fsec=usec;
154172
#else
155-
*fsec=usec*1.0e-6;
173+
*fsec=usec/1000000.0;
156174
#endif
157175
}
158176

@@ -307,7 +325,7 @@ tm2abstime(struct tm * tm, int tz)
307325
||tm->tm_sec<0||tm->tm_sec>59)
308326
returnINVALID_ABSTIME;
309327

310-
day=(date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-date2j(1970,1,1));
328+
day=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-UNIX_EPOCH_JDATE;
311329

312330
/* check for time out of range */
313331
if ((day<MIN_DAYNUM)|| (day>MAX_DAYNUM))

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,7 @@ pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
328328
if (sec==0&&usec==0)
329329
PG_RETURN_NULL();
330330

331-
/*
332-
* This method of converting "Unix time" (sec/usec since epoch) to a
333-
* PostgreSQL timestamp is an ugly hack -- if you fix it, be sure to
334-
* fix the similar hackery in timestamp.c
335-
*/
336-
#ifdefHAVE_INT64_TIMESTAMP
337-
result= (((sec- ((date2j(2000,1,1)-date2j(1970,1,1))*86400))
338-
*INT64CONST(1000000))+usec);
339-
#else
340-
result= (sec+ (usec*1.0e-6)- ((date2j(2000,1,1)-
341-
date2j(1970,1,1))*86400));
342-
#endif
331+
result=AbsoluteTimeUsecToTimestampTz(sec,usec);
343332

344333
PG_RETURN_TIMESTAMPTZ(result);
345334
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp