88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.105 2005/04 /2322:53:05 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.106 2005/05 /2318:56:55 momjian Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -281,11 +281,11 @@ date_mii(PG_FUNCTION_ARGS)
281281#ifdef HAVE_INT64_TIMESTAMP
282282/* date is days since 2000, timestamp is microseconds since same... */
283283#define date2timestamp (dateVal ) \
284- ((Timestamp) ((dateVal) *INT64CONST(86400000000) ))
284+ ((Timestamp) ((dateVal) *USECS_PER_DAY ))
285285#else
286286/* date is days since 2000, timestamp is seconds since same... */
287287#define date2timestamp (dateVal ) \
288- ((Timestamp) ((dateVal) *86400.0 ))
288+ ((Timestamp) ((dateVal) *(double)SECS_PER_DAY ))
289289#endif
290290
291291static TimestampTz
@@ -305,10 +305,10 @@ date2timestamptz(DateADT dateVal)
305305tz = DetermineLocalTimeZone (tm );
306306
307307#ifdef HAVE_INT64_TIMESTAMP
308- result = (dateVal * INT64CONST ( 86400000000 ) )
309- + (tz * INT64CONST ( 1000000 ) );
308+ result = (dateVal * USECS_PER_DAY )
309+ + (tz * USECS_PER_SEC );
310310#else
311- result = dateVal * 86400.0 + tz ;
311+ result = dateVal * ( double ) SECS_PER_DAY + tz ;
312312#endif
313313
314314return result ;
@@ -922,7 +922,7 @@ tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result)
922922{
923923#ifdef HAVE_INT64_TIMESTAMP
924924* result = ((((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec )
925- * INT64CONST ( 1000000 ) )+ fsec );
925+ * USECS_PER_SEC )+ fsec );
926926#else
927927* result = ((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec + fsec );
928928#endif
@@ -938,12 +938,12 @@ static int
938938time2tm (TimeADT time ,struct pg_tm * tm ,fsec_t * fsec )
939939{
940940#ifdef HAVE_INT64_TIMESTAMP
941- tm -> tm_hour = (time /INT64CONST ( 3600000000 ) );
942- time -= (tm -> tm_hour * INT64CONST ( 3600000000 ) );
943- tm -> tm_min = (time /INT64CONST ( 60000000 ) );
944- time -= (tm -> tm_min * INT64CONST ( 60000000 ) );
945- tm -> tm_sec = (time /INT64CONST ( 1000000 ) );
946- time -= (tm -> tm_sec * INT64CONST ( 1000000 ) );
941+ tm -> tm_hour = (time /USECS_PER_HOUR );
942+ time -= (tm -> tm_hour * USECS_PER_HOUR );
943+ tm -> tm_min = (time /USECS_PER_MINUTE );
944+ time -= (tm -> tm_min * USECS_PER_MINUTE );
945+ tm -> tm_sec = (time /USECS_PER_SEC );
946+ time -= (tm -> tm_sec * USECS_PER_SEC );
947947* fsec = time ;
948948#else
949949double trem ;
@@ -1343,7 +1343,7 @@ timestamp_time(PG_FUNCTION_ARGS)
13431343 * 86400000000) - timestamp;
13441344 */
13451345result = ((((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec )
1346- * INT64CONST ( 1000000 ) )+ fsec );
1346+ * USECS_PER_SEC )+ fsec );
13471347#else
13481348result = ((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec + fsec );
13491349#endif
@@ -1380,7 +1380,7 @@ timestamptz_time(PG_FUNCTION_ARGS)
13801380 * 86400000000) - timestamp;
13811381 */
13821382result = ((((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec )
1383- * INT64CONST ( 1000000 ) )+ fsec );
1383+ * USECS_PER_SEC )+ fsec );
13841384#else
13851385result = ((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec + fsec );
13861386#endif
@@ -1440,20 +1440,20 @@ interval_time(PG_FUNCTION_ARGS)
14401440int64 days ;
14411441
14421442result = span -> time ;
1443- if (result >=INT64CONST ( 86400000000 ) )
1443+ if (result >=USECS_PER_DAY )
14441444{
1445- days = result /INT64CONST ( 86400000000 ) ;
1446- result -= days * INT64CONST ( 86400000000 ) ;
1445+ days = result /USECS_PER_DAY ;
1446+ result -= days * USECS_PER_DAY ;
14471447}
14481448else if (result < 0 )
14491449{
1450- days = (- result + INT64CONST ( 86400000000 ) - 1 ) /INT64CONST ( 86400000000 ) ;
1451- result += days * INT64CONST ( 86400000000 ) ;
1450+ days = (- result + USECS_PER_DAY - 1 ) /USECS_PER_DAY ;
1451+ result += days * USECS_PER_DAY ;
14521452}
14531453#else
14541454result = span -> time ;
1455- if (result >=86400e0 || result < 0 )
1456- result -= floor (result /86400e0 ) * 86400e0 ;
1455+ if (result >=( double ) SECS_PER_DAY || result < 0 )
1456+ result -= floor (result /( double ) SECS_PER_DAY ) * ( double ) SECS_PER_DAY ;
14571457#endif
14581458
14591459PG_RETURN_TIMEADT (result );
@@ -1489,14 +1489,14 @@ time_pl_interval(PG_FUNCTION_ARGS)
14891489
14901490#ifdef HAVE_INT64_TIMESTAMP
14911491result = (time + span -> time );
1492- result -= (result /INT64CONST ( 86400000000 ) * INT64CONST ( 86400000000 ) );
1492+ result -= (result /USECS_PER_DAY * USECS_PER_DAY );
14931493if (result < INT64CONST (0 ))
1494- result += INT64CONST ( 86400000000 ) ;
1494+ result += USECS_PER_DAY ;
14951495#else
14961496TimeADT time1 ;
14971497
14981498result = (time + span -> time );
1499- TMODULO (result ,time1 ,86400e0 );
1499+ TMODULO (result ,time1 ,( double ) SECS_PER_DAY );
15001500if (result < 0 )
15011501result += 86400 ;
15021502#endif
@@ -1516,14 +1516,14 @@ time_mi_interval(PG_FUNCTION_ARGS)
15161516
15171517#ifdef HAVE_INT64_TIMESTAMP
15181518result = (time - span -> time );
1519- result -= (result /INT64CONST ( 86400000000 ) * INT64CONST ( 86400000000 ) );
1519+ result -= (result /USECS_PER_DAY * USECS_PER_DAY );
15201520if (result < INT64CONST (0 ))
1521- result += INT64CONST ( 86400000000 ) ;
1521+ result += USECS_PER_DAY ;
15221522#else
15231523TimeADT time1 ;
15241524
15251525result = (time - span -> time );
1526- TMODULO (result ,time1 ,86400e0 );
1526+ TMODULO (result ,time1 ,( double ) SECS_PER_DAY );
15271527if (result < 0 )
15281528result += 86400 ;
15291529#endif
@@ -1624,7 +1624,7 @@ time_part(PG_FUNCTION_ARGS)
16241624{
16251625case DTK_MICROSEC :
16261626#ifdef HAVE_INT64_TIMESTAMP
1627- result = ((tm -> tm_sec * INT64CONST ( 1000000 ) )+ fsec );
1627+ result = ((tm -> tm_sec * USECS_PER_SEC )+ fsec );
16281628#else
16291629result = ((tm -> tm_sec + fsec )* 1000000 );
16301630#endif
@@ -1641,7 +1641,7 @@ time_part(PG_FUNCTION_ARGS)
16411641
16421642case DTK_SECOND :
16431643#ifdef HAVE_INT64_TIMESTAMP
1644- result = (tm -> tm_sec + (fsec /INT64CONST ( 1000000 ) ));
1644+ result = (tm -> tm_sec + (fsec /USECS_PER_SEC ));
16451645#else
16461646result = (tm -> tm_sec + fsec );
16471647#endif
@@ -1709,7 +1709,7 @@ tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result)
17091709{
17101710#ifdef HAVE_INT64_TIMESTAMP
17111711result -> time = ((((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec )
1712- * INT64CONST ( 1000000 ) )+ fsec );
1712+ * USECS_PER_SEC )+ fsec );
17131713#else
17141714result -> time = ((((tm -> tm_hour * 60 )+ tm -> tm_min )* 60 )+ tm -> tm_sec + fsec );
17151715#endif
@@ -1823,12 +1823,12 @@ timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp)
18231823#ifdef HAVE_INT64_TIMESTAMP
18241824int64 trem = time -> time ;
18251825
1826- tm -> tm_hour = (trem /INT64CONST ( 3600000000 ) );
1827- trem -= (tm -> tm_hour * INT64CONST ( 3600000000 ) );
1828- tm -> tm_min = (trem /INT64CONST ( 60000000 ) );
1829- trem -= (tm -> tm_min * INT64CONST ( 60000000 ) );
1830- tm -> tm_sec = (trem /INT64CONST ( 1000000 ) );
1831- * fsec = (trem - (tm -> tm_sec * INT64CONST ( 1000000 ) ));
1826+ tm -> tm_hour = (trem /USECS_PER_HOUR );
1827+ trem -= (tm -> tm_hour * USECS_PER_HOUR );
1828+ tm -> tm_min = (trem /USECS_PER_MINUTE );
1829+ trem -= (tm -> tm_min * USECS_PER_MINUTE );
1830+ tm -> tm_sec = (trem /USECS_PER_SEC );
1831+ * fsec = (trem - (tm -> tm_sec * USECS_PER_SEC ));
18321832#else
18331833double trem = time -> time ;
18341834
@@ -1874,8 +1874,8 @@ timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
18741874int64 t1 ,
18751875t2 ;
18761876
1877- t1 = time1 -> time + (time1 -> zone * INT64CONST ( 1000000 ) );
1878- t2 = time2 -> time + (time2 -> zone * INT64CONST ( 1000000 ) );
1877+ t1 = time1 -> time + (time1 -> zone * USECS_PER_SEC );
1878+ t2 = time2 -> time + (time2 -> zone * USECS_PER_SEC );
18791879#else
18801880double t1 ,
18811881t2 ;
@@ -2026,12 +2026,12 @@ timetz_pl_interval(PG_FUNCTION_ARGS)
20262026
20272027#ifdef HAVE_INT64_TIMESTAMP
20282028result -> time = (time -> time + span -> time );
2029- result -> time -= (result -> time /INT64CONST ( 86400000000 ) * INT64CONST ( 86400000000 ) );
2029+ result -> time -= (result -> time /USECS_PER_DAY * USECS_PER_DAY );
20302030if (result -> time < INT64CONST (0 ))
2031- result -> time += INT64CONST ( 86400000000 ) ;
2031+ result -> time += USECS_PER_DAY ;
20322032#else
20332033result -> time = (time -> time + span -> time );
2034- TMODULO (result -> time ,time1 .time ,86400e0 );
2034+ TMODULO (result -> time ,time1 .time ,( double ) SECS_PER_DAY );
20352035if (result -> time < 0 )
20362036result -> time += 86400 ;
20372037#endif
@@ -2059,12 +2059,12 @@ timetz_mi_interval(PG_FUNCTION_ARGS)
20592059
20602060#ifdef HAVE_INT64_TIMESTAMP
20612061result -> time = (time -> time - span -> time );
2062- result -> time -= (result -> time /INT64CONST ( 86400000000 ) * INT64CONST ( 86400000000 ) );
2062+ result -> time -= (result -> time /USECS_PER_DAY * USECS_PER_DAY );
20632063if (result -> time < INT64CONST (0 ))
2064- result -> time += INT64CONST ( 86400000000 ) ;
2064+ result -> time += USECS_PER_DAY ;
20652065#else
20662066result -> time = (time -> time - span -> time );
2067- TMODULO (result -> time ,time1 .time ,86400e0 );
2067+ TMODULO (result -> time ,time1 .time ,( double ) SECS_PER_DAY );
20682068if (result -> time < 0 )
20692069result -> time += 86400 ;
20702070#endif
@@ -2281,10 +2281,10 @@ datetimetz_timestamptz(PG_FUNCTION_ARGS)
22812281TimestampTz result ;
22822282
22832283#ifdef HAVE_INT64_TIMESTAMP
2284- result = (((date * INT64CONST ( 86400000000 ) )+ time -> time )
2285- + (time -> zone * INT64CONST ( 1000000 ) ));
2284+ result = (((date * USECS_PER_DAY )+ time -> time )
2285+ + (time -> zone * USECS_PER_SEC ));
22862286#else
2287- result = (((date * 86400.0 )+ time -> time )+ time -> zone );
2287+ result = (((date * ( double ) SECS_PER_DAY )+ time -> time )+ time -> zone );
22882288#endif
22892289
22902290PG_RETURN_TIMESTAMP (result );
@@ -2400,7 +2400,7 @@ timetz_part(PG_FUNCTION_ARGS)
24002400
24012401case DTK_MICROSEC :
24022402#ifdef HAVE_INT64_TIMESTAMP
2403- result = ((tm -> tm_sec * INT64CONST ( 1000000 ) )+ fsec );
2403+ result = ((tm -> tm_sec * USECS_PER_SEC )+ fsec );
24042404#else
24052405result = ((tm -> tm_sec + fsec )* 1000000 );
24062406#endif
@@ -2417,7 +2417,7 @@ timetz_part(PG_FUNCTION_ARGS)
24172417
24182418case DTK_SECOND :
24192419#ifdef HAVE_INT64_TIMESTAMP
2420- result = (tm -> tm_sec + (fsec /INT64CONST ( 1000000 ) ));
2420+ result = (tm -> tm_sec + (fsec /USECS_PER_SEC ));
24212421#else
24222422result = (tm -> tm_sec + fsec );
24232423#endif
@@ -2496,11 +2496,11 @@ timetz_zone(PG_FUNCTION_ARGS)
24962496{
24972497tz = val * 60 ;
24982498#ifdef HAVE_INT64_TIMESTAMP
2499- result -> time = time -> time + ((time -> zone - tz )* INT64CONST ( 1000000 ) );
2499+ result -> time = time -> time + ((time -> zone - tz )* USECS_PER_SEC );
25002500while (result -> time < INT64CONST (0 ))
2501- result -> time += INT64CONST ( 86400000000 ) ;
2502- while (result -> time >=INT64CONST ( 86400000000 ) )
2503- result -> time -= INT64CONST ( 86400000000 ) ;
2501+ result -> time += USECS_PER_DAY ;
2502+ while (result -> time >=USECS_PER_DAY )
2503+ result -> time -= USECS_PER_DAY ;
25042504#else
25052505result -> time = time -> time + (time -> zone - tz );
25062506while (result -> time < 0 )
@@ -2542,19 +2542,19 @@ timetz_izone(PG_FUNCTION_ARGS)
25422542PointerGetDatum (zone ))))));
25432543
25442544#ifdef HAVE_INT64_TIMESTAMP
2545- tz = - (zone -> time /INT64CONST ( 1000000 ) );
2545+ tz = - (zone -> time /USECS_PER_SEC );
25462546#else
25472547tz = - (zone -> time );
25482548#endif
25492549
25502550result = (TimeTzADT * )palloc (sizeof (TimeTzADT ));
25512551
25522552#ifdef HAVE_INT64_TIMESTAMP
2553- result -> time = time -> time + ((time -> zone - tz )* INT64CONST ( 1000000 ) );
2553+ result -> time = time -> time + ((time -> zone - tz )* USECS_PER_SEC );
25542554while (result -> time < INT64CONST (0 ))
2555- result -> time += INT64CONST ( 86400000000 ) ;
2556- while (result -> time >=INT64CONST ( 86400000000 ) )
2557- result -> time -= INT64CONST ( 86400000000 ) ;
2555+ result -> time += USECS_PER_DAY ;
2556+ while (result -> time >=USECS_PER_DAY )
2557+ result -> time -= USECS_PER_DAY ;
25582558#else
25592559result -> time = time -> time + (time -> zone - tz );
25602560while (result -> time < 0 )