yumaa
yumaa

Reputation: 1075

TZ=UTC+03:00 vs TZ=UTC+3 difference for Nodejs

Can someone explain to me, why TZ=UTC+03:00 gives timezone offset -180, and TZ=UTC+3180?

$ TZ=UTC+03:00 node -e 'console.log((new Date).getTimezoneOffset())'
-180

$ TZ=UTC+3 node -e 'console.log((new Date).getTimezoneOffset())'
180

At first I thought it is some Nodejs parsing responsibility, tried to find anything in Nodejs documentation, but failed. I tried to search through Nodejs sources, but it didn't work either. Then I started to think, that this relates to Posix and described somewhere in C/C++ libraries documentation. Failed to find it either...

And looks like when setting TZ for date command both variants gives same timezone.

$ TZ=UTC+03:00 date +'%Z%z'
UTC-0300

$ TZ=UTC+3 date +'%Z%z'
UTC-0300

I'm not interested in how should I set TZ correctly. I'm curious, WHY this different results.

UPD: Looks like rabbit hole is deeper, than I thought:

$ TZ=UTC+01:00 node -e 'console.log((new Date).getTimezoneOffset())'
-180

$ TZ=UTC+02:00 node -e 'console.log((new Date).getTimezoneOffset())'
-180

$ TZ=UTC+10:00 node -e 'console.log((new Date).getTimezoneOffset())'
-180

Does Nodejs just ignore it? (my local timezone is Europe/Istanbul, hence offset -180)

Upvotes: 0

Views: 1298

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241959

The TZ environment variable supports two different formats:

  • It can be a POSIX time zone string, such as:

    • NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0
    • PST8PDT
    • CET-1
  • It can be an IANA time zone identifier, such as:

    • America/Los_Angeles
    • Africa/Nairobi
    • Etc/GMT-3

In the POSIX format, (and in the IANA identifiers of form Etc/GMT*) the offsets are inverse from the normal ISO 8601 convention. That is, Etc/GMT+3 (or FOO3BAR) is what is usually written as -03:00 - that is, 3 hours behind GMT.

When you pass TZ=UTC+3, since UTC+3 is not a valid IANA identifier, it is interpreted as a fixed offset time zone labeled "UTC" that is 3 hours behind the actual UTC.

In the case where the TZ string is neither a valid IANA identifier nor a valid POSIX string, Node will fallback to use your system's local time zone.

There does appear to be a bug with Node (or perhaps with ICU) in that UTC+03:00 should also be interpreted as a POSIX string. While not commonly used, it does meet the [+|-]hh[:mm[:ss]] format described in the TZ specification. I've raised a bug for this here: https://github.com/nodejs/node/issues/46246

Upvotes: 2

Related Questions