diff --git a/intl/components/src/TimeZone.h b/intl/components/src/TimeZone.h index 5150293b7bc20..dfed4ff35758e 100644 --- a/intl/components/src/TimeZone.h +++ b/intl/components/src/TimeZone.h @@ -143,6 +143,17 @@ class TimeZone final { return FillBufferWithICUCall(aBuffer, ucal_getDefaultTimeZone); } + /** + * Fill the buffer with the host system's default IANA time zone identifier, + * e.g. "America/Chicago". + * + * NOTE: This function is not thread-safe. + */ + template + static ICUResult GetHostTimeZone(B& aBuffer) { + return FillBufferWithICUCall(aBuffer, ucal_getHostTimeZone); + } + /** * Set the default time zone. */ diff --git a/js/src/builtin/TestingFunctions.cpp b/js/src/builtin/TestingFunctions.cpp index 976e9d8c56e8c..1ef47e6095053 100644 --- a/js/src/builtin/TestingFunctions.cpp +++ b/js/src/builtin/TestingFunctions.cpp @@ -9,6 +9,9 @@ #include "mozilla/Atomics.h" #include "mozilla/Casting.h" #include "mozilla/FloatingPoint.h" +#ifdef JS_HAS_INTL_API +# include "mozilla/intl/TimeZone.h" +#endif #include "mozilla/Maybe.h" #include "mozilla/ScopeExit.h" #include "mozilla/Span.h" @@ -40,6 +43,7 @@ #ifdef JS_HAS_INTL_API # include "builtin/intl/CommonFunctions.h" +# include "builtin/intl/FormatBuffer.h" # include "builtin/intl/SharedIntlData.h" #endif #include "builtin/Promise.h" @@ -7286,12 +7290,24 @@ static bool GetICUOptions(JSContext* cx, unsigned argc, Value* vp) { return false; } - str = intl::CallICU(cx, ucal_getDefaultTimeZone); + intl::FormatBuffer buf(cx); + + if (auto ok = mozilla::intl::TimeZone::GetDefaultTimeZone(buf); ok.isErr()) { + intl::ReportInternalError(cx, ok.unwrapErr()); + return false; + } + + str = buf.toString(cx); if (!str || !JS_DefineProperty(cx, info, "timezone", str, JSPROP_ENUMERATE)) { return false; } - str = intl::CallICU(cx, ucal_getHostTimeZone); + if (auto ok = mozilla::intl::TimeZone::GetHostTimeZone(buf); ok.isErr()) { + intl::ReportInternalError(cx, ok.unwrapErr()); + return false; + } + + str = buf.toString(cx); if (!str || !JS_DefineProperty(cx, info, "host-timezone", str, JSPROP_ENUMERATE)) { return false;