Skip to content

Commit

Permalink
Bug 1731620 - Part 8: Add TimeZone::GetHostTimeZone(). r=platform-i18…
Browse files Browse the repository at this point in the history
…n-reviewers,gregtatum

Add a wrapper for `ucal_getHostTimeZone` to `mozilla::intl::TimeZone`. This
function is only used for the "testing" function framework.

Differential Revision: https://phabricator.services.mozilla.com/D126198
  • Loading branch information
anba committed Sep 23, 2021
1 parent cd5768c commit 975b298
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 11 additions & 0 deletions intl/components/src/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename B>
static ICUResult GetHostTimeZone(B& aBuffer) {
return FillBufferWithICUCall(aBuffer, ucal_getHostTimeZone);
}

/**
* Set the default time zone.
*/
Expand Down
20 changes: 18 additions & 2 deletions js/src/builtin/TestingFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -7286,12 +7290,24 @@ static bool GetICUOptions(JSContext* cx, unsigned argc, Value* vp) {
return false;
}

str = intl::CallICU(cx, ucal_getDefaultTimeZone);
intl::FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> 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;
Expand Down

0 comments on commit 975b298

Please sign in to comment.