doutan2111
2017-11-22 15:10
浏览 82
已采纳

为什么不能time.In()在美国/纽约时区中获得正确的时间

Here is my code

package main

import (
  "fmt"
  "time"
)

func main() {
  tz := "America/New_York"
  loc, err := time.LoadLocation(tz)
  if err != nil {
    fmt.Println(err)
  }

  fmt.Println(time.Now().In(loc))
}

And it supposed to get the result like this The Go Playground

2009-11-10 18:00:00 -0500 EST

The weird thing is that it prints out the UTC time instead of EST when trying to run it on my server

2017-11-22 14:53:16.94672706 +0000 UTC

Some environments

go version go1.8.3 linux/amd64

Linux version 3.10.0-514.21.2.el7.x86_64 (mockbuild@x86-037.build.eng.bos.redhat.com) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Sun May 28 17:08:21 EDT 2017

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build674699918=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

图片转代码服务由CSDN问答提供 功能建议

这是我的代码

 包main 
 
import  (
“ fmt” 
“ time” 
)
 
func main(){
 tz:=“ America / New_York” 
 loc,err:= time.LoadLocation(tz)
 if err!  = nil {
 fmt.Println(err)
} 
 
 fmt.Println(time.Now()。In(loc))
} 
   
 
  

它应该得到这样的结果围棋场 \ n

  2009-11-10 18:00:00 -0500 EST 
   
 
 

奇怪的是,它会打印出 UTC 时间而不是 EST

  2017-11-22 14:53:16.94672706 +  0000 UTC 
   
 
 

某些环境

  go版本go1.8.3 linux / amd64  
 
Linux版本3.10.0-514.21.2.el7.x86_64(mockbuild@x86-037.build.eng.bos.redhat.com)(gcc版本4.8.5 20150623(Red Hat 4.8.5-11)(  GCC))#1 SMP Sun May 28 17:08:21 EDT 2017 
 
GOARCH =“  AMD64 “
GOBIN =” “
GOEXE =” “
GOHOSTARCH =” AMD64 “
GOHOSTOS =” linux的 “
GOOS =” linux的 “
GOPATH =”/根/去 “
GORACE =” “
GOROOT =”/ USR  / local / go“ 
GOTOOLDIR =” / usr / local / go / pkg / tool / linux_amd64“ 
GCCGO =” gccgo“ 
CC =” gcc“ 
GOGCCFLAGS =”-fPIC -m64 -pthread -fmessage-length = 0  -fdebug-prefix-map = / tmp / go-build674699918 = / tmp / go-build -gno-record-gcc-switches“ 
CXX =” g ++“ 
CGO_ENABLED =” 1“ 
PKG_CONFIG =” pkg-config“ \  nCGO_CFLAGS =“-g -O2” 
CGO_CPPFLAGS =“” 
CGO_CXXFLAGS =“-g -O2” 
CGO_FFLAGS =“-g -O2” 
CGO_LDFLAGS =“-g -O2” 
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doucigua0278 2017-11-26 05:02
    已采纳

    It turns out that the system tzdata package is out of date

    sudo yum update tzdata
    

    This command fixed the problem, as suggested by @Adrian

    打赏 评论

相关推荐 更多相似问题