Skip to content

Commit 4550c1e

Browse files
committed
More macro cleanups for date/time.
1 parent 5ebaae8 commit 4550c1e

File tree

11 files changed

+98
-88
lines changed

11 files changed

+98
-88
lines changed

src/backend/utils/adt/date.c

Lines changed: 15 additions & 15 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.106 2005/05/23 18:56:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.107 2005/05/23 21:54:01 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1339,8 +1339,8 @@ timestamp_time(PG_FUNCTION_ARGS)
13391339
#ifdef HAVE_INT64_TIMESTAMP
13401340

13411341
/*
1342-
* Could also do this with time = (timestamp / 86400000000 *
1343-
* 86400000000) - timestamp;
1342+
* Could also do this with time = (timestamp / USECS_PER_DAY *
1343+
* USECS_PER_DAY) - timestamp;
13441344
*/
13451345
result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
13461346
* USECS_PER_SEC) + fsec);
@@ -1376,8 +1376,8 @@ timestamptz_time(PG_FUNCTION_ARGS)
13761376
#ifdef HAVE_INT64_TIMESTAMP
13771377

13781378
/*
1379-
* Could also do this with time = (timestamp / 86400000000 *
1380-
* 86400000000) - timestamp;
1379+
* Could also do this with time = (timestamp / USECS_PER_DAY *
1380+
* USECS_PER_DAY) - timestamp;
13811381
*/
13821382
result = ((((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec)
13831383
* USECS_PER_SEC) + fsec);
@@ -1498,7 +1498,7 @@ time_pl_interval(PG_FUNCTION_ARGS)
14981498
result = (time + span->time);
14991499
TMODULO(result, time1, (double)SECS_PER_DAY);
15001500
if (result < 0)
1501-
result += 86400;
1501+
result += SECS_PER_DAY;
15021502
#endif
15031503

15041504
PG_RETURN_TIMEADT(result);
@@ -1525,7 +1525,7 @@ time_mi_interval(PG_FUNCTION_ARGS)
15251525
result = (time - span->time);
15261526
TMODULO(result, time1, (double)SECS_PER_DAY);
15271527
if (result < 0)
1528-
result += 86400;
1528+
result += SECS_PER_DAY;
15291529
#endif
15301530

15311531
PG_RETURN_TIMEADT(result);
@@ -2033,7 +2033,7 @@ timetz_pl_interval(PG_FUNCTION_ARGS)
20332033
result->time = (time->time + span->time);
20342034
TMODULO(result->time, time1.time, (double)SECS_PER_DAY);
20352035
if (result->time < 0)
2036-
result->time += 86400;
2036+
result->time += SECS_PER_DAY;
20372037
#endif
20382038

20392039
result->zone = time->zone;
@@ -2066,7 +2066,7 @@ timetz_mi_interval(PG_FUNCTION_ARGS)
20662066
result->time = (time->time - span->time);
20672067
TMODULO(result->time, time1.time, (double)SECS_PER_DAY);
20682068
if (result->time < 0)
2069-
result->time += 86400;
2069+
result->time += SECS_PER_DAY;
20702070
#endif
20712071

20722072
result->zone = time->zone;
@@ -2504,9 +2504,9 @@ timetz_zone(PG_FUNCTION_ARGS)
25042504
#else
25052505
result->time = time->time + (time->zone - tz);
25062506
while (result->time < 0)
2507-
result->time += 86400;
2508-
while (result->time >= 86400)
2509-
result->time -= 86400;
2507+
result->time += SECS_PER_DAY;
2508+
while (result->time >= SECS_PER_DAY)
2509+
result->time -= SECS_PER_DAY;
25102510
#endif
25112511

25122512
result->zone = tz;
@@ -2558,9 +2558,9 @@ timetz_izone(PG_FUNCTION_ARGS)
25582558
#else
25592559
result->time = time->time + (time->zone - tz);
25602560
while (result->time < 0)
2561-
result->time += 86400;
2562-
while (result->time >= 86400)
2563-
result->time -= 86400;
2561+
result->time += SECS_PER_DAY;
2562+
while (result->time >= SECS_PER_DAY)
2563+
result->time -= SECS_PER_DAY;
25642564
#endif
25652565

25662566
result->zone = tz;

src/backend/utils/adt/datetime.c

Lines changed: 8 additions & 8 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.142 2005/05/23 18:56:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.143 2005/05/23 21:54:01 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1214,7 +1214,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
12141214
&tm->tm_hour, &tm->tm_min,
12151215
&tm->tm_sec, fsec);
12161216
#else
1217-
dt2time(time * 86400, &tm->tm_hour,
1217+
dt2time(time * SECS_PER_DAY, &tm->tm_hour,
12181218
&tm->tm_min, &tm->tm_sec, fsec);
12191219
#endif
12201220
}
@@ -1611,8 +1611,8 @@ DetermineLocalTimeZone(struct pg_tm * tm)
16111611
goto overflow;
16121612
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - UNIX_EPOCH_JDATE;
16131613

1614-
day = ((pg_time_t) date) *86400;
1615-
if (day / 86400 != date)
1614+
day = ((pg_time_t) date) *SECS_PER_DAY;
1615+
if (day / SECS_PER_DAY != date)
16161616
goto overflow;
16171617
sec = tm->tm_sec + (tm->tm_min + tm->tm_hour * 60) * 60;
16181618
mytime = day + sec;
@@ -1972,7 +1972,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
19721972
dt2time(time * USECS_PER_DAY,
19731973
&tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
19741974
#else
1975-
dt2time(time * 86400,
1975+
dt2time(time * SECS_PER_DAY,
19761976
&tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
19771977
#endif
19781978
}
@@ -3099,7 +3099,7 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
30993099
{
31003100
int sec;
31013101

3102-
fval *= 86400;
3102+
fval *= SECS_PER_DAY;
31033103
sec = fval;
31043104
tm->tm_sec += sec;
31053105
#ifdef HAVE_INT64_TIMESTAMP
@@ -3117,7 +3117,7 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
31173117
{
31183118
int sec;
31193119

3120-
fval *= (7 * 86400);
3120+
fval *= (7 * SECS_PER_DAY);
31213121
sec = fval;
31223122
tm->tm_sec += sec;
31233123
#ifdef HAVE_INT64_TIMESTAMP
@@ -3135,7 +3135,7 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
31353135
{
31363136
int sec;
31373137

3138-
fval *= (30 * 86400);
3138+
fval *= (30 * SECS_PER_DAY);
31393139
sec = fval;
31403140
tm->tm_sec += sec;
31413141
#ifdef HAVE_INT64_TIMESTAMP

src/backend/utils/adt/nabstime.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.129 2005/05/23 18:56:55 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.130 2005/05/23 21:54:02 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -129,10 +129,10 @@ AbsoluteTimeUsecToTimestampTz(AbsoluteTime sec, int usec)
129129
TimestampTz result;
130130

131131
#ifdef HAVE_INT64_TIMESTAMP
132-
result = ((sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * 86400))
132+
result = ((sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY))
133133
* USECS_PER_SEC) + usec;
134134
#else
135-
result = sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * 86400)
135+
result = sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY)
136136
+ (usec / 1000000.0);
137137
#endif
138138

@@ -730,7 +730,7 @@ reltimein(PG_FUNCTION_ARGS)
730730
{
731731
case DTK_DELTA:
732732
result = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec);
733-
result += ((tm->tm_year * 36525 * 864) + (((tm->tm_mon * 30) + tm->tm_mday) * 86400));
733+
result += ((tm->tm_year * 36525 * 864) + (((tm->tm_mon * 30) + tm->tm_mday) * SECS_PER_DAY));
734734
break;
735735

736736
default:
@@ -795,7 +795,7 @@ reltime2tm(RelativeTime time, struct pg_tm * tm)
795795

796796
FMODULO(dtime, tm->tm_year, 31557600);
797797
FMODULO(dtime, tm->tm_mon, 2592000);
798-
FMODULO(dtime, tm->tm_mday, 86400);
798+
FMODULO(dtime, tm->tm_mday, SECS_PER_DAY);
799799
FMODULO(dtime, tm->tm_hour, 3600);
800800
FMODULO(dtime, tm->tm_min, 60);
801801
FMODULO(dtime, tm->tm_sec, 1);
@@ -947,10 +947,10 @@ interval_reltime(PG_FUNCTION_ARGS)
947947

948948
#ifdef HAVE_INT64_TIMESTAMP
949949
span = ((((INT64CONST(365250000) * year) + (INT64CONST(30000000) * month))
950-
* INT64CONST(86400)) + interval->time);
950+
* INT64CONST(SECS_PER_DAY)) + interval->time);
951951
span /= USECS_PER_SEC;
952952
#else
953-
span = (((((double) 365.25 * year) + ((double) 30 * month)) * 86400) + interval->time);
953+
span = (((((double) 365.25 * year) + ((double) 30 * month)) * SECS_PER_DAY) + interval->time);
954954
#endif
955955

956956
if ((span < INT_MIN) || (span > INT_MAX))
@@ -986,13 +986,13 @@ reltime_interval(PG_FUNCTION_ARGS)
986986
#ifdef HAVE_INT64_TIMESTAMP
987987
year = (reltime / (36525 * 864));
988988
reltime -= (year * (36525 * 864));
989-
month = (reltime / (30 * 86400));
990-
reltime -= (month * (30 * 86400));
989+
month = (reltime / (30 * SECS_PER_DAY));
990+
reltime -= (month * (30 * SECS_PER_DAY));
991991

992992
result->time = (reltime * USECS_PER_SEC);
993993
#else
994994
TMODULO(reltime, year, (36525 * 864));
995-
TMODULO(reltime, month, (30 * 86400));
995+
TMODULO(reltime, month, (30 * SECS_PER_DAY));
996996

997997
result->time = reltime;
998998
#endif

src/backend/utils/adt/timestamp.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.121 2005/05/23 18:56:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.122 2005/05/23 21:54:02 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -699,7 +699,7 @@ AdjustIntervalForTypmod(Interval *interval, int32 typmod)
699699
USECS_PER_DAY;
700700

701701
#else
702-
interval->time = ((int) (interval->time / 86400)) * 86400;
702+
interval->time = ((int) (interval->time / SECS_PER_DAY)) * SECS_PER_DAY;
703703
#endif
704704
}
705705
else if (range == INTERVAL_MASK(HOUR))
@@ -1021,7 +1021,7 @@ timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, char **tzn
10211021

10221022
if (time < 0)
10231023
{
1024-
time += 86400;
1024+
time += SECS_PER_DAY;
10251025
date -=1;
10261026
}
10271027
#endif
@@ -1074,10 +1074,10 @@ timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, char **tzn
10741074
*/
10751075
#ifdef HAVE_INT64_TIMESTAMP
10761076
dt = (dt - *fsec) / USECS_PER_SEC +
1077-
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * 86400;
1077+
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
10781078
#else
10791079
dt = rint(dt - *fsec +
1080-
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * 86400);
1080+
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
10811081
#endif
10821082
utime = (pg_time_t) dt;
10831083
if ((Timestamp) utime == dt)
@@ -1151,7 +1151,7 @@ tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
11511151
(*result >= 0 && date < 0))
11521152
return -1;
11531153
#else
1154-
*result = date * 86400 + time;
1154+
*result = date * SECS_PER_DAY + time;
11551155
#endif
11561156
if (tzp != NULL)
11571157
*result = dt2local(*result, -(*tzp));
@@ -1621,9 +1621,9 @@ interval_cmp_internal(Interval *interval1, Interval *interval2)
16211621
span2 += interval2->month * INT64CONST(30) * USECS_PER_DAY;
16221622
#else
16231623
if (interval1->month != 0)
1624-
span1 += interval1->month * (30.0 * 86400);
1624+
span1 += interval1->month * (30.0 * SECS_PER_DAY);
16251625
if (interval2->month != 0)
1626-
span2 += interval2->month * (30.0 * 86400);
1626+
span2 += interval2->month * (30.0 * SECS_PER_DAY);
16271627
#endif
16281628

16291629
return ((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0);
@@ -2166,7 +2166,7 @@ interval_mul(PG_FUNCTION_ARGS)
21662166
result->month = rint(months);
21672167
result->time = JROUND(span1->time * factor);
21682168
/* evaluate fractional months as 30 days */
2169-
result->time += JROUND((months - result->month) * 30 * 86400);
2169+
result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);
21702170
#endif
21712171

21722172
PG_RETURN_INTERVAL_P(result);
@@ -2211,7 +2211,7 @@ interval_div(PG_FUNCTION_ARGS)
22112211
result->month = rint(months);
22122212
result->time = JROUND(span->time / factor);
22132213
/* evaluate fractional months as 30 days */
2214-
result->time += JROUND((months - result->month) * 30 * 86400);
2214+
result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);
22152215
#endif
22162216

22172217
PG_RETURN_INTERVAL_P(result);
@@ -3788,8 +3788,8 @@ interval_part(PG_FUNCTION_ARGS)
37883788
#endif
37893789
if (interval->month != 0)
37903790
{
3791-
result += (365.25 * 86400) * (interval->month / 12);
3792-
result += (30.0 * 86400) * (interval->month % 12);
3791+
result += (365.25 * SECS_PER_DAY) * (interval->month / 12);
3792+
result += (30.0 * SECS_PER_DAY) * (interval->month % 12);
37933793
}
37943794
}
37953795
else

src/include/utils/timestamp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.41 2005/05/23 18:56:55 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.42 2005/05/23 21:54:02 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -60,10 +60,12 @@ typedef struct
6060
#define MAX_INTERVAL_PRECISION 6
6161

6262
#define SECS_PER_DAY 86400
63+
#ifdef HAVE_INT64_TIMESTAMP
6364
#define USECS_PER_DAY INT64CONST(86400000000)
6465
#define USECS_PER_HOUR INT64CONST(3600000000)
6566
#define USECS_PER_MINUTE INT64CONST(60000000)
6667
#define USECS_PER_SEC INT64CONST(1000000)
68+
#endif
6769

6870
/*
6971
* Macros for fmgr-callable functions.

src/interfaces/ecpg/pgtypeslib/datetime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ PGTYPESdate_from_timestamp(timestamp dt)
2222

2323
#ifdef HAVE_INT64_TIMESTAMP
2424
/* Microseconds to days */
25-
dDate = (dt / INT64CONST(86400000000));
25+
dDate = (dt / USECS_PER_DAY);
2626
#else
2727
/* Seconds to days */
28-
dDate = (dt / 86400.0);
28+
dDate = (dt / (double)SECS_PER_DAY);
2929
#endif
3030

3131
return dDate;

src/interfaces/ecpg/pgtypeslib/dt.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ do { \
216216
} while(0)
217217
#endif
218218

219+
#define SECS_PER_DAY 86400
220+
#ifdef HAVE_INT64_TIMESTAMP
221+
#define USECS_PER_DAY INT64CONST(86400000000)
222+
#define USECS_PER_HOUR INT64CONST(3600000000)
223+
#define USECS_PER_MINUTE INT64CONST(60000000)
224+
#define USECS_PER_SEC INT64CONST(1000000)
225+
#endif
226+
219227
/*
220228
* Date/time validation
221229
* Include check for leap year.

0 commit comments

Comments
 (0)