Skip to content

Commit

Permalink
lib: support setting process.env.TZ on windows
Browse files Browse the repository at this point in the history
Fixes: #4230
Signed-off-by: James M Snell <[email protected]>
  • Loading branch information
jasnell committed May 11, 2021
1 parent 184e0f7 commit f3f8c3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/node_env_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "node_external_reference.h"
#include "node_process.h"

#include "unicode/timezone.h"

#include <time.h> // tzset(), _tzset()

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

template <typename T>
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') {
auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
#ifdef __POSIX__
tzset();
#else
_tzset();
#endif
auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
isolate->DateTimeConfigurationChangeNotification(time_zone_detection);

// 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.
#ifndef __POSIX__
if (val != nullptr) {
icu::UnicodeString id =
icu::UnicodeString::fromUTF8(icu::StringPiece(val));
icu::TimeZone* tz = icu::TimeZone::createTimeZone(id);
if (*tz != icu::TimeZone::getUnknown())
icu::TimeZone::adoptDefault(tz);
}
}
#endif
}

Maybe<std::string> RealEnvStore::Get(const char* key) const {
Expand Down Expand Up @@ -128,7 +144,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 {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-datetime-change-notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

require('../common');
const assert = require('assert');

process.env.TZ = 'UTC';
assert.match(new Date().toString(), /GMT\+0000/);

process.env.TZ = 'EST';
assert.match(new Date().toString(), /GMT-05:00/);

0 comments on commit f3f8c3a

Please sign in to comment.