diff --git a/icu4c/source/i18n/utmscale.c b/icu4c/source/i18n/utmscale.c index a07f0a570d..e8484b783b 100644 --- a/icu4c/source/i18n/utmscale.c +++ b/icu4c/source/i18n/utmscale.c @@ -83,6 +83,7 @@ U_CAPI int64_t U_EXPORT2 utmscale_fromDouble(double otherTime, UDateTimeScale timeScale, UErrorCode *status) { const InternalTimeScaleData *data; + double fromMin, fromMax; if (status == NULL || U_FAILURE(*status)) { return 0; @@ -94,8 +95,10 @@ utmscale_fromDouble(double otherTime, UDateTimeScale timeScale, UErrorCode *stat } data = &timeScaleTable[timeScale]; + fromMin = (double) data->fromMin; + fromMax = (double) data->fromMax; - if (otherTime < data->fromMin || otherTime > data->fromMax) { + if (!(otherTime >= fromMin && otherTime <= fromMax)) { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } diff --git a/icu4c/source/test/cintltst/utmstest.c b/icu4c/source/test/cintltst/utmstest.c index d3ffb9e998..2578ae1380 100644 --- a/icu4c/source/test/cintltst/utmstest.c +++ b/icu4c/source/test/cintltst/utmstest.c @@ -141,6 +141,16 @@ static void epochOffsetTest(int64_t epochOffset, int64_t units, int32_t scale) log_err("utmscale_fromInt64(0, scale): scale = %d, result = %lld.\n", scale, universal); } } + +static double valueLessThan(double value) +{ + return value - 100000.0; +} + +static double valueGreaterThan(double value) +{ + return value + 100000.0; +} static void TestEpochOffsets(void) { @@ -219,13 +229,10 @@ static void TestFromDouble(void) log_err("utmscale_fromDouble(fromMin, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); } - /* only do this test if we can exactly represent fromMin - 1 */ - if (fromMin - 1 < fromMin) { - status = U_ZERO_ERROR; - result = utmscale_fromDouble((double) data.fromMin - 1, scale, &status); - if (status != U_ILLEGAL_ARGUMENT_ERROR) { - log_err("utmscale_fromDouble(fromMin - 1, %d, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); - } + status = U_ZERO_ERROR; + result = utmscale_fromDouble(valueLessThan(fromMin), scale, &status); + if (status != U_ILLEGAL_ARGUMENT_ERROR) { + log_err("utmscale_fromDouble(fromMin - 1, %d, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); } status = U_ZERO_ERROR; @@ -234,13 +241,10 @@ static void TestFromDouble(void) log_err("utmscale_fromDouble(fromMax, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); } - /* Only do this test if we can exactly represent fromMax + 1 */ - if (fromMax < fromMax + 1) { - status = U_ZERO_ERROR; - result = utmscale_fromDouble((double) data.fromMax + 1, scale, &status); - if (status != U_ILLEGAL_ARGUMENT_ERROR) { - log_err("utmscale_fromDouble(fromMax + 1, %d, &status) didn't generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); - } + status = U_ZERO_ERROR; + result = utmscale_fromDouble(valueGreaterThan(fromMax), scale, &status); + if (status != U_ILLEGAL_ARGUMENT_ERROR) { + log_err("utmscale_fromDouble(fromMax + 1, %d, &status) didn't generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); } }