Skip to content

Commit f3f8c3a

Browse files
committed
lib: support setting process.env.TZ on windows
Fixes: #4230 Signed-off-by: James M Snell <[email protected]>
1 parent 184e0f7 commit f3f8c3a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/node_env_var.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "node_external_reference.h"
55
#include "node_process.h"
66

7+
#include "unicode/timezone.h"
8+
79
#include <time.h> // tzset(), _tzset()
810

911
namespace node {
@@ -69,16 +71,30 @@ std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
6971
} // namespace per_process
7072

7173
template <typename T>
72-
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
74+
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key, const char* val = nullptr) {
7375
if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') {
76+
auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
7477
#ifdef __POSIX__
7578
tzset();
7679
#else
7780
_tzset();
7881
#endif
79-
auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
8082
isolate->DateTimeConfigurationChangeNotification(time_zone_detection);
83+
84+
// On windows, the TZ environment is not supported out of the box.
85+
// By default, v8 will only be able to detect the system configured
86+
// timezone. This supports using the TZ environment variable to set
87+
// the default timezone instead.
88+
#ifndef __POSIX__
89+
if (val != nullptr) {
90+
icu::UnicodeString id =
91+
icu::UnicodeString::fromUTF8(icu::StringPiece(val));
92+
icu::TimeZone* tz = icu::TimeZone::createTimeZone(id);
93+
if (*tz != icu::TimeZone::getUnknown())
94+
icu::TimeZone::adoptDefault(tz);
95+
}
8196
}
97+
#endif
8298
}
8399

84100
Maybe<std::string> RealEnvStore::Get(const char* key) const {
@@ -128,7 +144,7 @@ void RealEnvStore::Set(Isolate* isolate,
128144
if (key.length() > 0 && key[0] == '=') return;
129145
#endif
130146
uv_os_setenv(*key, *val);
131-
DateTimeConfigurationChangeNotification(isolate, key);
147+
DateTimeConfigurationChangeNotification(isolate, key, *val);
132148
}
133149

134150
int32_t RealEnvStore::Query(const char* key) const {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
process.env.TZ = 'UTC';
7+
assert.match(new Date().toString(), /GMT\+0000/);
8+
9+
process.env.TZ = 'EST';
10+
assert.match(new Date().toString(), /GMT-05:00/);

0 commit comments

Comments
 (0)