我有一个2016-06-16 22:21:00XML 文档内的格式的时间。
我想用 Golang 解析那个时间。
type Price struct {
Instrument string `xml:"Instrument"`
Bid float32 `xml:"Bid"`
Ask float32 `xml:"Ask"`
Updated time.Time `xml:"Updated"`
}
type Prices []Price
var p Prices
err := xml.Unmarshal(body, &p)
if err != nil {
log.Panicln(err)
}
我的输出错误如下:
panic: parsing time "2016-06-16 20:59:57" as "2006-01-02T15:04:05Z07:00": cannot parse " 20:59:57" as "T"
如何将 mysql 格式的日期时间字符串解组为time.Time?
我已经读到我需要创建一个新的自定义时间来实现time.Time.
type customTime struct {
time.Time
}
func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
const shortForm = "2016-12-10 01:00:00" // yyyy-mm-dd hh:ii:ss date format
var v string
d.DecodeElement(&v, &start)
parse, err := time.Parse(shortForm, v)
if err != nil {
return err
}
*c = customTime{parse}
return nil
}
但是当我这样做时,我收到以下错误。
panic: parsing time "2016-06-16 20:59:57": month out of range
 慕尼黑8549860
慕尼黑8549860 
					德玛西亚99
 随时随地看视频慕课网APP
随时随地看视频慕课网APP
相关分类