doushi7761
2016-06-03 07:48
浏览 711
已采纳

在GoLang中使用GMT + 2获取当前时间

I need to get the actual time in GMT+2 (Rome, Italy) in Go language.

I've used: time.Now() but it returns the actual date in GMT+0.

I've seen that there's a In(loc *Location) function but I can't understand how to use it. And also, do you know if setting GMT+2, it also consider the DST option in automatic?

Could you help me? Thanks

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

我需要用Go语言获取GMT + 2(意大利罗马)的实际时间。

我使用过: time.Now() ,但它返回的实际日期为GMT + 0。

我已经看到有一个In(loc * Location)函数,但是我不知道如何使用它。 而且,您是否知道是否将GMT + 2设置为自动选择DST选项?

您能帮我吗? 谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhao6584 2016-06-03 08:16
    已采纳

    You can user the function FixedZone or LoadLocation to get a *Location And then use that *Location in func In

    // get the location
    location,_ := time.LoadLocation("Europe/Rome")
    
    // this should give you time in location
    t := time.Now().In(location)
    
    fmt.Println(t)
    

    Here is more of docs https://golang.org/pkg/time/#Time

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douquan1953 2016-06-03 08:54

    Another method to get the current time is to get GMT time (UTC) and add the number of hours you need, but this way does not take into account DST, and yields precisely GMT+2 (GMT does not change with daylight saving time).

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        when := time.Now().UTC().Add(2*time.Hour)
        fmt.Println("GMT+2:", when)
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题