Skip to content

Commit 3e887ff

Browse files
kennygrantianlancetaylor
authored andcommitted
time: document that valid layouts are not valid Parse values
For #9346 #22135 explicitly state under layout constants that they are not valid time values for Parse. Also add examples of parsing valid RFC3339 values and the layout to the example for time.Parse. Fix capitalisation of time.Parse and Time.Format. For #20869 include RFC3339 in the list of layouts that do not accept all the time formats allowed by RFCs (lowercase z). This does not fully address #20869. Fixes #9346 Fixes #22135 Change-Id: Ia4c13e5745de583db5ef7d5b1688d7768bc42c1b Reviewed-on: https://go-review.googlesource.com/74231 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent bc98cea commit 3e887ff

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/time/example_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func ExampleTime_Format() {
300300
}
301301

302302
func ExampleParse() {
303-
// See the example for time.Format for a thorough description of how
303+
// See the example for Time.Format for a thorough description of how
304304
// to define the layout string to parse a time.Time value; Parse and
305305
// Format use the same model to describe their input and output.
306306

@@ -317,9 +317,25 @@ func ExampleParse() {
317317
t, _ = time.Parse(shortForm, "2013-Feb-03")
318318
fmt.Println(t)
319319

320+
// Valid layouts may not be a valid time value, due to format specifiers
321+
// like _ for zero padding or Z for zone information.
322+
// For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
323+
// contains both Z and a time zone offset in order to handle both valid options:
324+
// 2006-01-02T15:04:05Z
325+
// 2006-01-02T15:04:05+07:00
326+
t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
327+
fmt.Println(t)
328+
t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
329+
fmt.Println(t)
330+
_, err := time.Parse(time.RFC3339, time.RFC3339)
331+
fmt.Println("error", err) // Returns an error as the layout is not a valid time value
332+
320333
// Output:
321334
// 2013-02-03 19:54:00 -0800 PST
322335
// 2013-02-03 00:00:00 +0000 UTC
336+
// 2006-01-02 15:04:05 +0000 UTC
337+
// 2006-01-02 15:04:05 +0700 +0700
338+
// error parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00
323339
}
324340

325341
func ExampleParseInLocation() {

src/time/format.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package time
66

77
import "errors"
88

9-
// These are predefined layouts for use in Time.Format and Time.Parse.
9+
// These are predefined layouts for use in Time.Format and time.Parse.
1010
// The reference time used in the layouts is the specific time:
1111
// Mon Jan 2 15:04:05 MST 2006
1212
// which is Unix time 1136239445. Since MST is GMT-0700,
@@ -18,6 +18,9 @@ import "errors"
1818
// reference time looks like so that the Format and Parse methods can apply
1919
// the same transformation to a general time value.
2020
//
21+
// Valid layouts may not be a valid time value for time.Parse, due to formats
22+
// like _ for zero padding or Z for zone information.
23+
//
2124
// Within the format string, an underscore _ represents a space that may be
2225
// replaced by a digit if the following number (a day) has two digits; for
2326
// compatibility with fixed-width Unix time formats.
@@ -49,7 +52,7 @@ import "errors"
4952
// time is echoed verbatim during Format and expected to appear verbatim
5053
// in the input to Parse.
5154
//
52-
// The executable example for time.Format demonstrates the working
55+
// The executable example for Time.Format demonstrates the working
5356
// of the layout string in detail and is a good reference.
5457
//
5558
// Note that the RFC822, RFC850, and RFC1123 formats should be applied
@@ -58,7 +61,7 @@ import "errors"
5861
// use of "GMT" in that case.
5962
// In general RFC1123Z should be used instead of RFC1123 for servers
6063
// that insist on that format, and RFC3339 should be preferred for new protocols.
61-
// RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting;
64+
// RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting;
6265
// when used with time.Parse they do not accept all the time formats
6366
// permitted by the RFCs.
6467
// The RFC3339Nano format removes trailing zeros from the seconds field
@@ -741,7 +744,7 @@ func skip(value, prefix string) (string, error) {
741744
// and convenient representations of the reference time. For more information
742745
// about the formats and the definition of the reference time, see the
743746
// documentation for ANSIC and the other constants defined by this package.
744-
// Also, the executable example for time.Format demonstrates the working
747+
// Also, the executable example for Time.Format demonstrates the working
745748
// of the layout string in detail and is a good reference.
746749
//
747750
// Elements omitted from the value are assumed to be zero or, when

0 commit comments

Comments
 (0)