From b6a0fac4966d1f6f4e5b4a2938bd1d3cec94bf60 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 11 May 2021 09:10:46 -0700 Subject: [PATCH 1/2] lib: support setting process.env.TZ on windows Fixes: https://github.com/nodejs/node/issues/4230 Signed-off-by: James M Snell --- doc/api/cli.md | 33 ++++++++++++++++++++ src/node.cc | 8 +++++ src/node_env_var.cc | 26 ++++++++++++--- src/node_i18n.cc | 10 ++++++ src/node_i18n.h | 2 ++ test/parallel/test-datetime-change-notify.js | 20 ++++++++++++ 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-datetime-change-notify.js diff --git a/doc/api/cli.md b/doc/api/cli.md index c2e5f9079576ba..a47d724bf9093c 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1669,6 +1669,37 @@ Be aware that unless the child environment is explicitly set, this environment variable will be inherited by any child processes, and if they use OpenSSL, it may cause them to trust the same CAs as node. +### `TZ` + + +The `TZ` environment variable is used to specify the timezone configuration. + +While the Node.js support for `TZ` will not handle all of the various +[ways that `TZ` is handled in other environments][], it will support basic +[timezone IDs][] (such as `'Etc/UTC'`, `'Europe/Paris'` or `'America/New_York'`. +It may support a few other abbreviations or aliases, but these are strongly +discouraged and not guaranteed. + +```console +$ TZ=Europe/Dublin node -pe "new Date().toString()" +Wed May 12 2021 20:30:48 GMT+0100 (Irish Standard Time) +``` + ### `UV_THREADPOOL_SIZE=size` Set the number of threads used in libuv's threadpool to `size` threads. @@ -1743,3 +1774,5 @@ $ node --max-old-space-size=1536 index.js [jitless]: https://v8.dev/blog/jitless [libuv threadpool documentation]: https://docs.libuv.org/en/latest/threadpool.html [remote code execution]: https://www.owasp.org/index.php/Code_Injection +[timezone IDs]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones +[ways that `TZ` is handled in other environments]: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html diff --git a/src/node.cc b/src/node.cc index b757013ce125cd..bf041fb682a2c2 100644 --- a/src/node.cc +++ b/src/node.cc @@ -937,6 +937,14 @@ int InitializeNodeWithArgs(std::vector* argv, return 9; } per_process::metadata.versions.InitializeIntlVersions(); + +# ifndef __POSIX__ + std::string tz; + if (credentials::SafeGetenv("TZ", &tz) && !tz.empty()) { + i18n::SetDefaultTimeZone(tz.c_str()); + } +# endif + #endif NativeModuleEnv::InitializeCodeCache(); diff --git a/src/node_env_var.cc b/src/node_env_var.cc index e6d9573ca7ccef..b13f7fcd16fd50 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -2,6 +2,7 @@ #include "env-inl.h" #include "node_errors.h" #include "node_external_reference.h" +#include "node_i18n.h" #include "node_process.h" #include // tzset(), _tzset() @@ -69,15 +70,32 @@ std::shared_ptr system_environment = std::make_shared(); } // namespace per_process template -void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) { +void DateTimeConfigurationChangeNotification( + Isolate* isolate, + const T& key, + const char* val = nullptr) { if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') { #ifdef __POSIX__ tzset(); + isolate->DateTimeConfigurationChangeNotification( + Isolate::TimeZoneDetection::kRedetect); #else _tzset(); + +# if defined(NODE_HAVE_I18N_SUPPORT) + isolate->DateTimeConfigurationChangeNotification( + Isolate::TimeZoneDetection::kSkip); + + // On windows, the TZ environment is not supported out of the box. + // By default, v8 will only be able to detect the system configured + // timezone. This supports using the TZ environment variable to set + // the default timezone instead. + if (val != nullptr) i18n::SetDefaultTimeZone(val); +# else + isolate->DateTimeConfigurationChangeNotification( + Isolate::TimeZoneDetection::kRedetect); +# endif #endif - auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect; - isolate->DateTimeConfigurationChangeNotification(time_zone_detection); } } @@ -128,7 +146,7 @@ void RealEnvStore::Set(Isolate* isolate, if (key.length() > 0 && key[0] == '=') return; #endif uv_os_setenv(*key, *val); - DateTimeConfigurationChangeNotification(isolate, key); + DateTimeConfigurationChangeNotification(isolate, key, *val); } int32_t RealEnvStore::Query(const char* key) const { diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 42e618ac30181f..1b105cc9e56cd6 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -542,6 +542,16 @@ bool InitializeICUDirectory(const std::string& path) { return status == U_ZERO_ERROR; } +void SetDefaultTimeZone(const char* tzid) { + size_t tzidlen = strlen(tzid) + 1; + UErrorCode status = U_ZERO_ERROR; + MaybeStackBuffer id(tzidlen); + u_charsToUChars(tzid, id.out(), tzidlen); + // This is threadsafe: + ucal_setDefaultTimeZone(id.out(), &status); + CHECK(U_SUCCESS(status)); +} + int32_t ToUnicode(MaybeStackBuffer* buf, const char* input, size_t length) { diff --git a/src/node_i18n.h b/src/node_i18n.h index 22164e6647d913..1e9f13ebc93f02 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -40,6 +40,8 @@ namespace i18n { bool InitializeICUDirectory(const std::string& path); +void SetDefaultTimeZone(const char* tzid); + enum idna_mode { // Default mode for maximum compatibility. IDNA_DEFAULT, diff --git a/test/parallel/test-datetime-change-notify.js b/test/parallel/test-datetime-change-notify.js new file mode 100644 index 00000000000000..146b9d23883b6b --- /dev/null +++ b/test/parallel/test-datetime-change-notify.js @@ -0,0 +1,20 @@ +'use strict'; + +const common = require('../common'); + +if (!common.hasIntl) + common.skip('Intl not present.'); + +const assert = require('assert'); + +process.env.TZ = 'Etc/UTC'; +assert.match(new Date().toString(), /GMT\+0000/); + +process.env.TZ = 'America/New_York'; +assert.match(new Date().toString(), /Eastern (Standard|Daylight) Time/); + +process.env.TZ = 'America/Los_Angeles'; +assert.match(new Date().toString(), /Pacific (Standard|Daylight) Time/); + +process.env.TZ = 'Europe/Dublin'; +assert.match(new Date().toString(), /Irish/); From 8f3bbc478552cfd397f1295af22aabc48a305bab Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 13 May 2021 20:58:46 -0700 Subject: [PATCH 2/2] fixup! lib: support setting process.env.TZ on windows --- test/parallel/test-datetime-change-notify.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/parallel/test-datetime-change-notify.js b/test/parallel/test-datetime-change-notify.js index 146b9d23883b6b..94f126179b79de 100644 --- a/test/parallel/test-datetime-change-notify.js +++ b/test/parallel/test-datetime-change-notify.js @@ -1,10 +1,14 @@ 'use strict'; const common = require('../common'); +const { isMainThread } = require('worker_threads'); if (!common.hasIntl) common.skip('Intl not present.'); +if (!isMainThread) + common.skip('Test not support running within a worker'); + const assert = require('assert'); process.env.TZ = 'Etc/UTC';