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

node:8-alpine wrong timezone behavior #626

Closed
SachaCR opened this issue Feb 13, 2018 · 8 comments
Closed

node:8-alpine wrong timezone behavior #626

SachaCR opened this issue Feb 13, 2018 · 8 comments

Comments

@SachaCR
Copy link

SachaCR commented Feb 13, 2018

Hi all,

I have found a strange behavior in node:8-alpine. Some methods of the Date object return incorrect values.

The getTimezoneOffset() method return 0 instead of -60 for Paris TZ

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().getTimezoneOffset()"
-> 0

Also the getHours method return the non local hour:

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().getHours()"
-> 10

But the toLocaleTimeString method return the correct hour.

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().toLocaleTimeString()"
-> 11:xx:xx

So it seem that the methods getHours don't take into account the TZ env var.

@SachaCR SachaCR changed the title Date Method broken about timezone Date methods broken about timezone Feb 13, 2018
@SachaCR SachaCR changed the title Date methods broken about timezone node:8-alpine wrong timezone behavior Feb 13, 2018
@LaurentGoderre
Copy link
Member

You need to run the following

RUN apk --update add \
		tzdata \
	&& cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
	&& apk del tzdata

Info from gliderlabs/docker-alpine/issues/136

@SimenB
Copy link
Member

SimenB commented Feb 17, 2018

Closing as it seems answered? Happy to reopen

@SimenB SimenB closed this as completed Feb 17, 2018
@davidschrooten
Copy link

davidschrooten commented Sep 17, 2019

This issue is not exactly answered. Having this same issue on node:10-alpine. Tried solution above and whatever I could find in gliderlabs/docker-alpine/issues/136. But to no avail. When doing console.log(new Date()) the issue persists in node.js; time differs minus 2 hours from what busybox says when running the date command.

@craig-riecke
Copy link

craig-riecke commented Mar 10, 2020

I had the same issue and found that setting environment variable TZ in the Dockerfile solves it. At least for me it did.

RUN apk --update add \
   	tzdata \
   && cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
   && apk del tzdata
ENV TZ=America/New_York

@jektvik
Copy link

jektvik commented Jun 30, 2020

Having same issue don't know what the problem is. Running date in the shell returns the correct time but running new Date in the node REPL returns current time minus 2hrs which seems to be GMT at the time of this writing. Also can't get nodecron to work properly when setting the timezone there instead, it works against the same offset.
Something is messed up with how node gets the time from the OS.
At the same time the below works for debian stretch with no issue:

RUN ln -fs /usr/share/zoneinfo/Europe/Copenhagen /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata

@nschonni
Copy link
Member

new Date is returned in UTC https://tc39.es/ecma262/#sec-date-constructor-date

@jektvik
Copy link

jektvik commented Jun 30, 2020

@nschonni
Okay, but the offset is not recognized either:
i.e. (new Date()).getTimezoneOffset() returns 0. It should return -120.

@dominic-p
Copy link

Just ran into this myself on node:12-alpine:

$ date
Fri Aug  7 21:21:25 PDT 2020

$ node
Welcome to Node.js v12.18.3.
Type ".help" for more information.
> process.env.TZ
undefined
> new Date().toLocaleString();
'8/7/2020, 7:22:25 PM'

$ TZ=America/Los_Angeles node
Welcome to Node.js v12.18.3.
Type ".help" for more information.
> process.env.TZ
'America/Los_Angeles'
> new Date().toLocaleString();
'8/7/2020, 9:25:24 PM'

I'm guessing this means node depends on the TZ environment variable being set. Although, the 2 hours off thing is just strange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants