Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't set a default timezone on windows. #4230

Closed
Claud opened this issue Dec 10, 2015 · 53 comments
Closed

I can't set a default timezone on windows. #4230

Claud opened this issue Dec 10, 2015 · 53 comments
Labels
i18n-api Issues and PRs related to the i18n implementation. question Issues that look for answers. v8 engine Issues and PRs related to the V8 dependency. windows Issues and PRs related to the Windows platform.

Comments

@Claud
Copy link

Claud commented Dec 10, 2015

This code works well on Linux but does not work on windows.

process.env.TZ = 'UTC';
console.log(new Date());

On windows i see timezone from my os.

Thu Dec 10 2015 14:28:45 GMT+0300

@r-52 r-52 added question Issues that look for answers. windows Issues and PRs related to the Windows platform. labels Dec 10, 2015
@bnoordhuis
Copy link
Member

Does it work when you set TZ on the command line? V8 calls _tzset() on Windows, which respects TZ, but it runs quite early on.

The same caveat applies to Unices; the fact that setting TZ inside the program works is luck more than anything else. If you want it to be reliable and future proof, run your program with env TZ=UTC node app.js.

@Claud
Copy link
Author

Claud commented Dec 10, 2015

@bnoordhuis Yes, I tried to set an environment variable, set "TZ=UTC" is not work.

@bnoordhuis
Copy link
Member

What version of Windows is that? Do you get correct output after tzutil /s UTC?

@Claud
Copy link
Author

Claud commented Dec 10, 2015

Windows 10x64

Thu Dec 10 2015 12:42:57 GMT+0000

It works, but it changes the time zone of the OS.

@bnoordhuis
Copy link
Member

It works, but it changes the time zone of the OS.

Right, but it confirms that the timezone calculation as such is correct once it has the right timezone.

/cc @nodejs/platform-windows - I'm fairly sure it's a V8 issue (relevant code here) but maybe you can take a look?

@seishun
Copy link
Contributor

seishun commented Dec 10, 2015

Is it even supposed to work the way it does on Linux? Is it documented anywhere?

@bnoordhuis
Copy link
Member

V8 documentation? Surely you jest! If you're referring to _tzset(), MSDN claims it honors TZ.

@seishun
Copy link
Contributor

seishun commented Dec 10, 2015

No, I'm referring to how the TZ variable affects the behavior of new Date() in Node.js.

@seishun
Copy link
Contributor

seishun commented Dec 10, 2015

Correction: new Date() is not affected, the implicit toString() call is.

@bnoordhuis
Copy link
Member

Neither we nor V8 document the effect of TZ, it's an artifact of the implementation.

@mscdex mscdex added the v8 engine Issues and PRs related to the V8 dependency. label Dec 10, 2015
@seishun
Copy link
Contributor

seishun commented Dec 10, 2015

If you're referring to _tzset(), MSDN claims it honors TZ.

GetTimeZoneInformation, however, does not - and that's where V8 gets timezone information from. I'm not sure if there is any point to the _tzset() call at all.

@jasnell
Copy link
Member

jasnell commented Jun 6, 2016

Closing as there does not appear to be anything for us to do in core on this. Can reopen if necessary.

@jasnell jasnell closed this as completed Jun 6, 2016
@pankleks
Copy link

Hello,

I've been struggling with same problem.
Under windows, TZ setting is not respected, zone is taken from user which runs app.
Under Linux it works as expected.

I'm running production enviroment where I can't change zone for user - what should I do?

Thanks!

@capaj
Copy link

capaj commented Mar 16, 2018

@pankleks the only solution I've come up for windows is: https://github.com/capaj/set-tz

@smokinjoe
Copy link

This problem still exists two years later.

willsmythe added a commit to willsmythe/d3-scale that referenced this issue Dec 6, 2018
Setting the TZ environment variable has no effect on Windows; causing some date/time related tests to fail.
See nodejs/node#4230
@danielbayerlein
Copy link

I've created a ugly batch script for this issue.

package.json

"scripts": {
  "test": "jest",
  "test:win": "bin/test.cmd"
}

bin/test.cmd

FOR /F "tokens=* USEBACKQ" %%F IN (`tzutil /g`) DO SET PREVIOUS_TZ=%%F
tzutil /s "UTC"
cmd.exe /c yarn test
tzutil /s "%PREVIOUS_TZ%"

jasnell added a commit to jasnell/node that referenced this issue May 12, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell
Copy link
Member

jasnell commented May 12, 2021

Ok, updated #38642

it now supports setting TZ before running the node.js process and during...

$ set TZ="America/New_York"
$ node -pe "new Date().toString()"
process.env.TZ = 'Etc/UTC'
console.log(new Date().toString());

@srl295
Copy link
Member

srl295 commented May 12, 2021

Ok, then yeah, it would appear that we'll have to do this mapping ourselves. @srl295 ... is there an API in ICU to map timezone identifiers like "EST" to the identifier expected by TimeZone::createTimeZone()

As i noted in the PR, EST should be strongly discouraged. It's also almost certainly not what you want. (the east coast of n. america is in daylight time right now, EST will give the wrong result for example). if we do anything (such as in the pr) it should be advertised as using tz ids only, everything else is undefined results.

jasnell added a commit to jasnell/node that referenced this issue May 12, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell added a commit to jasnell/node that referenced this issue May 12, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell added a commit to jasnell/node that referenced this issue May 12, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell added a commit to jasnell/node that referenced this issue May 12, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell added a commit to jasnell/node that referenced this issue May 13, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell added a commit to jasnell/node that referenced this issue May 14, 2021
Fixes: nodejs#4230
Signed-off-by: James M Snell <jasnell@gmail.com>
@srl295 srl295 added the i18n-api Issues and PRs related to the i18n implementation. label May 14, 2021
targos pushed a commit that referenced this issue May 18, 2021
Fixes: #4230
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #38642
Reviewed-By: Steven R Loomis <srloomis@us.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@WiXSL
Copy link

WiXSL commented Oct 31, 2021

I can confirm this problem is solved in node version 17.0.1 using Windows 10

@pavelmickevic
Copy link

thank you for the fix 16cb4f7

I can confirm this problem is solved in node version 17.0.1 using Windows 10

works now:

node -v
v17.2.0

OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.19041 N/A Build 19041

-console.log({ tz: process.env.TZ, offset: moment().utcOffset() }); // { tz: 'UTC', offset: 120 } # bad
+console.log({ tz: process.env.TZ, offset: moment().utcOffset() }); // { tz: 'UTC', offset: -0 } # good

@tmeindle
Copy link

tmeindle commented Feb 15, 2022

Could the fix for this issue be backported to Node 16 LTS?

@richardlau
Copy link
Member

Could the fix for this issue be backported to Node 16 LTS?

#38642 was included in Node.js 16.2.0.

@phalgunv
Copy link

Is this fix available on Node.js v14?

@richardlau
Copy link
Member

Is this fix available on Node.js v14?

No, it was not backported to Node.js v14.

@phalgunv
Copy link

What can be done to get it ported back to v14?

@targos
Copy link
Member

targos commented May 15, 2023

Nothing. v14 is out of support now.

@NisuSan
Copy link

NisuSan commented May 29, 2023

Not working if send the timesatamp to contructor

$ node -pe "new Date(1685389932472)"
2023-05-29T19:52:12.472Z

$ node -pe "new Date()"
2023-05-29T20:21:51.841Z 

$ set TZ="Europe/Kiev" && node -pe "new Date(1685389932472)" #still not working
2023-05-29T19:52:12.472Z

But timesatmp is Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)

@mattjohnsonpint
Copy link

@NisuSan - Time zones don't change the UTC representation of a timestamp.

@NisuSan
Copy link

NisuSan commented Jun 12, 2023

@mattjohnsonpint Hmm, in browser it works fine. Any way, can you explain how to deal with timestamp and time zone in this case?

@mattjohnsonpint
Copy link

@NisuSan - The Z at the end of the ISO 8601 / RFC 3339 formatted string represents "Zulu" time - which is another term for "UTC". Unix timestamps are also in terms of UTC (being the number of seconds or milliseconds since 1970-01-01T00:00:00.000Z). UTC is irrespective of time zones, thus time zones do not affect UTC output.

If you're asking why you see UTC output instead of local output, that's because you're just printing a raw Date object. You didn't call any function on it that creates a string. Unfortunately, the implementation of printing a raw Date object is undefined. Some environments print the UTC representation (as if you printed the result of calling the toISOString function), and other environments print the local time representation (as if you printed the result of calling the toString function).

I have asked for standardization of this, to avoid the type of confusion you encountered, but with no result. See whatwg/console#132

@mattjohnsonpint
Copy link

mattjohnsonpint commented Jun 12, 2023

If you want to see the effect of the system time zone, and Node's support for changing it, then you'll need to call toString - not just print the raw Date object.

on Windows:

set TZ=Europe/Kiev && node -pe "new Date(1685389932472).toString()"
Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)

Note: Make sure there are no quotes around the ID on Windows.

or on Linux / macOS :

TZ="Europe/Kiev" node -pe "new Date(1685389932472).toString()"
Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)

@NisuSan
Copy link

NisuSan commented Jun 12, 2023

@mattjohnsonpint Great explanation, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n-api Issues and PRs related to the i18n implementation. question Issues that look for answers. v8 engine Issues and PRs related to the V8 dependency. windows Issues and PRs related to the Windows platform.
Projects
None yet