duanba4942 2019-07-16 09:11
浏览 252
已采纳

通过设置本地全局设置时区

Related to Setting timezone globally in golang

In zoneinfo.go you can find the following

// Local represents the system's local time zone.
var Local *Location = &localLoc

wich suggest that you have a global modifiable Local, and you can set it. By setting it, should it have effect to timezones?

  • 写回答

1条回答 默认 最新

  • duanji1902 2019-07-16 14:47
    关注

    Setting time.Local does have an effect on the time local time zone.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        now := time.Now().Round(0)
        fmt.Println(now, time.Local)
    
        loc, err := time.LoadLocation("America/Atka")
        if err != nil {
            fmt.Println(err)
            return
        }
        time.Local = loc
    
        now = time.Now().Round(0)
        fmt.Println(now, time.Local)
    }
    

    Output:

    2019-07-16 10:45:41.263418395 -0400 EDT Local
    2019-07-16 05:45:41.263530699 -0900 HDT America/Atka
    

    Playground: https://play.golang.org/p/XBxO2toH-SJ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了