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: LoadLocation(now.Local().Location().String()) != now.Local().Location() #15568

Closed
GameXG opened this issue May 6, 2016 · 7 comments
Closed

Comments

@GameXG
Copy link

@GameXG GameXG commented May 6, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6.2 windows/amd64
  2. What operating system and processor architecture are you using (go env)?
    windows 10 x64

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\golang
set GORACE=
set GOROOT=c:\go
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=1
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

  1. What did you do?
package main

import (
    "time"
    "fmt"
)

func main() {
    now :=time.Now()

    fmt.Println("now:",now)
    fmt.Println("now.Location:",now.Location())
    fmt.Println("now.Local().Location:",now.Local().Location())

    l,err:=time.LoadLocation(now.Location().String())
    fmt.Println(l,err)
    l,err=time.LoadLocation(now.Local().Location().String())
    fmt.Println(l,err)
}

/*
now: 2016-05-06 09:47:54.1544589 +0800 CST
now.Location:
now.Local().Location:
UTC <nil>
UTC <nil>
*/
  1. What did you expect to see?
    Sorry, my English is not good.
    now.Location().String() returns "Asia/ShangHai"
  2. What did you see instead?
    In non UTC time zone, time.LoadLocation(now.Local().Location().String()) != now.Local().Location()

now.Location() returns ""
now.Local().Location() returns ""
time.LoadLocation(now.Local().Location().String()) returns "UTC"

Is it bug, or is it just like that? I can't get the local time zone.
now.Location().String() returns ""
now.Local().Location().String() returns ""

l, err := time.LoadLocation("Local")
l.String() returns ""

t2, _ := time.Parse("2006-01-02T15:04:05.999999999Z07:00 MST", time.Now().Format("2006-01-02T15:04:05.999999999Z07:00 MST"))

t2.Location() returns ""
t2.Local().Location() returns ""

@GameXG
Copy link
Author

@GameXG GameXG commented May 6, 2016

I try to use IDEA IntelliJ debugging.

Found in the abbrev function
a.std= "CST";
a.dst= "CST";
return a.std, a.dst.

But stdname, dstname :=abbrev(I) is the result of stdname = "" dstname="CST".

C:\go\src\time\zoneinfo_windows.go

func abbrev(z *syscall.Timezoneinformation) (std, dst string) {
    stdName := syscall.UTF16ToString(z.StandardName[:])
    a, ok := abbrs[stdName]
    if !ok {
        dstName := syscall.UTF16ToString(z.DaylightName[:])
        // Perhaps stdName is not English. Try to convert it.
        englishName, err := toEnglishName(stdName, dstName)
                //[ DEBUG]englishName = "China Standard Time"
        if err == nil {
            a, ok = abbrs[englishName]
            if ok {
                                // [DEBUG] a.std = "CST"  
                                // [DEBUG] a.dst = "CST"  
                return a.std, a.dst
            }
        }
        // fallback to using capital letters
        return extractCAPS(stdName), extractCAPS(dstName)
    }
    return a.std, a.dst
}

C:\go\src\time\zoneinfo_windows.go

func initLocalFromTZI(i *syscall.Timezoneinformation) {
    l := &localLoc

    nzone := 1
    if i.StandardDate.Month > 0 {
        nzone++
    }
    l.zone = make([]zone, nzone)

    stdname, dstname := abbrev(i)
        // DEBUG  stdname =""
        // DEBUG dstname ="CST"

    std := &l.zone[0]
    std.name = stdname
@GameXG
Copy link
Author

@GameXG GameXG commented May 6, 2016

Also, now.Zone() normal.

    name,offset:=t.Zone()
    fmt.Println(name,offset)

// output: CST 28800
@bradfitz bradfitz added this to the Go1.8Maybe milestone May 6, 2016
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented May 6, 2016

Sorry, I don't think we guarantee that a time.Location's String() returns a unique key which can be used to load that *Location back again.

For instance, I remember an old bug where something like LoadLocation("EST") could mean United State's east coast or Australia's east coast depending on who ran it. (I forget the details)

Some similar old bugs: #4838 #3790

I don't think there's any code to change here, but maybe we just need to document the expectations better.

@bradfitz bradfitz changed the title time.LoadLocation(now.Local().Location().String()) != now.Local().Location() time: LoadLocation(now.Local().Location().String()) != now.Local().Location() May 6, 2016
@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented May 7, 2016

I don't think there's any code to change here

What about this:

U:\>type main.go
package main

import (
        "fmt"
        "time"
)

func main() {
        fmt.Println(time.Local)
}

U:\>go run main.go


U:\>

Does it look correct to you?

Alex

@gopherbot
Copy link

@gopherbot gopherbot commented May 13, 2016

CL https://golang.org/cl/23078 mentions this issue.

gopherbot pushed a commit that referenced this issue May 14, 2016
Local.String() returns "Local" on every OS, but windows.
Change windows code to do like others.

Updates #15568

Change-Id: I7a4d2713d940e2a01cff9d7f5cefc89def07546a
Reviewed-on: https://go-review.googlesource.com/23078
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@quentinmit quentinmit added the NeedsFix label Oct 10, 2016
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 17, 2016

CL https://golang.org/cl/31144 mentions this issue.

@rsc
Copy link
Contributor

@rsc rsc commented Oct 17, 2016

CL 31144 is unrelated, sorry, I typed the wrong bug number into the commit message.

This bug seems to be fixed by Alex's commit CL 23078. Now that Local.String() returns "Local" not "", everything is OK.

@rsc rsc closed this Oct 17, 2016
@golang golang locked and limited conversation to collaborators Oct 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.