Oliver Watkins
Oliver Watkins

Reputation: 13549

Node process.timezone not being set

I am trying to set the timezone on the process.env object but it does not seem to work.

The following code is run as a JEST test, however it should still be relevant to node processes (right?).

If I set TZ to UTC, then the date I create is still my current timezone and not UTC. See below :

describe('Timezones', () => {
    it('should always be UTC', () => {

        process.env.TZ = 'UTC'

        let d = new Date();

        expect(d.getTimezoneOffset()).toBe(0); //ERROR!!! 120 minutes out... ie. Europe/Berlin where i am
    });
})

Upvotes: 1

Views: 250

Answers (1)

tuhin47
tuhin47

Reputation: 6068

If you set a time in process.env then you should fetch it from the process. So if you are in the Linux kernel you should run a shell script to fetch the date. Here is my solution for Linux.

process.env.TZ='UTC'
const execSync = require('child_process').execSync;
const output = execSync('date', { encoding: 'utf-8' }); 
console.log('UTC = '+output);

process.env.TZ='GMT'
const output2 = execSync('date', { encoding: 'utf-8' }); 
console.log('GMT = '+output2);

Upvotes: 1

Related Questions