diff options
author | 2023-05-23 00:40:12 -0700 | |
---|---|---|
committer | 2023-05-23 00:40:12 -0700 | |
commit | 5b38c55c3db018a94505f61cd785f0dd40f442ac (patch) | |
tree | af522e38ffa9b6c400c500c76de1fdca4ab931db /src/bun.js/bindings/BunJSCModule.cpp | |
parent | 83e7b9e198b25c7af7905c5dcabe1e325c5a38fb (diff) | |
download | bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.tar.gz bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.tar.zst bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.zip |
Support setting a timezone with `process.env.TZ` and `Bun.env.TZ` (#3018)
* Support setting a timezone via `process.env.TZ`
* Implement `setTimeZone` in `bun:jsc` module
* [breaking] `bun:test` now defaults to `Etc/UTC` timezone
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/BunJSCModule.cpp')
-rw-r--r-- | src/bun.js/bindings/BunJSCModule.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/bun.js/bindings/BunJSCModule.cpp b/src/bun.js/bindings/BunJSCModule.cpp index 63721a878..5809a9813 100644 --- a/src/bun.js/bindings/BunJSCModule.cpp +++ b/src/bun.js/bindings/BunJSCModule.cpp @@ -420,6 +420,38 @@ JSC_DEFINE_HOST_FUNCTION(functionDrainMicrotasks, (JSGlobalObject * globalObject return JSValue::encode(jsUndefined()); } +JSC_DEFINE_HOST_FUNCTION(functionSetTimeZone, (JSGlobalObject * globalObject, CallFrame* callFrame)) +{ + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (callFrame->argumentCount() < 1) { + throwTypeError(globalObject, scope, "setTimeZone requires a timezone string"_s); + return encodedJSValue(); + } + + if (!callFrame->argument(0).isString()) { + throwTypeError(globalObject, scope, "setTimeZone requires a timezone string"_s); + return encodedJSValue(); + } + + String timeZoneName = callFrame->argument(0).toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + double time = callFrame->argument(1).toNumber(globalObject); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + if (!WTF::setTimeZoneOverride(timeZoneName)) { + throwTypeError(globalObject, scope, makeString("Invalid timezone: \""_s, timeZoneName, "\""_s)); + return encodedJSValue(); + } + vm.dateCache.resetIfNecessarySlow(); + WTF::Vector<UChar, 32> buffer; + WTF::getTimeZoneOverride(buffer); + WTF::String timeZoneString(buffer.data(), buffer.size()); + return JSValue::encode(jsString(vm, timeZoneString)); +} + JSC_DEFINE_HOST_FUNCTION(functionRunProfiler, (JSGlobalObject * globalObject, CallFrame* callFrame)) { JSC::VM& vm = globalObject->vm(); @@ -528,6 +560,7 @@ JSC::JSObject* createJSCModule(JSC::JSGlobalObject* globalObject) object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "getProtectedObjects"_s), 1, functionGetProtectedObjects, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0); object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "generateHeapSnapshotForDebugging"_s), 0, functionGenerateHeapSnapshotForDebugging, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0); object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "profile"_s), 0, functionRunProfiler, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0); + object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "setTimeZone"_s), 0, functionSetTimeZone, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0); } return object; |