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

time: (Time).Zone() returns empty timezone string "" on Darwin and "UTC" on Linux after UnmarshalText("9066-03-06T6:05:06Z") or using any valid year #45909

Closed
odeke-em opened this issue May 2, 2021 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@odeke-em
Copy link
Member

odeke-em commented May 2, 2021

What version of Go are you using (go version)?

Go1.17, Go1.16, Go1.15 the rest I haven't tested

Does this issue reproduce with the latest release?

Yes!

What operating system and processor architecture are you using (go env)?

Alternating between Darwin and Linux

What did you do?

Looking a bug reported from oss-fuzz that just came from a reflection check failure when the location of a time parsed from
"9066-03-06T6:05:06-00:00" via UnmarshalText and round trip MarshalText then UnmarshalText again per https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=33092&q=label%3AProj-golang

while digging more, I ran https://play.golang.org/p/ealMZGPl7NX or inlined below

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	var t1 time.Time
	b1 := []byte("9066-03-06T6:05:06Z")
	if err := t1.UnmarshalText(b1); err != nil {
		panic(err)
	}
	println(runtime.Version(), runtime.GOOS)
	var t2 time.Time
	b2, err := t1.MarshalText()
	if err != nil {
		panic(err)
	}
	if err := t2.UnmarshalText(b2); err != nil {
		panic(err)
	}
	z1, o1 := t1.Zone()
	z2, o2 := t2.Zone()
	fmt.Printf("t1:: %#v\nt2:: %#v\nb1:: %q\nb2:: %q\n", t1, t2, b1, b2)
	fmt.Printf("t1:: zone: %q offset: %d\n", z1, o1)
	fmt.Printf("t2:: zone: %q offset: %d\n", z2, o2)
}

What did you expect to see?

On both Linux and Darwin, I expected to see at the end

t1:: zone: "UTC" offset: 0
t2:: zone: "UTC" offset: 0

What did you see instead?

On Linux, I correctly at the end see

t1:: zone: "UTC" offset: 0
t2:: zone: "UTC" offset: 0

on Darwin, I see only

t1:: zone: "" offset: 0
t2:: zone: "UTC" offset: 0
@odeke-em
Copy link
Member Author

odeke-em commented May 2, 2021

By the way, the problem persists even with any valid year range e.g. 2020, 2021 etc.

@odeke-em odeke-em changed the title time: (Time).Zone() returns empty timezone string "" on Darwin and "UTC" on Linux after UnmarshalText("9066-03-06T6:05:06Z") time: (Time).Zone() returns empty timezone string "" on Darwin and "UTC" on Linux after UnmarshalText("9066-03-06T6:05:06Z") or using any valid year May 2, 2021
@odeke-em odeke-em added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 2, 2021
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented May 3, 2021

I can't recreate this on the darwin-amd64-11 gomote.

devel gomote.XXXXX darwin
t1:: time.Date(9066, time.March, 6, 6, 5, 6, 0, time.UTC)
t2:: time.Date(9066, time.March, 6, 6, 5, 6, 0, time.UTC)
b1:: "9066-03-06T6:05:06Z"
b2:: "9066-03-06T06:05:06Z"
t1:: zone: "UTC" offset: 0
t2:: zone: "UTC" offset: 0

@odeke-em
Copy link
Member Author

odeke-em commented May 3, 2021

My uname information is:

$ uname -a
Darwin Emmanuels-MacBook-Pro-2.local 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64 x86_64

@ianlancetaylor
Copy link
Contributor

Also worked as expected for me on the Darwin 10.15 gomote. I'm not sure what is happening here.

@seankhliao
Copy link
Member

@odeke-em is b1 the string in the example program or the string in the oss-fuzz report?

9066-03-06T6:05:06Z roundtrips successfully for me on both mac and linux while 9066-03-06T6:05:06-00:00 fails for both

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 20, 2022
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@sourabhswain
Copy link

sourabhswain commented May 12, 2023

I face the same issue. The zone is "". Please see here : https://go.dev/play/p/677G6V48r6s

package main

import (
	"fmt"
	"time"
)

func main() {

	t, _ := time.Parse(time.RFC3339, "1970-01-20T12:44:37+01:00")
	zone, offset := t.Zone()
	fmt.Println(zone, offset)
}
    

@ianlancetaylor
Copy link
Contributor

@sourabhswain That program is behaving as documented. Quoting https://pkg.go.dev/time#Parse

When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location (Local), then Parse uses that location and zone in the returned time. Otherwise it records the time as being in a fabricated location with time fixed at the given zone offset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants