doujiu8479 2017-02-17 07:11
浏览 112
已采纳

PST到UTC的Golang时间解析

I am trying to convert the time from PST to UTC timezone but seeing some unexpected result, while IST to UTC is working fine:

package main

import (
    "fmt"
    "time"
)

func main() {

    const longForm = "2006-01-02 15:04:05 MST"
    t, err := time.Parse(longForm, "2016-01-17 20:04:05 IST")
    fmt.Println(t, err)
    fmt.Printf("IST to UTC: %v

", t.UTC())

    s, err1 := time.Parse(longForm, "2016-01-17 23:04:05 PST")
    fmt.Println(s, err1)
    fmt.Printf("PST to UTC: %v

", s.UTC())

}

Output is :

2016-01-17 20:04:05 +0530 IST <nil>
IST to UTC: 2016-01-17 14:34:05 +0000 UTC

2016-01-17 23:04:05 +0000 PST <nil>
PST to UTC: 2016-01-17 23:04:05 +0000 UTC

When parsing is done for IST, it shows +0530, while for PST shows +0000 and in UTC it print same value of HH:MM:SS (23:04:05) as in PST. Am i missing anything here?

  • 写回答

2条回答 默认 最新

  • douzhuangxuan3268 2017-02-17 23:23
    关注

    The documentation for time.Parse() says:

    If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset. This choice means that such a time can be parsed and reformatted with the same layout losslessly, but the exact instant used in the representation will differ by the actual zone offset. To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation.

    Here is how to use ParseInLocation:

    IST, err := time.LoadLocation("Asia/Kolkata")
    if err != nil {
        fmt.Println(err)
        return
    }
    PST, err := time.LoadLocation("America/Los_Angeles")
    if err != nil {
        fmt.Println(err)
        return
    }
    
    const longForm = "2006-01-02 15:04:05 MST"
    t, err := time.ParseInLocation(longForm, "2016-01-17 20:04:05 IST", IST)
    fmt.Println(t, err)
    fmt.Printf("IST to UTC: %v
    
    ", t.UTC())
    
    s, err1 := time.ParseInLocation(longForm, "2016-01-17 23:04:05 PST", PST)
    fmt.Println(s, err1)
    fmt.Printf("PST to UTC: %v
    
    ", s.UTC())
    

    Output:

    2016-01-17 20:04:05 +0530 IST <nil>
    IST to UTC: 2016-01-17 14:34:05 +0000 UTC
    
    2016-01-17 23:04:05 -0800 PST <nil>
    PST to UTC: 2016-01-18 07:04:05 +0000 UTC
    

    Full code on the Go Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douying8666 2017-02-17 14:00
    关注

    The documentation for time.Parse() says:

    If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset. This choice means that such a time can be parsed and reformatted with the same layout losslessly, but the exact instant used in the representation will differ by the actual zone offset. To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation.

    So, the system doesn't know what "PST" is. For me, the system also doesn't know what IST is. You can check for supported locations like so:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        for _, name := range []string{"MST", "UTC", "IST", "PST", "EST", "PT"} {
            loc, err := time.LoadLocation(name)
            if err != nil {
                fmt.Println("No location", name)
            } else {
                fmt.Println("Location", name, "is", loc)
            }
        }
    }
    

    Output on my system:

    Location MST is MST
    Location UTC is UTC
    No location IST
    No location PST
    Location EST is EST
    No location PT
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组件库引入并使用在若依框架未展示
  • ¥149 关于#使用python 的Flash Echarts+ajax+mysql动态数据实现饼图#的问题,请各位专家解答!
  • ¥15 RichTextBox中追加文本时报错
  • ¥15 关于c语言的学习问题
  • ¥15 activity升级到flowable工作流act_ge_bytearray的草稿json数据复制到act_de_model 的model_editor_json的脚本
  • ¥15 cvi使用CreateThread创建线程时,出现存储空间不足无法处理此命令的错误
  • ¥15 求苹果推信imessage批量推信技术
  • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
  • ¥15 c++ word自动化,为什么可用接口是空的?
  • ¥15 Matlab计算100000*100000的矩阵运算问题: