How to set a timezone of a node process?

Viewed 632

I am using 12.14.1 and it seems to me, node ignores TZ env. variable in many cases:

$ TZ=GMT-4 node -e 'console.log(new Date().getTimezoneOffset(), new Date().toString())'
-120 Mon Mar 30 2020 10:10:12 GMT+0200 (Central European Summer Time)

This is plain wrong. Either it should all be in GMT+4 (yes, +, weird POSIX thing) or returned time must not match my local time (same time in timezone GMT+2 and GMT+4 is nonsense).

Time from different timezones should differ when converted to a same timezone for printing:

$ TZ=GMT-2 node -e 'console.log(new Date().getTimezoneOffset(), new Date().toString())'
-120 Mon Mar 30 2020 10:10:14 GMT+0200 (Central European Summer Time)

Yet Node is adamant it is same.

How to reliably and cross-platform (Linux, Windows, Mac) set arbitrary (not only UTC and equivalents) timezone of a node process?

Edit:
Here is an example of one correct option, how it could behave:

$ TZ=GMT-4 date
Po 30. března 2020, 12:06:04 GMT
$ TZ=GMT-2 date
Po 30. března 2020, 10:06:09 GMT

When converted to same timzone, times from different timezones must differ (GMT+4 and GMT+2 differes by 2 hours).

1 Answers

TZ=GMT followed by +/- and a number no longer works since version 10. For example for the GMT+4 can be used TZ=Europe/Samara instead. When searching for a suitable timezone, be careful to pick one which doesn't have daylight saving time (DST).

Test printing new Date().getTimezoneOffset(), new Date().toString() for major node versions 6-13:

current date         : Po 30. března 2020, 14:32:31 CEST
with TZ=GMT-4        : Po 30. března 2020, 16:32:31 GMT
with TZ=UTC          : Po 30. března 2020, 12:32:31 UTC
with TZ=GET          : Po 30. března 2020, 16:32:31 +04
with TZ=Europe/Samara: Po 30. března 2020, 12:32:31 GET

v6.17.1
with TZ=GMT-4        : -240 'Mon Mar 30 2020 16:32:31 GMT+0400 (GMT)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:31 GMT+0000 (UTC)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:31 GMT+0000 (GET)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:31 GMT+0400 (+04)'

v7.10.1
with TZ=GMT-4        : -240 'Mon Mar 30 2020 16:32:32 GMT+0400 (GMT)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:32 GMT+0000 (UTC)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:32 GMT+0000 (GET)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:32 GMT+0400 (+04)'

v8.17.0
with TZ=GMT-4        : -240 'Mon Mar 30 2020 16:32:32 GMT+0400 (GMT)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:33 GMT+0000 (UTC)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:33 GMT+0000 (GET)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:33 GMT+0400 (+04)'

v9.11.2
with TZ=GMT-4        : -240 'Mon Mar 30 2020 16:32:33 GMT+0400 (GMT)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:33 GMT+0000 (UTC)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:33 GMT+0000 (GET)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:33 GMT+0400 (+04)'

v10.19.0
with TZ=GMT-4        : -120 'Mon Mar 30 2020 14:32:34 GMT+0200 (Central European Summer Time)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:34 GMT+0000 (Coordinated Universal Time)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:34 GMT+0000 (GMT)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:34 GMT+0400 (Samara Standard Time)'

v11.15.0
with TZ=GMT-4        : -120 'Mon Mar 30 2020 14:32:35 GMT+0200 (Central European Summer Time)'
with TZ=UTC          : 0 'Mon Mar 30 2020 12:32:35 GMT+0000 (Coordinated Universal Time)'
with TZ=GET          : 0 'Mon Mar 30 2020 12:32:35 GMT+0000 (GMT)'
with TZ=Europe/Samara: -240 'Mon Mar 30 2020 16:32:35 GMT+0400 (Samara Standard Time)'

v12.16.1
with TZ=GMT-4        : -120 Mon Mar 30 2020 14:32:35 GMT+0200 (Central European Summer Time)
with TZ=UTC          : 0 Mon Mar 30 2020 12:32:35 GMT+0000 (Coordinated Universal Time)
with TZ=GET          : 0 Mon Mar 30 2020 12:32:35 GMT+0000 (GMT)
with TZ=Europe/Samara: -240 Mon Mar 30 2020 16:32:35 GMT+0400 (Samara Standard Time)

v13.12.0
with TZ=GMT-4        : -120 Mon Mar 30 2020 14:32:36 GMT+0200 (Central European Summer Time)
with TZ=UTC          : 0 Mon Mar 30 2020 12:32:36 GMT+0000 (Coordinated Universal Time)
with TZ=GET          : 0 Mon Mar 30 2020 12:32:36 GMT+0000 (GMT)
with TZ=Europe/Samara: -240 Mon Mar 30 2020 16:32:36 GMT+0400 (Samara Standard Time)

For more details see the ticket: https://github.com/nodejs/node/issues/32564

Related