Proposal-temporal: Document time zone used for Instant in Intl.DateTimeFormat

Created on 20 Oct 2020  路  11Comments  路  Source: tc39/proposal-temporal

How to reproduce

I created a small runkit here with the following code:

const { Temporal, Intl } = require("proposal-temporal")

const TZ = 'Australia/Sydney';
const date = new Temporal.DateTime(2020, 4, 5, 2, 0, 0).toInstant(TZ);

console.log(new Intl.DateTimeFormat('en').format(date));

Description

And it returns 4/4/2020, 5:00:00 PM. Shouldn鈥檛 this be 4/5/2020, 2:00:00 AM instead (or at least that鈥檚 what I have from France) as the time zone is specified in the instant? Or is it normal and is meant to use my native timezone?

image

Versions

  • proposal-temporal: 0.4.0
  • node 14.14.0
documentation

Most helpful comment

After talking with @sffc, the conversion to local time is correct. The time zone used for the output should be, in order of priority:

  1. the timeZone option (e.g. new Intl.DateTimeFormat('en', { timeZone: 'Australia/Sydney' }).format(date))
  2. the -u-tz- locale extension (e.g. new Intl.DateTimeFormat('en-u-tz-ausyd').format(date)) (it actually doesn't, ECMA-402 makes an exception and ignores this key)
  3. the current time zone from the environment.
    (This is the same behaviour as Intl.DateTimeFormat has for legacy Date)

All 11 comments

It shouldn't be 2AM - Instant doesn't remember that the Sydney time zone was involved, so it only stores the UTC date/time: 2020-04-04T15:00Z. I don't think there's then supposed to be a conversion to local time, though. (The polyfill only does that because it defers to the existing in-browser implementation, which does; the spec doesn't, AFAICT.)

You should convert the Temporal.DateTime to a Temporal.ZonedDateTime instead of Temporal.Instant if you want it to remember "Australia/Sydney".

After talking with @sffc, the conversion to local time is correct. The time zone used for the output should be, in order of priority:

  1. the timeZone option (e.g. new Intl.DateTimeFormat('en', { timeZone: 'Australia/Sydney' }).format(date))
  2. the -u-tz- locale extension (e.g. new Intl.DateTimeFormat('en-u-tz-ausyd').format(date)) (it actually doesn't, ECMA-402 makes an exception and ignores this key)
  3. the current time zone from the environment.
    (This is the same behaviour as Intl.DateTimeFormat has for legacy Date)

I will keep this ticket open, so that we clarify this in the documentation.

Thanks for the report!

Thank you for those details 馃檱

You should convert the Temporal.DateTime to a Temporal.ZonedDateTime instead of Temporal.Instant if you want it to remember "Australia/Sydney".

@sffc I used toInstant because when I was playing with the polyfill to try it out, I couldn't find toZonedDateTime in DateTime's prototype. And when I try dateTime.toZondedDateTime(timezone), it crashes (runkit):

image

Yeah, ZonedDateTime hasn't landed in the polyfill yet. Soon(TM).

Also, in the current Intl section of the spec, _dateTimeFormat_.[[TimeZone]] is only taken into account in the non-temporal case. Sounds like we should fix that.

1034 should have addressed this, so closing, but feel free to reopen.

Also, in the current Intl section of the spec, dateTimeFormat.[[TimeZone]] is only taken into account in the non-temporal case. Sounds like we should fix that.

Fixed for Instant.

I think we still need to document what's mentioned in https://github.com/tc39/proposal-temporal/issues/1021#issuecomment-713015540, so reopening.

Was this page helpful?
0 / 5 - 0 ratings