Skip to content

Setting process.env.TZ does not affect Dates #9856

Closed
@djbeaumont

Description

@djbeaumont

🐛 Bug Report

Nodejs 13+ allows for setting process.env.TZ and having it affect new Dates created at runtime. You can try this in a repl with the following (comments are the dates output on my system):

console.log(process.env.TZ); // undefined
console.log(new Date().toString()); // Tue Apr 21 2020 22:05:28 GMT+0100 (British Summer Time)
process.env.TZ = 'Asia/Kolkata'; // not my timezone
console.log(new Date().toString()); // Wed Apr 22 2020 02:35:28 GMT+0530 (India Standard Time)

This also works in a simple node script (i.e. node test.js) or in the mocha testing library.

Doing the same in jest, either with the lines copied into an test, or some variation where the timezone is set in beforeEach(), does not work. The time printed before and after setting the timezone is the same, ignoring the change in system timezone.

To Reproduce

Steps to reproduce the behavior:

describe('changing the timezone', () => {
  it('resets the nodejs timezone cache', () => {
    console.log(process.env.TZ); // undefined
    console.log(new Date().toString()); // Tue Apr 21 2020 22:36:08 GMT+0100 (British Summer Time)
    process.env.TZ = 'Asia/Kolkata'; // not my timezone
    console.log(new Date().toString()); // Tue Apr 21 2020 22:36:08 GMT+0100 (British Summer Time)
  });
});

Expected behavior

Note that that the date serialisation should be affected by the timezone in the second console.log()

describe('changing the timezone', () => {
  it('resets the nodejs timezone cache', () => {
    console.log(process.env.TZ); // undefined
    console.log(new Date().toString()); // Tue Apr 21 2020 22:37:36 GMT+0100 (British Summer Time)
    process.env.TZ = 'Asia/Kolkata'; // not my timezone
    console.log(new Date().toString()); // Wed Apr 22 2020 03:07:36 GMT+0530 (India Standard Time)
  });
});

Link to repl or repo (highly encouraged)

Unfortunately the linked repl uses nodejs 12 and so won't benefit from the change merged in nodejs 13.

envinfo

$ npx envinfo --preset jest
npx: installed 1 in 1.318s

  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.0.0 - ~/.nvm/versions/node/v14.0.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v14.0.0/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions