英文:
How come I cannot parse an already RFC3339 string back to a RFC3339 string?
问题
我正在将一个变量存储到一个字段类型为VARCHAR(255)
的MySQL数据库中,代码如下:
ts, err := time.Parse(time.RFC3339, nonce[0:20])
这段代码运行良好,并且显示如下:
然后,当我需要从数据库中获取它时,我执行以下操作:
rows, err := db.Query("SELECT nonce, time FROM noncestore WHERE endpoint=?", endpoint)
var sTimeStamp, nonceHolder string
for rows.Next() {
err = rows.Scan(&nonceHolder, &sTimeStamp)
errCheck(err)
出错的地方//>> timeStamp, err := time.Parse(time.RFC3339, sTimeStamp)
errCheck(err)
if timeStamp == ts && nonceHolder == s {
return errors.New("Nonce already used")
}
if now.Sub(timeStamp) < *maxNonceAge {
_, err := db.Query("INSERT INTO noncestore SET nonce=?, time=?, endpoint=?", nonceHolder, sTimeStamp, endpoint)
errCheck(err)
}
}
上面标注的那行代码给我报了这个错误:
2015/08/14 21:56:18 http: panic serving [::1]:49663: Failed: parsing time "2015-08-15 03:56:00" as "2006-01-02T15:04:05Z07:00": cannot parse " 03:56:00" as "T"
为什么它能够将其转换为RFC3339
并存储到数据库中,但当我需要将其格式化回RFC3339
以便在if语句中使用时,却出现错误?
英文:
I'm storing a variable into a MySQL database with the field type of VARCHAR(255)
like this:
ts, err := time.Parse(time.RFC3339, nonce[0:20])
which works great and shows up like this:
Then when I need to get it from the database I do this:
rows, err := db.Query("SELECT nonce, time FROM noncestore WHERE endpoint=?", endpoint)
var sTimeStamp, nonceHolder string
for rows.Next() {
err = rows.Scan(&nonceHolder, &sTimeStamp)
errCheck(err)
Gives error//>> timeStamp, err := time.Parse(time.RFC3339, sTimeStamp)
errCheck(err)
if timeStamp == ts && nonceHolder == s {
return errors.New("Nonce already used")
}
if now.Sub(timeStamp) < *maxNonceAge {
_, err := db.Query("INSERT INTO noncestore SET nonce=?, time=?, endpoint=?", nonceHolder, sTimeStamp, endpoint)
errCheck(err)
}
}
The line noted above gives me this error:
2015/08/14 21:56:18 http: panic serving [::1]:49663: Failed: parsing time "2015-08-15 03:56:00" as "2006-01-02T15:04:05Z07:00": cannot parse " 03:56:00" as "T"
How come it is able to convert it to RFC3339
and store it in the database, but when it comes time to get it and format it back to RFC3339
so that I can use in my if statement I get an error?
答案1
得分: 2
看起来你的数据库驱动程序返回的时间格式不符合RFC3339标准。你可以尝试使用以下代码替代:
timeStamp, err := time.Parse("2006-01-02 15:04:05", sTimeStamp)
需要记住的一点是,数据库本身不会以原始字符串的形式存储时间,无论是RFC3339还是其他格式。它有自己的内部时间表示方式,当你查询时间时,你得到的值取决于数据库希望如何向你表示它。在这种情况下,它选择了一种稍微不同的格式,上面我粘贴的格式字符串(以"2006-01-02..."开头的那个)应该可以解决问题。是的,在Go语言中,日期格式字符串看起来像日期本身。
英文:
It looks like your database driver returns times in a format that's not in RFC3339. Instead of using
timeStamp, err := time.Parse(time.RFC3339, sTimeStamp)
try using
timeStamp, err := time.Parse("2006-01-02 15:04:05", sTimeStamp)
An important thing to remember is that the database itself is not going to store raw strings at all, RFC3339 or otherwise. It has its own internal representation of time, and when you query for it, the value you get depends on how that db wishes to reperesent it to you. In this case, it's chosen a somewhat different format, which the format string I've pasted above (the one that starts with "2006-01-02...") should fix. And yes, in Go, date format strings look like dates themselves.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论