Skip to content

time: js/wasm sets time.Local to a UTC offset instead of the actual timezone #44408

Open
@Splizard

Description

@Splizard

In time/zoneinfo_js.go the local timezone is determined by the UTC offset returned by the JS call:

(new Date()).getTimezoneOffset()

However UTC offset != timezone and this behaviour causes issues with displaying 'daylight savings' times. This behaviour is unexpected and undocumented and I would expect that time.Local is set to the JS environment's timezone (where available) instead of the current UTC offset.

The timezone can be determined in JS with the Internationalization API:

Intl.DateTimeFormat().resolvedOptions().timeZone

As long as the tzdata package has been imported/embedded and the its init function runs first, adding the following code to the beginning of initLocal should (in theory) fix this issue.

func initLocal() {
	if intl := js.Global().Get("Intl"); !intl.IsUndefined() {
		var err error
		tz := js.Global().Get("Intl").Call("DateTimeFormat").Call("resolvedOptions").Get("timeZone").String()
		Local, err = LoadLocation(tz)
		if err == nil {
		    return
		}
	}
	...

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-JSarch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions