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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题