MorayM
MorayM

Reputation: 2687

How does node determine the timezone if the TZ environment variable is not used?

I'm trying to diagnose an issue in my node app where I'm seeing different Date behaviour on two different servers.

The servers are physically located in the UK, where the timezone is currently British Summer Time (BST, UTC+1), however on 25th October, BST will end and the timezone will revert to GMT/UTC. One server knows about BST and one does not.

For example, on the correctly-configured server, if local time (UTC+1) is 16:00,

const d = new Date();

the time part of d will show as 15:00:00Z. If I then change the date after 25th October with

d.setDate(30);

node will realise that the timezone is now UTC and as a result, the time part of d will now be set to 16:00:00Z.

On the broken server, new Date() will return the correct time in UTC, but using setDate to move the date after the end of BST will NOT change the time by an hour. As far as this server is concerned, the call to new Date() happened in UTC, not UTC+1.

This is puzzling as both servers have process.env.LANG set to en_GB.UTF-8 and on both, localectl status shows (confusingly) LANG=en_US.UTF-8. process.env.TZ is undefined on both as well, so how does one know about BST but the other doesn't?

Upvotes: 2

Views: 1274

Answers (1)

O. Jones
O. Jones

Reputation: 108839

Probably the default serverwide timezone on one server is set to UTC and on the other it is set to Europe/London *

If you put this in your code or set the TZ environment variable before running nodejs, you should be good.

process.env.TZ = 'Europe/London' 

Part of your confusion may come from the fact that UTC and Europe/London are the same in the winter.

All this stuff comes from IANA's timezone database. They use these Continent/City strings so they don't have to use BDT / BST or other stuff. They keep this up to date when jurisdictions change their daylight-time rules.

* Take that, Boris!

Upvotes: 3

Related Questions