dongmou3615 2015-10-05 07:34
浏览 165
已采纳

如何解析2015年10月5日星期一00:24:08 +0800(GMT + 08:00)

I'm trying to parse a time value (received via email) but I can't find what layout I should use.

package main

import "fmt"
import "time"

func main() {
    layout := "Mon, _2 Jan 2006 15:04:05 -0700 (MST-07:00)"
    data := "Mon, 5 Oct 2015 00:24:08 +0800 (GMT+08:00)"
    t, err := time.Parse(layout, data)
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
}

Check

panic: parsing time "Mon, 5 Oct 2015 00:24:08 +0800 (GMT+08:00)" as "Mon, _2 Jan 2006 15:04:05 -0700 (MST-07:00)": cannot parse ":00)" as "-07:00"

  • 写回答

2条回答 默认 最新

  • doucheng9304 2015-10-05 08:54
    关注

    Please read the Important note! section to get a full picture as the last part including GMT is not what it seems (that is, it is not the zone abbreviation).


    Your input time string is not parsable as-is with the time package because in the last part (GMT+08:00) the zone abbreviation and the zone offset are not separated with any extra characters. And since the zone abbreviation is not limited to 3 characters, the "+08" is also treated as part of the zone abbreviation, which will only leave the rest ":00)" to match the zone offset "-07:00" hence the error message:

    cannot parse ":00)" as "-07:00"
    

    Possible workaround:

    Simply cut off the extra zone offset as it is redundant (duplicate because zone offset is included twice):

    func Parse(s string) (time.Time, error) {
        if len(s) < 7 {
            return time.Time{}, errors.New("Too short!")
        }
        s = s[:len(s)-7]
        layout := "Mon, _2 Jan 2006 15:04:05 -0700 (MST"
        return time.Parse(layout, s)
    }
    

    And using it:

    data := "Mon, 5 Oct 2015 00:24:08 +0800 (GMT+08:00)"
    t, err := Parse(data)
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
    

    Try it on the Go Playground.

    Important note!

    Although logically analyzing your input time string:

    Mon, 5 Oct 2015 00:24:08 +0800 (GMT+08:00)
    

    The last part "(GMT+08:00)" does not denote the zone abbreviation (GMT is not +0800 but +0000)! It is just a helper text to the reader explaining that the +0800 is a zone offset to be added to the GMT timezone, so most likely it will always be GMT but not because the time is specified in the GMT timezone but because it explains that the zone offset means an offset to be added to the GMT time. And as such, it is not part of the time specification and should not be parsed.

    So instead cut off all the last part and parse it like this:

    func Parse(s string) (time.Time, error) {
        if len(s) < 12 {
            return time.Time{}, errors.New("Too short!")
        }
        s = s[:len(s)-12]
        layout := "Mon, 2 Jan 2006 15:04:05 -0700"
        return time.Parse(layout, s)
    }
    

    Also note that if this is really the case (that it is not the zone abbreviation but it will always be "GMT"), you can parse the input string without truncating it by changing your layout to include the static text "GMT" instead of the zone abbreviation specifier "MST":

    layout := "Mon, 2 Jan 2006 15:04:05 -0700 (GMT-07:00)"
    data := "Mon, 5 Oct 2015 00:24:08 +0800 (GMT+08:00)"
    t, err := time.Parse(layout, data)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大