第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang unmarshall mysql format time to time.Time

Golang unmarshall mysql format time to time.Time

Go
慕尼黑8549860 2022-01-17 18:26:57
我有一個2016-06-16 22:21:00XML 文檔內(nèi)的格式的時間。我想用 Golang 解析那個時間。type Price struct {    Instrument string `xml:"Instrument"`    Bid float32 `xml:"Bid"`    Ask float32 `xml:"Ask"`    Updated time.Time  `xml:"Updated"`}type Prices []Pricevar p Priceserr := 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?我已經(jīng)讀到我需要創(chuàng)建一個新的自定義時間來實現(xiàn)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
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經(jīng)驗 獲得超3個贊

你想要const shortForm = "2006-01-02 15:04:05"。這有點奇怪,但是time.Parse理解格式的方式是你必須給出這個特定的時間作為例子。請參閱https://golang.org/src/time/format.go上的評論:


// These are predefined layouts for use in Time.Format and Time.Parse.

// The reference time used in the layouts is the specific time:

//    Mon Jan 2 15:04:05 MST 2006

// which is Unix time 1136239445. Since MST is GMT-0700,

// the reference time can be thought of as

//    01/02 03:04:05PM '06 -0700

// To define your own format, write down what the reference time would look

// like formatted your way; see the values of constants like ANSIC,

// StampMicro or Kitchen for examples.

您可能可以想象為什么在這里使用預(yù)定義的日期很重要……例如,圖書館需要知道您的意思是 01/02 是 1 月 2 日還是 2 月 1 日。有一個預(yù)定義的日期(巧妙地構(gòu)造以避免重復(fù)值)可以消除歧義。


查看完整回答
反對 回復(fù) 2022-01-17
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號