我想使用 Golang 将 UTC 时间转换为特定位置的本地时间。

huangapple go评论88阅读模式
英文:

I want to Convert a UTC Time into local time by giving specific location using golang

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是一个 Golang 开发者,我正在尝试将一个 UTC 时间转换为本地时间,但我的代码不起作用。以下是我的代码:

utc := time.Now().UTC()
local := utc
location, err := time.LoadLocation("Asia/Delhi")
if err == nil {
   local = local.In(location)
}
log.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))
英文:

I am a golang developer and i am trying to convert a UTC time into local time but my code not working.Here is my code

utc := time.Now().UTC()
local := utc
location, err := time.LoadLocation("Asia/Delhi")
if err == nil {
   local = local.In(location)
}
log.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))

答案1

得分: 4

你应该重写你的代码以处理错误。默认的执行路径应该是无错误的。所以,在time.LoadLocation之后检查是否有错误:

utc := time.Now().UTC()
local := utc
location, err := time.LoadLocation("Asia/Delhi")
if err != nil {
   // 执行会停止,你也可以打印错误信息并继续使用默认值
   log.Fatal(err)
}
local = local.In(location)
log.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))

现在你会得到类似以下的错误信息:

cannot find Asia/Delhi in zip file /usr/local/go/lib/time/zoneinfo.zip
panic: time: missing Location in call to Time.In

你需要找到一个有效的时区来代表你的位置,正如其他人所说,可以在维基百科上查找时区

英文:

You should rewrite your code to handle errors when they occur. The default execution path should be error free. So, after time.LoadLocation check if there is an error:

utc := time.Now().UTC()
local := utc
location, err := time.LoadLocation("Asia/Delhi")
if err != nil {
   // execution would stop, you could also print the error and continue with default values
   log.Fatal(err)
}
local = local.In(location)
log.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))

Now you'll get something like this error message:

cannot find Asia/Delhi in zip file /usr/local/go/lib/time/zoneinfo.zip
panic: time: missing Location in call to Time.In

You have to find a valid time zome for your location, as others said check Wikipedia for time zones

答案2

得分: 3

你在第3行遇到了一个错误,只是你看不到,因为它避开了if块,所以从不更新第5行的local变量。

它失败的原因是因为'Asia/Delhi'不是一个有效的奥尔森格式。

添加一个else块,并打印出err.Error()的值。

请参考以下链接获取有效格式的列表:

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

英文:

You're getting an error on line 3, you just can't see it because it avoids the if block and so never updates the local variable on line 5.

The reason it fails is because 'Asia/Delhi' is not a valid olsen format.

Add an else block and print out the err.Error() value

See the following link for a list of valid formats:

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

答案3

得分: 0

这只是根据你指定的格式,将UTC时间字符串转换为本地时间字符串的函数。

func UTCTimeStr2LocalTimeStr(ts, layout string) (string, error) {                                                                  
    timeObject, err := time.Parse(time.RFC3339, ts)
    if err != nil {
        return "", err
    }

    return time.Unix(timeObject.Unix(), 0).Format(layout), nil
}
英文:

This is just converting UTC time string to your local time string according to your specified layout.

func UTCTimeStr2LocalTimeStr(ts, layout string) (string, error) {                                                                  
    timeObject, err := time.Parse(time.RFC3339, ts)
    if err != nil {
        return "", err
    }

    return time.Unix(timeObject.Unix(), 0).Format(layout), nil
}

huangapple
  • 本文由 发表于 2017年7月11日 15:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/45027606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定