doupingdiao3546 2017-09-15 18:46
浏览 155
已采纳

在知道UTC时间和时间偏移时如何设置Go时间区域的值?

I have an UTC time and a time offset in seconds, and need to return the corresponding Go time value.

It is trivial to instantiate the UTC time value by using the time.Unix() function. But to set the Zone, I need to determine the time.Location.

How can I find the time.Location when knowing the UTC time and time offset ?

  • 写回答

1条回答 默认 最新

  • douxiangbiao1899 2017-09-15 18:52
    关注

    Without an actual entry to lookup in the time zone database, you can't know the true location for the time. If you want to work with just an offset, you can create a fixed location using time.FixedZone

    edt := time.FixedZone("EDT", -60*60*4)
    t, _ := time.ParseInLocation("02 Jan 06 15:04", "15 Sep 17 14:55", edt)
    fmt.Println(t)
    
    // 2017-09-15 14:55:00 -0400 EDT
    

    You can opt to specify a non-existent zone name, or none at all, as long as the output format you use doesn't require one.

    minus4 := time.FixedZone("", -60*60*4)
    t, _ = time.ParseInLocation("02 Jan 06 15:04", "15 Sep 17 14:55", minus4)
    fmt.Println(t.Format(time.RFC3339))
    
    // 2017-09-15T14:55:00-04:00
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题