| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 524a9d6 commit c0deeea
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1667,6 +1667,37 @@ Be aware that unless the child environment is explicitly set, this environment | |||
| 1667 | 1667 | variable will be inherited by any child processes, and if they use OpenSSL, it | |
| 1668 | 1668 | may cause them to trust the same CAs as node. | |
| 1669 | 1669 | ||
| 1670 | + ### `TZ` | ||
| 1671 | + <!-- YAML | ||
| 1672 | + added: v0.0.1 | ||
| 1673 | + changes: | ||
| 1674 | + - version: | ||
| 1675 | + - REPLACEME | ||
| 1676 | + pr-url: https://github.com/nodejs/node/pull/38642 | ||
| 1677 | + description: | ||
| 1678 | + Changing the TZ variable using process.env.TZ = changes the timezone | ||
| 1679 | + on Windows as well. | ||
| 1680 | + - version: | ||
| 1681 | + - v13.0.0 | ||
| 1682 | + pr-url: https://github.com/nodejs/node/pull/20026 | ||
| 1683 | + description: | ||
| 1684 | + Changing the TZ variable using process.env.TZ = changes the timezone | ||
| 1685 | + on POSIX systems. | ||
| 1686 | + --> | ||
| 1687 | + | ||
| 1688 | + The `TZ` environment variable is used to specify the timezone configuration. | ||
| 1689 | + | ||
| 1690 | + While the Node.js support for `TZ` will not handle all of the various | ||
| 1691 | + [ways that `TZ` is handled in other environments][], it will support basic | ||
| 1692 | + [timezone IDs][] (such as `'Etc/UTC'`, `'Europe/Paris'` or `'America/New_York'`. | ||
| 1693 | + It may support a few other abbreviations or aliases, but these are strongly | ||
| 1694 | + discouraged and not guaranteed. | ||
| 1695 | + | ||
| 1696 | + ```console | ||
| 1697 | + $ TZ=Europe/Dublin node -pe "new Date().toString()" | ||
| 1698 | + Wed May 12 2021 20:30:48 GMT+0100 (Irish Standard Time) | ||
| 1699 | + ``` | ||
| 1700 | + | ||
| 1670 | 1701 | ### `UV_THREADPOOL_SIZE=size` | |
| 1671 | 1702 | ||
| 1672 | 1703 | Set the number of threads used in libuv's threadpool to `size` threads. | |
@@ -1741,3 +1772,5 @@ $ node --max-old-space-size=1536 index.js | |||
| 1741 | 1772 | [jitless]: https://v8.dev/blog/jitless | |
| 1742 | 1773 | [libuv threadpool documentation]: https://docs.libuv.org/en/latest/threadpool.html | |
| 1743 | 1774 | [remote code execution]: https://www.owasp.org/index.php/Code_Injection | |
| 1775 | + [timezone IDs]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones | ||
| 1776 | + [ways that `TZ` is handled in other environments]: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -937,6 +937,14 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv, | |||
| 937 | 937 | return 9; | |
| 938 | 938 | } | |
| 939 | 939 | per_process::metadata.versions.InitializeIntlVersions(); | |
| 940 | + | ||
| 941 | + # ifndef __POSIX__ | ||
| 942 | + std::string tz; | ||
| 943 | + if (credentials::SafeGetenv("TZ", &tz) && !tz.empty()) { | ||
| 944 | + i18n::SetDefaultTimeZone(tz.c_str()); | ||
| 945 | + } | ||
| 946 | + # endif | ||
| 947 | + | ||
| 940 | 948 | #endif | |
| 941 | 949 | ||
| 942 | 950 | NativeModuleEnv::InitializeCodeCache(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | #include "env-inl.h" | |
| 3 | 3 | #include "node_errors.h" | |
| 4 | 4 | #include "node_external_reference.h" | |
| 5 | + #include "node_i18n.h" | ||
| 5 | 6 | #include "node_process.h" | |
| 6 | 7 | ||
| 7 | 8 | #include <time.h> // tzset(), _tzset() | |
@@ -69,15 +70,32 @@ std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>(); | |||
| 69 | 70 | } // namespace per_process | |
| 70 | 71 | ||
| 71 | 72 | template <typename T> | |
| 72 | - void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) { | ||
| 73 | + void DateTimeConfigurationChangeNotification( | ||
| 74 | + Isolate* isolate, | ||
| 75 | + const T& key, | ||
| 76 | + const char* val = nullptr) { | ||
| 73 | 77 | if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') { | |
| 74 | 78 | #ifdef __POSIX__ | |
| 75 | 79 | tzset(); | |
| 80 | + isolate->DateTimeConfigurationChangeNotification( | ||
| 81 | + Isolate::TimeZoneDetection::kRedetect); | ||
| 76 | 82 | #else | |
| 77 | 83 | _tzset(); | |
| 84 | + | ||
| 85 | + # if defined(NODE_HAVE_I18N_SUPPORT) | ||
| 86 | + isolate->DateTimeConfigurationChangeNotification( | ||
| 87 | + Isolate::TimeZoneDetection::kSkip); | ||
| 88 | + | ||
| 89 | + // On windows, the TZ environment is not supported out of the box. | ||
| 90 | + // By default, v8 will only be able to detect the system configured | ||
| 91 | + // timezone. This supports using the TZ environment variable to set | ||
| 92 | + // the default timezone instead. | ||
| 93 | + if (val != nullptr) i18n::SetDefaultTimeZone(val); | ||
| 94 | + # else | ||
| 95 | + isolate->DateTimeConfigurationChangeNotification( | ||
| 96 | + Isolate::TimeZoneDetection::kRedetect); | ||
| 97 | + # endif | ||
| 78 | 98 | #endif | |
| 79 | - auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect; | ||
| 80 | - isolate->DateTimeConfigurationChangeNotification(time_zone_detection); | ||
| 81 | 99 | } | |
| 82 | 100 | } | |
| 83 | 101 | ||
@@ -128,7 +146,7 @@ void RealEnvStore::Set(Isolate* isolate, | |||
| 128 | 146 | if (key.length() > 0 && key[0] == '=') return; | |
| 129 | 147 | #endif | |
| 130 | 148 | uv_os_setenv(*key, *val); | |
| 131 | - DateTimeConfigurationChangeNotification(isolate, key); | ||
| 149 | + DateTimeConfigurationChangeNotification(isolate, key, *val); | ||
| 132 | 150 | } | |
| 133 | 151 | ||
| 134 | 152 | int32_t RealEnvStore::Query(const char* key) const { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -542,6 +542,16 @@ bool InitializeICUDirectory(const std::string& path) { | |||
| 542 | 542 | return status == U_ZERO_ERROR; | |
| 543 | 543 | } | |
| 544 | 544 | ||
| 545 | + void SetDefaultTimeZone(const char* tzid) { | ||
| 546 | + size_t tzidlen = strlen(tzid) + 1; | ||
| 547 | + UErrorCode status = U_ZERO_ERROR; | ||
| 548 | + MaybeStackBuffer<UChar, 256> id(tzidlen); | ||
| 549 | + u_charsToUChars(tzid, id.out(), tzidlen); | ||
| 550 | + // This is threadsafe: | ||
| 551 | + ucal_setDefaultTimeZone(id.out(), &status); | ||
| 552 | + CHECK(U_SUCCESS(status)); | ||
| 553 | + } | ||
| 554 | + | ||
| 545 | 555 | int32_t ToUnicode(MaybeStackBuffer<char>* buf, | |
| 546 | 556 | const char* input, | |
| 547 | 557 | size_t length) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,8 @@ namespace i18n { | |||
| 40 | 40 | ||
| 41 | 41 | bool InitializeICUDirectory(const std::string& path); | |
| 42 | 42 | ||
| 43 | + void SetDefaultTimeZone(const char* tzid); | ||
| 44 | + | ||
| 43 | 45 | enum idna_mode { | |
| 44 | 46 | // Default mode for maximum compatibility. | |
| 45 | 47 | IDNA_DEFAULT, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const { isMainThread } = require('worker_threads'); | ||
| 5 | + | ||
| 6 | + if (!common.hasIntl) | ||
| 7 | + common.skip('Intl not present.'); | ||
| 8 | + | ||
| 9 | + if (!isMainThread) | ||
| 10 | + common.skip('Test not support running within a worker'); | ||
| 11 | + | ||
| 12 | + const assert = require('assert'); | ||
| 13 | + | ||
| 14 | + process.env.TZ = 'Etc/UTC'; | ||
| 15 | + assert.match(new Date().toString(), /GMT\+0000/); | ||
| 16 | + | ||
| 17 | + process.env.TZ = 'America/New_York'; | ||
| 18 | + assert.match(new Date().toString(), /Eastern (Standard|Daylight) Time/); | ||
| 19 | + | ||
| 20 | + process.env.TZ = 'America/Los_Angeles'; | ||
| 21 | + assert.match(new Date().toString(), /Pacific (Standard|Daylight) Time/); | ||
| 22 | + | ||
| 23 | + process.env.TZ = 'Europe/Dublin'; | ||
| 24 | + assert.match(new Date().toString(), /Irish/); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments