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)

    Here is more of docs

    解决 无用
    打赏 举报
  • 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 (
    func main() {
        when := time.Now().UTC().Add(2*time.Hour)
        fmt.Println("GMT+2:", when)
    解决 无用
    打赏 举报

相关推荐 更多相似问题