Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5ebaae8

Browse files
committed
Add datetime macros for constants, for clarity:
#define SECS_PER_DAY 86400 #define USECS_PER_DAY INT64CONST(86400000000) #define USECS_PER_HOUR INT64CONST(3600000000) #define USECS_PER_MINUTE INT64CONST(60000000) #define USECS_PER_SEC INT64CONST(1000000)
1 parent 33d0d4c commit 5ebaae8

File tree

8 files changed

+146
-141
lines changed

8 files changed

+146
-141
lines changed

src/backend/utils/adt/date.c

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.105 2005/04/23 22:53:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.106 2005/05/23 18: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

291291
static TimestampTz
@@ -305,10 +305,10 @@ date2timestamptz(DateADT dateVal)
305305
tz = 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

314314
return 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
938938
time2tm(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
949949
double trem;
@@ -1343,7 +1343,7 @@ timestamp_time(PG_FUNCTION_ARGS)
13431343
* 86400000000) - timestamp;
13441344
*/
13451345
result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1346-
* INT64CONST(1000000)) + fsec);
1346+
* USECS_PER_SEC) + fsec);
13471347
#else
13481348
result = ((((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
*/
13821382
result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1383-
* INT64CONST(1000000)) + fsec);
1383+
* USECS_PER_SEC) + fsec);
13841384
#else
13851385
result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
13861386
#endif
@@ -1440,20 +1440,20 @@ interval_time(PG_FUNCTION_ARGS)
14401440
int64 days;
14411441

14421442
result = 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
}
14481448
else 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
14541454
result = 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

14591459
PG_RETURN_TIMEADT(result);
@@ -1489,14 +1489,14 @@ time_pl_interval(PG_FUNCTION_ARGS)
14891489

14901490
#ifdef HAVE_INT64_TIMESTAMP
14911491
result = (time + span->time);
1492-
result -= (result / INT64CONST(86400000000) * INT64CONST(86400000000));
1492+
result -= (result / USECS_PER_DAY * USECS_PER_DAY);
14931493
if (result < INT64CONST(0))
1494-
result += INT64CONST(86400000000);
1494+
result += USECS_PER_DAY;
14951495
#else
14961496
TimeADT time1;
14971497

14981498
result = (time + span->time);
1499-
TMODULO(result, time1, 86400e0);
1499+
TMODULO(result, time1, (double)SECS_PER_DAY);
15001500
if (result < 0)
15011501
result += 86400;
15021502
#endif
@@ -1516,14 +1516,14 @@ time_mi_interval(PG_FUNCTION_ARGS)
15161516

15171517
#ifdef HAVE_INT64_TIMESTAMP
15181518
result = (time - span->time);
1519-
result -= (result / INT64CONST(86400000000) * INT64CONST(86400000000));
1519+
result -= (result / USECS_PER_DAY * USECS_PER_DAY);
15201520
if (result < INT64CONST(0))
1521-
result += INT64CONST(86400000000);
1521+
result += USECS_PER_DAY;
15221522
#else
15231523
TimeADT time1;
15241524

15251525
result = (time - span->time);
1526-
TMODULO(result, time1, 86400e0);
1526+
TMODULO(result, time1, (double)SECS_PER_DAY);
15271527
if (result < 0)
15281528
result += 86400;
15291529
#endif
@@ -1624,7 +1624,7 @@ time_part(PG_FUNCTION_ARGS)
16241624
{
16251625
case 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
16291629
result = ((tm->tm_sec + fsec) * 1000000);
16301630
#endif
@@ -1641,7 +1641,7 @@ time_part(PG_FUNCTION_ARGS)
16411641

16421642
case 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
16461646
result = (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
17111711
result->time = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
1712-
* INT64CONST(1000000)) + fsec);
1712+
* USECS_PER_SEC) + fsec);
17131713
#else
17141714
result->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
18241824
int64 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
18331833
double trem = time->time;
18341834

@@ -1874,8 +1874,8 @@ timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2)
18741874
int64 t1,
18751875
t2;
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
18801880
double t1,
18811881
t2;
@@ -2026,12 +2026,12 @@ timetz_pl_interval(PG_FUNCTION_ARGS)
20262026

20272027
#ifdef HAVE_INT64_TIMESTAMP
20282028
result->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);
20302030
if (result->time < INT64CONST(0))
2031-
result->time += INT64CONST(86400000000);
2031+
result->time += USECS_PER_DAY;
20322032
#else
20332033
result->time = (time->time + span->time);
2034-
TMODULO(result->time, time1.time, 86400e0);
2034+
TMODULO(result->time, time1.time, (double)SECS_PER_DAY);
20352035
if (result->time < 0)
20362036
result->time += 86400;
20372037
#endif
@@ -2059,12 +2059,12 @@ timetz_mi_interval(PG_FUNCTION_ARGS)
20592059

20602060
#ifdef HAVE_INT64_TIMESTAMP
20612061
result->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);
20632063
if (result->time < INT64CONST(0))
2064-
result->time += INT64CONST(86400000000);
2064+
result->time += USECS_PER_DAY;
20652065
#else
20662066
result->time = (time->time - span->time);
2067-
TMODULO(result->time, time1.time, 86400e0);
2067+
TMODULO(result->time, time1.time, (double)SECS_PER_DAY);
20682068
if (result->time < 0)
20692069
result->time += 86400;
20702070
#endif
@@ -2281,10 +2281,10 @@ datetimetz_timestamptz(PG_FUNCTION_ARGS)
22812281
TimestampTz 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

22902290
PG_RETURN_TIMESTAMP(result);
@@ -2400,7 +2400,7 @@ timetz_part(PG_FUNCTION_ARGS)
24002400

24012401
case 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
24052405
result = ((tm->tm_sec + fsec) * 1000000);
24062406
#endif
@@ -2417,7 +2417,7 @@ timetz_part(PG_FUNCTION_ARGS)
24172417

24182418
case 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
24222422
result = (tm->tm_sec + fsec);
24232423
#endif
@@ -2496,11 +2496,11 @@ timetz_zone(PG_FUNCTION_ARGS)
24962496
{
24972497
tz = 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);
25002500
while (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
25052505
result->time = time->time + (time->zone - tz);
25062506
while (result->time < 0)
@@ -2542,19 +2542,19 @@ timetz_izone(PG_FUNCTION_ARGS)
25422542
PointerGetDatum(zone))))));
25432543

25442544
#ifdef HAVE_INT64_TIMESTAMP
2545-
tz = -(zone->time / INT64CONST(1000000));
2545+
tz = -(zone->time / USECS_PER_SEC);
25462546
#else
25472547
tz = -(zone->time);
25482548
#endif
25492549

25502550
result = (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);
25542554
while (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
25592559
result->time = time->time + (time->zone - tz);
25602560
while (result->time < 0)

src/backend/utils/adt/datetime.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.141 2005/05/23 17:13:14 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.142 2005/05/23 18:56:55 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1210,7 +1210,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
12101210

12111211
tmask |= DTK_TIME_M;
12121212
#ifdef HAVE_INT64_TIMESTAMP
1213-
dt2time(time * INT64CONST(86400000000),
1213+
dt2time(time * USECS_PER_DAY,
12141214
&tm->tm_hour, &tm->tm_min,
12151215
&tm->tm_sec, fsec);
12161216
#else
@@ -1969,7 +1969,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
19691969

19701970
tmask |= DTK_TIME_M;
19711971
#ifdef HAVE_INT64_TIMESTAMP
1972-
dt2time(time * INT64CONST(86400000000),
1972+
dt2time(time * USECS_PER_DAY,
19731973
&tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
19741974
#else
19751975
dt2time(time * 86400,
@@ -2193,7 +2193,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
21932193
#ifdef HAVE_INT64_TIMESTAMP
21942194
if (tm->tm_hour < 0 || tm->tm_hour > 23 || tm->tm_min < 0 ||
21952195
tm->tm_min > 59 || tm->tm_sec < 0 || tm->tm_sec > 60 ||
2196-
*fsec < INT64CONST(0) || *fsec >= INT64CONST(1000000))
2196+
*fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
21972197
return DTERR_FIELD_OVERFLOW;
21982198
#else
21992199
if (tm->tm_hour < 0 || tm->tm_hour > 23 || tm->tm_min < 0 ||
@@ -2447,7 +2447,7 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec)
24472447
#ifdef HAVE_INT64_TIMESTAMP
24482448
if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_min > 59 ||
24492449
tm->tm_sec < 0 || tm->tm_sec > 60 || *fsec < INT64CONST(0) ||
2450-
*fsec >= INT64CONST(1000000))
2450+
*fsec >= USECS_PER_SEC)
24512451
return DTERR_FIELD_OVERFLOW;
24522452
#else
24532453
if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_min > 59 ||
@@ -3222,8 +3222,8 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
32223222
int sec;
32233223

32243224
#ifdef HAVE_INT64_TIMESTAMP
3225-
sec = (*fsec / INT64CONST(1000000));
3226-
*fsec -= (sec * INT64CONST(1000000));
3225+
sec = (*fsec / USECS_PER_SEC);
3226+
*fsec -= (sec * USECS_PER_SEC);
32273227
#else
32283228
TMODULO(*fsec, sec, 1e0);
32293229
#endif

0 commit comments

Comments
 (0)