【问题标题】:how to convert UTC to India local time using golang如何使用golang将UTC转换为印度当地时间
【发布时间】:2018-07-03 11:57:46
【问题描述】:

提到下面的代码

 loc, _ := time.LoadLocation("Asia/Kolkata")
        now := time.Now().In(loc)
        fmt.Println("Location : ", loc, " Time : ", now)
        visit.Time = now

获取 UTC 时间,但我需要在我的数据存储中获取 IST

【问题讨论】:

  • 它适用于 Go Playground,并在 IST 中提供时间:playground。很可能您的LoadLocation() 失败,请检查返回的错误!为什么用google-cloud-datastore标记?
  • 您没有显示打印语句的输出。任何尝试的人都会看到..... ISTvisit.Time 的值将只是一个时间戳(时区仅用于显示),如果您希望将其显示在 IST 中,则需要在打印时指定时区。
  • @Marc 地点:亚洲/加尔各答时间:2009-11-11 04:30:00 +0530 IST,但仅获得 UCT 时间
  • 或者你说的是操场上的输出?这不会是实际时间,因为操场伪造了很多系统实用程序。详情请见this blog post

标签: datetime go time


【解决方案1】:

Go Playground 中,这是按预期工作的。

About the Playground

Go Playground 是在 golang.org 的服务器上运行的 Web 服务。 该服务接收一个 Go 程序,编译、链接并运行 在沙箱中运行程序,然后返回输出。

可以运行的程序有限制 游乐场:

在操场上,时间从 2009 年 11 月 10 日 23:00:00 UTC 开始 (确定这个日期的重要性是 读者)。这使得通过给它们缓存程序更容易 确定性输出。

文章“Inside the Go Playground”描述了操场是怎样的 实施。

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, _ := time.LoadLocation("Asia/Kolkata")
    now := time.Now().In(loc)
    fmt.Println("Location : ", loc, " Time : ", now)
}

游乐场:https://play.golang.org/p/l8t3BnQATg7

输出:

Location :  Asia/Kolkata  Time :  2009-11-11 04:30:00 +0530 IST

【讨论】:

    【解决方案2】:

    IST 的正确参数是“亚洲/加尔各答”而不是“亚洲/加尔各答”。这是完整列表https://golang.org/src/time/zoneinfo_abbrs_windows.go

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2018-11-12
      • 2015-09-26
      • 1970-01-01
      • 2011-03-25
      相关资源
      最近更新 更多