为什么我无法将已经是 RFC3339 格式的字符串解析回 RFC3339 格式的字符串?

huangapple go评论85阅读模式
英文:

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])

这段代码运行良好,并且显示如下:

为什么我无法将已经是 RFC3339 格式的字符串解析回 RFC3339 格式的字符串?

然后,当我需要从数据库中获取它时,我执行以下操作:

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:

为什么我无法将已经是 RFC3339 格式的字符串解析回 RFC3339 格式的字符串?

Then when I need to get it from the database I do this:

rows, err := db.Query(&quot;SELECT nonce, time FROM noncestore WHERE endpoint=?&quot;, endpoint)

        var sTimeStamp, nonceHolder string
        for rows.Next() {
                err = rows.Scan(&amp;nonceHolder, &amp;sTimeStamp)
                errCheck(err)

Gives error//&gt;&gt; timeStamp, err := time.Parse(time.RFC3339, sTimeStamp)
                errCheck(err)

                if timeStamp == ts &amp;&amp; nonceHolder == s {
                        return errors.New(&quot;Nonce already used&quot;)
                }

                if now.Sub(timeStamp) &lt; *maxNonceAge {
                        _, err := db.Query(&quot;INSERT INTO noncestore SET nonce=?, time=?, endpoint=?&quot;, 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 &quot;2015-08-15 03:56:00&quot; as &quot;2006-01-02T15:04:05Z07:00&quot;: cannot parse &quot; 03:56:00&quot; as &quot;T&quot;

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(&quot;2006-01-02 15:04:05&quot;, 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.

huangapple
  • 本文由 发表于 2015年8月15日 12:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/32021400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定