You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go's time library provides the time.RFC3339 layout string, which by its name is supposed to implement the RFC 3339 standard for timestamps. However, by reading the standard I found out that it's actually broader than I thought, and that the time.RFC3339 layout string actually only implements the common subset shared between RFC 3339 and ISO 8601, which consists of the timestamps where the date and time are separated by the uppercase "T" character.
I wanted full support for the RFC 3339 standard in the application I'm working on, so I tried to implement the rest by myself using time.Parse's layout string parser. By reading the standard, I came up with 3 extra layout strings to supplement time.RFC3339:
2006-01-02 15:04:05Z07:00
2006-01-02_15:04:05Z07:00
2006-01-02t15:04:05z07:00
I then implemented a function to parse an RFC 3339 timestamp by successively calling time.Parse on each different string, returning the first result that doesn't return an error, or failing if all of them return an error. A test program containing a rewritten version of this function, ready to run, is available here: https://go.dev/play/p/vl5-BbRoulL?v=gotip
I then proceeded to test this function.
What did you expect to see?
I expected all of my custom-crafted layout strings to work and all of my test values to be parsed correctly.
What did you see instead?
One of my layout strings - the one with lowercase letters, 2006-01-02t15:04:05z07:00, did not work. This is the output from the test program linked above:
Upon investigation, I landed on the following error being returned from the time.Parse function:
parsing time "2023-10-10t09:19:21.275-03:00" as "2006-01-02t15:04:05z07:00": cannot parse "-03:00" as "z07:00"
This pointed to the "z07:00" element not being recognized as a timezone element. Upon further investigation with a debugger, I landed on the function nextStdChunk() of format.go, where I found this code snippet:
The function looks for an uppercase "Z" to find the ISO timezone layout element - but nowhere in the function does it look for a lowercase "z". This means that the parser simply doesn't expect this specific case from RFC 3339.
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, both on Go 1.21 and on the unstable dev branch.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Go's
time
library provides thetime.RFC3339
layout string, which by its name is supposed to implement the RFC 3339 standard for timestamps. However, by reading the standard I found out that it's actually broader than I thought, and that thetime.RFC3339
layout string actually only implements the common subset shared between RFC 3339 and ISO 8601, which consists of the timestamps where the date and time are separated by the uppercase "T" character.I wanted full support for the RFC 3339 standard in the application I'm working on, so I tried to implement the rest by myself using
time.Parse
's layout string parser. By reading the standard, I came up with 3 extra layout strings to supplementtime.RFC3339
:2006-01-02 15:04:05Z07:00
2006-01-02_15:04:05Z07:00
2006-01-02t15:04:05z07:00
I then implemented a function to parse an RFC 3339 timestamp by successively calling
time.Parse
on each different string, returning the first result that doesn't return an error, or failing if all of them return an error. A test program containing a rewritten version of this function, ready to run, is available here: https://go.dev/play/p/vl5-BbRoulL?v=gotipI then proceeded to test this function.
What did you expect to see?
I expected all of my custom-crafted layout strings to work and all of my test values to be parsed correctly.
What did you see instead?
One of my layout strings - the one with lowercase letters,
2006-01-02t15:04:05z07:00
, did not work. This is the output from the test program linked above:Upon investigation, I landed on the following error being returned from the
time.Parse
function:This pointed to the "z07:00" element not being recognized as a timezone element. Upon further investigation with a debugger, I landed on the function
nextStdChunk()
offormat.go
, where I found this code snippet:The function looks for an uppercase "Z" to find the ISO timezone layout element - but nowhere in the function does it look for a lowercase "z". This means that the parser simply doesn't expect this specific case from RFC 3339.
The text was updated successfully, but these errors were encountered: