47 #error -ffast-math is known to break this code
50 #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
88 #define IA_TOTAL_COUNT(ia) \
89 ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)
116 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
117 errmsg(
"invalid type modifier")));
128 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
129 errmsg(
"TIMESTAMP(%d)%s precision must not be negative",
130 typmod, (istz ?
" WITH TIME ZONE" :
""))));
134 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
135 errmsg(
"TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
136 typmod, (istz ?
" WITH TIME ZONE" :
""),
148 const char *tz = istz ?
" with time zone" :
" without time zone";
151 return psprintf(
"(%d)%s", (
int) typmod, tz);
172 Node *escontext = fcinfo->context;
190 &dtype,
tm, &fsec, &tz, &extra);
202 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
203 errmsg(
"timestamp out of range: \"%s\"",
str)));
219 elog(
ERROR,
"unexpected dtype %d while parsing timestamp \"%s\"",
248 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
249 errmsg(
"timestamp out of range")));
280 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
281 errmsg(
"timestamp out of range")));
394 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
395 errmsg(
"timestamp(%d) precision must be between %d and %d",
400 *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
401 TimestampScales[typmod];
405 *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
406 * TimestampScales[typmod]);
424 Node *escontext = fcinfo->context;
442 &dtype,
tm, &fsec, &tz, &extra);
455 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
456 errmsg(
"timestamp out of range: \"%s\"",
str)));
472 elog(
ERROR,
"unexpected dtype %d while parsing timestamptz \"%s\"",
508 if (isdigit((
unsigned char) *tzname))
510 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
511 errmsg(
"invalid input syntax for type %s: \"%s\"",
512 "numeric time zone", tzname),
513 errhint(
"Numeric time zones must have \"-\" or \"+\" as first character.")));
524 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
525 errmsg(
"numeric time zone \"%s\" out of range", tzname)));
528 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
529 errmsg(
"time zone \"%s\" not recognized", tzname)));
574 int hour,
int min,
double sec)
598 (
errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
599 errmsg(
"date field value out of range: %d-%02d-%02d",
604 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
605 errmsg(
"date out of range: %d-%02d-%02d",
613 (
errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
614 errmsg(
"time field value out of range: %d:%02d:%02g",
624 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
625 errmsg(
"timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
632 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
633 errmsg(
"timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
705 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
706 errmsg(
"timestamp out of range")));
714 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
715 errmsg(
"timestamp out of range")));
733 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
734 errmsg(
"timestamp cannot be NaN")));
751 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
752 errmsg(
"timestamp out of range: \"%g\"", seconds)));
758 result = (
int64) seconds;
763 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
764 errmsg(
"timestamp out of range: \"%g\"",
792 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
793 errmsg(
"timestamp out of range")));
825 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
826 errmsg(
"timestamp out of range")));
897 Node *escontext = fcinfo->context;
946 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
947 errmsg(
"interval out of range")));
959 elog(
ERROR,
"unexpected dtype %d while parsing interval \"%s\"",
1083 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1084 errmsg(
"invalid INTERVAL type modifier")));
1099 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1100 errmsg(
"INTERVAL(%d) precision must not be negative",
1105 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1106 errmsg(
"INTERVAL(%d) precision reduced to maximum allowed, %d",
1116 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1117 errmsg(
"invalid INTERVAL type modifier")));
1131 const char *fieldstr;
1148 fieldstr =
" month";
1157 fieldstr =
" minute";
1160 fieldstr =
" second";
1163 fieldstr =
" year to month";
1166 fieldstr =
" day to hour";
1169 fieldstr =
" day to minute";
1172 fieldstr =
" day to second";
1175 fieldstr =
" hour to minute";
1178 fieldstr =
" hour to second";
1181 fieldstr =
" minute to second";
1187 elog(
ERROR,
"invalid INTERVAL typmod: 0x%x", typmod);
1247 elog(
ERROR,
"invalid INTERVAL typmod: 0x%x", typmod);
1290 int old_least_field;
1291 int new_least_field;
1309 noop = (new_least_field <= old_least_field) &&
1310 (old_least_field > 0 ||
1312 new_precis >= old_precis);
1488 elog(
ERROR,
"unrecognized interval typmod: %d", typmod);
1495 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1496 errmsg(
"interval(%d) precision must be between %d and %d",
1502 IntervalOffsets[precision],
1505 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1506 errmsg(
"interval out of range")));
1512 IntervalOffsets[precision],
1515 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1516 errmsg(
"interval out of range")));
1544 if (isinf(secs) || isnan(secs))
1576 errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1577 errmsg(
"interval out of range"));
1593 elog(
ERROR,
"invalid argument for EncodeSpecialTimestamp");
1604 elog(
ERROR,
"invalid argument for EncodeSpecialInterval");
1721 long *secs,
int *microsecs)
1765 return (
long) INT_MAX;
1766 if (diff >= (INT_MAX *
INT64CONST(1000) - 999))
1767 return (
long) INT_MAX;
1769 return (
long) ((diff + 999) / 1000);
1898 if (attimezone == NULL)
2009 *result =
dt2local(*result, -(*tzp));
2043 itm->
tm_min = (int) tfrac;
2046 itm->
tm_sec = (int) tfrac;
2062 if (total_months > INT_MAX || total_months < INT_MIN)
2100 if (total_months > INT_MAX || total_months < INT_MIN)
2157 elog(
ERROR,
"could not convert epoch to timestamp: %m");
2193 return (dt1 < dt2) ? -1 : ((dt1 > dt2) ? 1 : 0);
2259 #if SIZEOF_DATUM < 8
2276 #if SIZEOF_DATUM >= 8
2640 #define TIMESTAMP_GT(t1,t2) \
2641 DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2642 #define TIMESTAMP_LT(t1,t2) \
2643 DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2658 else if (!te1IsNull)
2678 else if (!te2IsNull)
2734 if (te1IsNull || te2IsNull)
2799 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2800 errmsg(
"interval out of range")));
2808 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2809 errmsg(
"interval out of range")));
2823 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2824 errmsg(
"interval out of range")));
2890 if ((result->
day > 0 && result->
time > 0) ||
2891 (result->
day < 0 && result->
time < 0))
2897 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2898 errmsg(
"interval out of range")));
2908 result->
day += wholeday;
2914 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2915 errmsg(
"interval out of range")));
2917 if (result->
month > 0 &&
2918 (result->
day < 0 || (result->
day == 0 && result->
time < 0)))
2923 else if (result->
month < 0 &&
2924 (result->
day > 0 || (result->
day == 0 && result->
time > 0)))
2930 if (result->
day > 0 && result->
time < 0)
2935 else if (result->
day < 0 && result->
time > 0)
2971 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2972 errmsg(
"interval out of range")));
2974 if (result->
day > 0 && result->
time < 0)
2979 else if (result->
day < 0 && result->
time > 0)
3014 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3015 errmsg(
"interval out of range")));
3017 if (result->
month > 0 && result->
day < 0)
3022 else if (result->
month < 0 && result->
day > 0)
3058 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3059 errmsg(
"timestamp out of range")));
3067 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3068 errmsg(
"timestamp out of range")));
3076 if (span->
month != 0)
3084 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3085 errmsg(
"timestamp out of range")));
3089 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3090 errmsg(
"timestamp out of range")));
3108 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3109 errmsg(
"timestamp out of range")));
3121 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3122 errmsg(
"timestamp out of range")));
3132 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3133 errmsg(
"timestamp out of range")));
3138 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3139 errmsg(
"timestamp out of range")));
3144 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3145 errmsg(
"timestamp out of range")));
3149 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3150 errmsg(
"timestamp out of range")));
3202 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3203 errmsg(
"timestamp out of range")));
3211 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3212 errmsg(
"timestamp out of range")));
3221 if (attimezone == NULL)
3224 if (span->
month != 0)
3232 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3233 errmsg(
"timestamp out of range")));
3237 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3238 errmsg(
"timestamp out of range")));
3258 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3259 errmsg(
"timestamp out of range")));
3271 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3272 errmsg(
"timestamp out of range")));
3285 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3286 errmsg(
"timestamp out of range")));
3293 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3294 errmsg(
"timestamp out of range")));
3299 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3300 errmsg(
"timestamp out of range")));
3304 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3305 errmsg(
"timestamp out of range")));
3392 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3393 errmsg(
"interval out of range")));
3450 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3451 errmsg(
"interval out of range")));
3473 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3474 errmsg(
"interval out of range")));
3482 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3483 errmsg(
"interval out of range")));
3488 memcpy(result, span2,
sizeof(
Interval));
3506 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3507 errmsg(
"interval out of range")));
3529 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3530 errmsg(
"interval out of range")));
3538 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3539 errmsg(
"interval out of range")));
3564 double month_remainder_days,
3568 orig_day = span->
day;
3590 memcpy(result, span,
sizeof(
Interval));
3601 if (factor * isign < 0)
3609 result_double = span->
month * factor;
3614 result_double = span->
day * factor;
3617 result->
day = (
int32) result_double;
3638 month_remainder_days =
TSROUND(month_remainder_days);
3639 sec_remainder = (orig_day * factor - result->
day +
3640 month_remainder_days - (int) month_remainder_days) *
SECS_PER_DAY;
3641 sec_remainder =
TSROUND(sec_remainder);
3673 errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3674 errmsg(
"interval out of range"));
3694 double month_remainder_days,
3698 orig_day = span->
day;
3705 (
errcode(ERRCODE_DIVISION_BY_ZERO),
3706 errmsg(
"division by zero")));
3726 memcpy(result, span,
sizeof(
Interval));
3731 result_double = span->
month / factor;
3736 result_double = span->
day / factor;
3739 result->
day = (
int32) result_double;
3745 month_remainder_days =
TSROUND(month_remainder_days);
3746 sec_remainder = (orig_day / factor - result->
day +
3747 month_remainder_days - (int) month_remainder_days) *
SECS_PER_DAY;
3748 sec_remainder =
TSROUND(sec_remainder);
3774 errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3775 errmsg(
"interval out of range"));
3802 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3803 errmsg(
"invalid preceding or following size in window function")));
3839 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3840 errmsg(
"invalid preceding or following size in window function")));
3880 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3881 errmsg(
"invalid preceding or following size in window function")));
3926 elog(
ERROR,
"aggregate function called in non-aggregate context");
3987 memset(&
state->sumX, 0,
sizeof(
state->sumX));
4034 state1->
N = state2->
N;
4045 state1->
N += state2->
N;
4069 elog(
ERROR,
"aggregate function called in non-aggregate context");
4106 elog(
ERROR,
"aggregate function called in non-aggregate context");
4150 elog(
ERROR,
"interval_avg_accum_inv called with NULL state");
4175 if (
state->pInfcount > 0 ||
state->nInfcount > 0)
4179 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
4181 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4182 errmsg(
"interval out of range")));
4185 if (
state->pInfcount > 0)
4216 if (
state->pInfcount > 0 &&
state->nInfcount > 0)
4218 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4219 errmsg(
"interval out of range")));
4223 if (
state->pInfcount > 0)
4225 else if (
state->nInfcount > 0)
4266 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4267 errmsg(
"interval out of range")));
4275 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4276 errmsg(
"interval out of range")));
4284 else if (
timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
4288 tm->tm_usec = fsec1 - fsec2;
4299 tm->tm_usec = -
tm->tm_usec;
4309 while (
tm->tm_usec < 0)
4356 tm->tm_usec = -
tm->tm_usec;
4367 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4368 errmsg(
"interval out of range")));
4372 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4373 errmsg(
"timestamp out of range")));
4414 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4415 errmsg(
"interval out of range")));
4423 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4424 errmsg(
"interval out of range")));
4432 else if (
timestamp2tm(dt1, &tz1, tm1, &fsec1, NULL, NULL) == 0 &&
4436 tm->tm_usec = fsec1 - fsec2;
4447 tm->tm_usec = -
tm->tm_usec;
4457 while (
tm->tm_usec < 0)
4508 tm->tm_usec = -
tm->tm_usec;
4519 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4520 errmsg(
"interval out of range")));
4524 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4525 errmsg(
"timestamp out of range")));
4556 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4557 errmsg(
"origin out of range")));
4561 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4562 errmsg(
"timestamps cannot be binned into infinite intervals")));
4564 if (stride->
month != 0)
4566 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4567 errmsg(
"timestamps cannot be binned into intervals containing months or years")));
4572 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4573 errmsg(
"interval out of range")));
4575 if (stride_usecs <= 0)
4577 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4578 errmsg(
"stride must be greater than zero")));
4582 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4583 errmsg(
"interval out of range")));
4586 tm_modulo = tm_diff % stride_usecs;
4587 tm_delta = tm_diff - tm_modulo;
4588 result = origin + tm_delta;
4600 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4601 errmsg(
"timestamp out of range")));
4636 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4637 errmsg(
"timestamp out of range")));
4710 fsec = (fsec / 1000) * 1000;
4718 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4719 errmsg(
"unit \"%s\" not supported for type %s",
4726 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4727 errmsg(
"timestamp out of range")));
4732 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4733 errmsg(
"unit \"%s\" not recognized for type %s",
4761 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4762 errmsg(
"origin out of range")));
4766 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4767 errmsg(
"timestamps cannot be binned into infinite intervals")));
4769 if (stride->
month != 0)
4771 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4772 errmsg(
"timestamps cannot be binned into intervals containing months or years")));
4777 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4778 errmsg(
"interval out of range")));
4780 if (stride_usecs <= 0)
4782 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4783 errmsg(
"stride must be greater than zero")));
4787 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4788 errmsg(
"interval out of range")));
4791 tm_modulo = tm_diff % stride_usecs;
4792 tm_delta = tm_diff - tm_modulo;
4793 result = origin + tm_delta;
4805 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4806 errmsg(
"timestamp out of range")));
4825 bool redotz =
false;
4841 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4842 errmsg(
"timestamp out of range")));
4926 fsec = (fsec / 1000) * 1000;
4933 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4934 errmsg(
"unit \"%s\" not supported for type %s",
4944 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4945 errmsg(
"timestamp out of range")));
4950 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4951 errmsg(
"unit \"%s\" not recognized for type %s",
5074 tm->tm_usec = (
tm->tm_usec / 1000) * 1000;
5081 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5082 errmsg(
"unit \"%s\" not supported for type %s",
5089 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5090 errmsg(
"interval out of range")));
5095 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5096 errmsg(
"unit \"%s\" not recognized for type %s",
5119 day4 =
date2j(year, 1, 4);
5122 day0 =
j2day(day4 - 1);
5124 return ((week - 1) * 7) + (day4 - day0);
5156 j2date(jday, year, mon, mday);
5172 dayn =
date2j(year, mon, mday);
5175 day4 =
date2j(year, 1, 4);
5178 day0 =
j2day(day4 - 1);
5184 if (dayn < day4 - day0)
5186 day4 =
date2j(year - 1, 1, 4);
5189 day0 =
j2day(day4 - 1);
5192 result = (dayn - (day4 - day0)) / 7 + 1;
5200 day4 =
date2j(year + 1, 1, 4);
5203 day0 =
j2day(day4 - 1);
5205 if (dayn >= day4 - day0)
5206 result = (dayn - (day4 - day0)) / 7 + 1;
5209 return (
int) result;
5227 dayn =
date2j(year, mon, mday);
5230 day4 =
date2j(year, 1, 4);
5233 day0 =
j2day(day4 - 1);
5239 if (dayn < day4 - day0)
5241 day4 =
date2j(year - 1, 1, 4);
5244 day0 =
j2day(day4 - 1);
5249 result = (dayn - (day4 - day0)) / 7 + 1;
5257 day4 =
date2j(year + 1, 1, 4);
5260 day0 =
j2day(day4 - 1);
5262 if (dayn >= day4 - day0)