-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
When calling:
time.Parse(time.RFC3339, s)one would expect that this strictly parses s according to RFC 3339 as the constant suggests.
This parses all valid RFC 3339 timestamps (except those with leap-seconds),
however it fails to reject certain inputs that are not valid RFC 3339.
Parse differs from the RFC 3339 syntax in the following ways:
-
Parsepermits the hour fields to only have one digit, while RFC 3339 requires that it always be two digits.- See time: Parse of RFC3339 is incorrect for hour field #54568.
- For example,
0000-01-01T0:00:00Zis considered valid, when it should not be. - There is an open issue (time: one-digit hour is accepted for 15:04:05 template #29911)
requesting that "15" in the format be always parsed as a two-digit field.
However, it is unlikely that this behavior will be changed due to it being a breaking change.
-
Parsepermits a comma separator between seconds and sub-seconds, while RFC 3339 only permits a period.- See time: Parse of RFC3339 is incorrect for fractional second seperator #54571.
- For example,
0000-01-01T00:00:00,000Zis considered valid, when it should not be. - This behavior changed recently in time: support Decimal Comma for fractional seconds #43823,
which modified the behavior of "." in the format to parse either "." or ",".
While this expands the set of ISO 8601 timestamps we can handle,
it further breaks compliance with RFC 3339.
-
Parsepermits time zone offsets with any range of values, while RFC 3339 limits hours to be less than 24 and minutes to be less than 60.- For example,
0000-01-01T00:00:00+24:60is considered valid, when it should not be.
- For example,
In all situations, the time format template provides no way to strictly parse RFC 3339.
Since we can't change the text template meaning of "15" and ".",
nor can we change the constant value of RFC3339 or RFC3339Nano,
I propose that we special-case Parse to use strict parsing when handling a format template
identical to RFC3339 and RFC3339Nano.
Justification for this special-casing:
- RFC 3339 is by far the most popular time representation.
- RFC 3339 exists because ISO 8601 had too many alternate representations.
Being loose in what Go considers "valid" RFC 3339 is going against the goal of RFC 3339.
\cc @robpike