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

Commit3a3f39f

Browse files
committed
Use macros for time-based constants, rather than constants.
1 parent3d9f7ec commit3a3f39f

File tree

6 files changed

+46
-41
lines changed

6 files changed

+46
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ timetz_zone(PG_FUNCTION_ARGS)
26122612
type=DecodeSpecial(0,lowzone,&val);
26132613

26142614
if (type==TZ||type==DTZ)
2615-
tz=val*60;
2615+
tz=val*MINS_PER_HOUR;
26162616
else
26172617
{
26182618
tzp=pg_tzset(tzname);

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ j2date(int jd, int *year, int *month, int *day)
342342
*year=y-4800;
343343
quad=julian*2141 /65536;
344344
*day=julian-7834*quad /256;
345-
*month= (quad+10) %12+1;
345+
*month= (quad+10) %MONTHS_PER_YEAR+1;
346346

347347
return;
348348
}/* j2date() */
@@ -952,8 +952,8 @@ DecodeDateTime(char **field, int *ftype, int nf,
952952
* DecodeTime()
953953
*/
954954
/* test for > 24:00:00 */
955-
if (tm->tm_hour>24||
956-
(tm->tm_hour==24&&
955+
if (tm->tm_hour>HOURS_PER_DAY||
956+
(tm->tm_hour==HOURS_PER_DAY&&
957957
(tm->tm_min>0||tm->tm_sec>0||*fsec>0)))
958958
returnDTERR_FIELD_OVERFLOW;
959959
break;
@@ -1371,12 +1371,12 @@ DecodeDateTime(char **field, int *ftype, int nf,
13711371
returndterr;
13721372

13731373
/* handle AM/PM */
1374-
if (mer!=HR24&&tm->tm_hour>12)
1374+
if (mer!=HR24&&tm->tm_hour>HOURS_PER_DAY /2)
13751375
returnDTERR_FIELD_OVERFLOW;
1376-
if (mer==AM&&tm->tm_hour==12)
1376+
if (mer==AM&&tm->tm_hour==HOURS_PER_DAY /2)
13771377
tm->tm_hour=0;
1378-
elseif (mer==PM&&tm->tm_hour!=12)
1379-
tm->tm_hour+=12;
1378+
elseif (mer==PM&&tm->tm_hour!=HOURS_PER_DAY /2)
1379+
tm->tm_hour+=HOURS_PER_DAY /2;
13801380

13811381
/* do additional checking for full date specs... */
13821382
if (*dtype==DTK_DATE)
@@ -2058,17 +2058,18 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
20582058
returndterr;
20592059

20602060
/* handle AM/PM */
2061-
if (mer!=HR24&&tm->tm_hour>12)
2061+
if (mer!=HR24&&tm->tm_hour>HOURS_PER_DAY /2)
20622062
returnDTERR_FIELD_OVERFLOW;
2063-
if (mer==AM&&tm->tm_hour==12)
2063+
if (mer==AM&&tm->tm_hour==HOURS_PER_DAY /2)
20642064
tm->tm_hour=0;
2065-
elseif (mer==PM&&tm->tm_hour!=12)
2066-
tm->tm_hour+=12;
2065+
elseif (mer==PM&&tm->tm_hour!=HOURS_PER_DAY /2)
2066+
tm->tm_hour+=HOURS_PER_DAY /2;
20672067

2068-
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>59||
2069-
tm->tm_sec<0||tm->tm_sec>60||tm->tm_hour>24||
2068+
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
2069+
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE||
2070+
tm->tm_hour>HOURS_PER_DAY||
20702071
/* test for > 24:00:00 */
2071-
(tm->tm_hour==24&&
2072+
(tm->tm_hour==HOURS_PER_DAY&&
20722073
(tm->tm_min>0||tm->tm_sec>0||*fsec>0))||
20732074
#ifdefHAVE_INT64_TIMESTAMP
20742075
*fsec<INT64CONST(0)||*fsec>USECS_PER_SEC
@@ -2396,13 +2397,15 @@ DecodeTime(char *str, int fmask, int range,
23962397

23972398
/* do a sanity check */
23982399
#ifdefHAVE_INT64_TIMESTAMP
2399-
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>59||
2400-
tm->tm_sec<0||tm->tm_sec>60||*fsec<INT64CONST(0)||
2400+
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
2401+
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE||
2402+
*fsec<INT64CONST(0)||
24012403
*fsec>USECS_PER_SEC)
24022404
returnDTERR_FIELD_OVERFLOW;
24032405
#else
2404-
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>59||
2405-
tm->tm_sec<0||tm->tm_sec>60||*fsec<0||*fsec>1)
2406+
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
2407+
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE||
2408+
*fsec<0||*fsec>1)
24062409
returnDTERR_FIELD_OVERFLOW;
24072410
#endif
24082411

@@ -2748,9 +2751,9 @@ DecodeTimezone(char *str, int *tzp)
27482751

27492752
if (hr<0||hr>14)
27502753
returnDTERR_TZDISP_OVERFLOW;
2751-
if (min<0||min >=60)
2754+
if (min<0||min >=MINS_PER_HOUR)
27522755
returnDTERR_TZDISP_OVERFLOW;
2753-
if (sec<0||sec >=60)
2756+
if (sec<0||sec >=SECS_PER_MINUTE)
27542757
returnDTERR_TZDISP_OVERFLOW;
27552758

27562759
tz= (hr*MINS_PER_HOUR+min)*SECS_PER_MINUTE+sec;
@@ -3324,7 +3327,7 @@ DecodeISO8601Interval(char *str,
33243327
{
33253328
case'Y':
33263329
tm->tm_year+=val;
3327-
tm->tm_mon+= (fval*12);
3330+
tm->tm_mon+= (fval*MONTHS_PER_YEAR);
33283331
break;
33293332
case'M':
33303333
tm->tm_mon+=val;
@@ -3359,7 +3362,7 @@ DecodeISO8601Interval(char *str,
33593362
returnDTERR_BAD_FORMAT;
33603363

33613364
tm->tm_year+=val;
3362-
tm->tm_mon+= (fval*12);
3365+
tm->tm_mon+= (fval*MONTHS_PER_YEAR);
33633366
if (unit=='\0')
33643367
return0;
33653368
if (unit=='T')
@@ -4155,7 +4158,7 @@ InstallTimeZoneAbbrevs(tzEntry *abbrevs, int n)
41554158
{
41564159
strncpy(newtbl[i].token,abbrevs[i].abbrev,TOKMAXLEN);
41574160
newtbl[i].type=abbrevs[i].is_dst ?DTZ :TZ;
4158-
TOVAL(&newtbl[i],abbrevs[i].offset /60);
4161+
TOVAL(&newtbl[i],abbrevs[i].offset /MINS_PER_HOUR);
41594162
}
41604163

41614164
/* Check the ordering, if testing */

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
21292129
* intervals
21302130
*/
21312131
sprintf(s,"%0*d",S_FM(n->suffix) ?0 :2,
2132-
tm->tm_hour % (HOURS_PER_DAY /2)==0 ?12 :
2132+
tm->tm_hour % (HOURS_PER_DAY /2)==0 ?HOURS_PER_DAY /2 :
21332133
tm->tm_hour % (HOURS_PER_DAY /2));
21342134
if (S_THth(n->suffix))
21352135
str_numth(s,s,S_TH_TYPE(n->suffix));
@@ -2486,14 +2486,14 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
24862486
if (!tm->tm_mon)
24872487
break;
24882488
sprintf(s,"%*s",S_FM(n->suffix) ?0 :-4,
2489-
rm_months_upper[12-tm->tm_mon]);
2489+
rm_months_upper[MONTHS_PER_YEAR-tm->tm_mon]);
24902490
s+=strlen(s);
24912491
break;
24922492
caseDCH_rm:
24932493
if (!tm->tm_mon)
24942494
break;
24952495
sprintf(s,"%*s",S_FM(n->suffix) ?0 :-4,
2496-
rm_months_lower[12-tm->tm_mon]);
2496+
rm_months_lower[MONTHS_PER_YEAR-tm->tm_mon]);
24972497
s+=strlen(s);
24982498
break;
24992499
caseDCH_W:
@@ -2779,12 +2779,12 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
27792779
caseDCH_RM:
27802780
from_char_seq_search(&value,&s,rm_months_upper,
27812781
ALL_UPPER,MAX_RM_LEN,n);
2782-
from_char_set_int(&out->mm,12-value,n);
2782+
from_char_set_int(&out->mm,MONTHS_PER_YEAR-value,n);
27832783
break;
27842784
caseDCH_rm:
27852785
from_char_seq_search(&value,&s,rm_months_lower,
27862786
ALL_LOWER,MAX_RM_LEN,n);
2787-
from_char_set_int(&out->mm,12-value,n);
2787+
from_char_set_int(&out->mm,MONTHS_PER_YEAR-value,n);
27882788
break;
27892789
caseDCH_W:
27902790
from_char_parse_int(&out->w,&s,n);
@@ -3236,16 +3236,16 @@ do_to_timestamp(text *date_txt, text *fmt,
32363236

32373237
if (tmfc.clock==CLOCK_12_HOUR)
32383238
{
3239-
if (tm->tm_hour<1||tm->tm_hour>12)
3239+
if (tm->tm_hour<1||tm->tm_hour>HOURS_PER_DAY /2)
32403240
ereport(ERROR,
32413241
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
32423242
errmsg("hour \"%d\" is invalid for the 12-hour clock",
32433243
tm->tm_hour),
32443244
errhint("Use the 24-hour clock, or give an hour between 1 and 12.")));
32453245

3246-
if (tmfc.pm&&tm->tm_hour<12)
3247-
tm->tm_hour+=12;
3248-
elseif (!tmfc.pm&&tm->tm_hour==12)
3246+
if (tmfc.pm&&tm->tm_hour<HOURS_PER_DAY /2)
3247+
tm->tm_hour+=HOURS_PER_DAY /2;
3248+
elseif (!tmfc.pm&&tm->tm_hour==HOURS_PER_DAY /2)
32493249
tm->tm_hour=0;
32503250
}
32513251

@@ -3347,7 +3347,7 @@ do_to_timestamp(text *date_txt, text *fmt,
33473347

33483348
y=ysum[isleap(tm->tm_year)];
33493349

3350-
for (i=1;i <=12;i++)
3350+
for (i=1;i <=MONTHS_PER_YEAR;i++)
33513351
{
33523352
if (tmfc.ddd<y[i])
33533353
break;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ tm2abstime(struct pg_tm * tm, int tz)
179179

180180
/* validate, before going out of range on some members */
181181
if (tm->tm_year<1901||tm->tm_year>2038||
182-
tm->tm_mon<1||tm->tm_mon>12||
182+
tm->tm_mon<1||tm->tm_mon>MONTHS_PER_YEAR||
183183
tm->tm_mday<1||tm->tm_mday>31||
184184
tm->tm_hour<0||
185-
tm->tm_hour>24||/* test for > 24:00:00 */
186-
(tm->tm_hour==24&& (tm->tm_min>0||tm->tm_sec>0))||
187-
tm->tm_min<0||tm->tm_min>59||
188-
tm->tm_sec<0||tm->tm_sec>60)
185+
tm->tm_hour>HOURS_PER_DAY||/* test for > 24:00:00 */
186+
(tm->tm_hour==HOURS_PER_DAY&& (tm->tm_min>0||tm->tm_sec>0))||
187+
tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
188+
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE)
189189
returnINVALID_ABSTIME;
190190

191191
day=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)-UNIX_EPOCH_JDATE;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4422,7 +4422,7 @@ timestamp_zone(PG_FUNCTION_ARGS)
44224422

44234423
if (type==TZ||type==DTZ)
44244424
{
4425-
tz=-(val*60);
4425+
tz=-(val*MINS_PER_HOUR);
44264426
result=dt2local(timestamp,tz);
44274427
}
44284428
else
@@ -4596,7 +4596,7 @@ timestamptz_zone(PG_FUNCTION_ARGS)
45964596

45974597
if (type==TZ||type==DTZ)
45984598
{
4599-
tz=val*60;
4599+
tz=val*MINS_PER_HOUR;
46004600
result=dt2local(timestamp,tz);
46014601
}
46024602
else

‎src/include/utils/timestamp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ typedef struct
8181
*/
8282
#defineDAYS_PER_MONTH30/* assumes exactly 30 days per month */
8383
#defineHOURS_PER_DAY24/* assume no daylight savings time changes */
84+
#defineMINS_PER_HOUR60/* assume no daylight savings time changes */
85+
#defineSECS_PER_MINUTE60/* assume no daylight savings time changes */
8486

8587
/*
8688
*This doesn't adjust for uneven daylight savings time intervals or leap

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp