golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.83k stars 17.65k forks source link

time.Now() returns GMT time instead of the current local time. #3617

Closed gopherbot closed 9 years ago

gopherbot commented 12 years ago

by yangchunxu:

Hi,
as package doc say: 
the time.Now() shall return local current time,but actually,the first call return GMT
time, 2nd call return local time,

t := time.Now() 
t.Hour()  //will return GMT time
t.Hour()  //will return local time

tested under window XP, go1

Before filing a bug, please check whether it has been fixed since the
latest release. Search the issue tracker and check that you're running the
latest version of Go:

Run "go version" and compare against
http://golang.org/doc/devel/release.html  If a newer version of Go exists,
install it and retry what you did to reproduce the problem.

Thanks.

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1.
2.
3.

What is the expected output?

What do you see instead?

Which compiler are you using (5g, 6g, 8g, gccgo)?

Which operating system are you using?

Which version are you using?  (run 'go version')

Please provide any additional information below.
cznic commented 12 years ago

Comment 1:

Cannot reproduce the bug on 64b Linux at tip.
alexbrainman commented 12 years ago

Comment 2:

Please, be more specific. Provide a small program for us to demonstrate your problem.
For example, I have tried this:
package main
import (
    "fmt"
    "time"
)
func main() {
    t := time.Now() 
    t1 := t.Hour()  //will return GMT time
    t2 := t.Hour()  //will return local time
    fmt.Printf("t1=%v\n", t1)
    fmt.Printf("t2=%v\n", t2)
}
And it prints
C:\>go run a.go
t1=9
t2=9
which, I suspect, is different from what you see. Please, tell us what your program
outputs, and how it is different from what you expect it to do and why. Thank you.
Alex

Owner changed to @alexbrainman.

Status changed to WaitingForReply.

gopherbot commented 12 years ago

Comment 3 by yangchunxu:

souce code: 
package main
import (
    "fmt"
    "time"
)
func main() {
    t := time.Now()
    fmt.Printf("first call: %v  2nd call: %v \n", t.Hour(),t.Hour())
    switch {
    case t.Hour() < 12:
        fmt.Println("Good morning!")
    case t.Hour() < 17:
        fmt.Println("Good afternoon.")
    default:
        fmt.Println("Good evening.")
    }
}
run result:
D:\myGo>go install timeHour
D:\myGo>bin\timehour
first call: 2  2nd call: 10
Good morning!
D:\myGo>
alexbrainman commented 12 years ago

Comment 4:

I think your issue is similar to https://golang.org/issue/3592. It
has been fixed some time ago (http://goo.gl/htyjy). Please, try latest Go version. Or at
least go1.0.1.
Alex
gopherbot commented 12 years ago

Comment 5 by yangchunxu:

Yes, there are same.
alexbrainman commented 12 years ago

Comment 6:

Status changed to Duplicate.

Merged into issue #3592.