JavaScript-Duktape-XS
view release on metacpan or search on metacpan
}
return 1;
}
DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) {
return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS);
}
DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) {
return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY);
}
DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) {
return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR);
}
DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) {
if (!DUK_ISFINITE(x)) {
return DUK_DOUBLE_NAN;
}
if (!duk_bi_date_timeval_in_valid_range(x)) {
return DUK_DOUBLE_NAN;
}
x = duk_js_tointeger_number(x);
/* Here we'd have the option to normalize -0 to +0. */
return x;
}
/* Integer division which floors also negative values correctly. */
DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) {
DUK_ASSERT(b > 0);
if (a >= 0) {
return a / b;
} else {
/* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1
* a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1
* a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2
*/
return (a - b + 1) / b;
}
}
/* Compute day number of the first day of a given year. */
DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) {
/* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative
* values, but is incorrect for negative ones.
*/
return 365 * (year - 1970) + duk__div_floor(year - 1969, 4) - duk__div_floor(year - 1901, 100) +
duk__div_floor(year - 1601, 400);
}
/* Given a day number, determine year and day-within-year. */
DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) {
duk_int_t year;
duk_int_t diff_days;
/* estimate year upwards (towards positive infinity), then back down;
* two iterations should be enough
*/
if (day >= 0) {
year = 1970 + day / 365;
} else {
year = 1970 + day / 366;
}
for (;;) {
diff_days = duk__day_from_year(year) - day;
DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days));
if (diff_days <= 0) {
DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */
*out_day_within_year = -diff_days;
DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld", (long) year, (long) *out_day_within_year));
DUK_ASSERT(*out_day_within_year >= 0);
DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365));
return year;
}
/* Note: this is very tricky; we must never 'overshoot' the
* correction downwards.
*/
year -= 1 + (diff_days - 1) / 366; /* conservative */
}
}
/* Given a (year, month, day-within-month) triple, compute day number.
* The input triple is un-normalized and may contain non-finite values.
*/
DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) {
duk_int_t day_num;
duk_bool_t is_leap;
duk_small_int_t i, n;
/* Assume that year, month, day are all coerced to whole numbers.
* They may also be NaN or infinity, in which case this function
* must return NaN or infinity to ensure time value becomes NaN.
* If 'day' is NaN, the final return will end up returning a NaN,
* so it doesn't need to be checked here.
*/
if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) {
return DUK_DOUBLE_NAN;
}
year += DUK_FLOOR(month / 12.0);
month = DUK_FMOD(month, 12.0);
if (month < 0.0) {
/* handle negative values */
month += 12.0;
}
/* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but
* does not normalize the day-of-month (nor check whether or not
* it is finite) because it's not necessary for finding the day
* number which matches the (year,month) pair.
*
* We assume that duk__day_from_year() is exact here.
( run in 0.932 second using v1.01-cache-2.11-cpan-71847e10f99 )