aboutsummaryrefslogtreecommitdiff
path: root/src/time/format.go
AgeCommit message (Collapse)Author
2023-02-16src: rename unexported errors by adding prefix errOleksandr Redko
By convention, use `err` as prefix for variables of type `error`. Change-Id: I9401d5d47e994a27be245b2c8b1edd55cdd52db1 Reviewed-on: https://go-review.googlesource.com/c/go/+/467536 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-11-18all: add missing periods in commentscui fliter
Change-Id: I69065f8adf101fdb28682c55997f503013a50e29 Reviewed-on: https://go-review.googlesource.com/c/go/+/449757 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Joedian Reid <joedian@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-11-17time: avoid creating a parse error from the next chunk of the valueZeke Lu
When it reports a parse error, it uses the "value" variable as the value element of the parse error. Previously, in some of the cases, the "value" variable is always updated to the next chunk of the value to be parsed (even if an earlier chunk is invalid). The reported parse error is confusing in this case. This CL addresses this issue by holding the original value, and when it fails to parse the time, use it to create the parse error. Fixes #56730. Change-Id: I445b1d8a1b910208d0608b2186881746adb550e0 GitHub-Last-Rev: 67b1102b5e9b345beb2ddcc529a8e608e5afc865 GitHub-Pull-Request: golang/go#56754 Reviewed-on: https://go-review.googlesource.com/c/go/+/450936 Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Joedian Reid <joedian@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
2022-10-24time: optimize appendInt and appendNanosJoe Tsai
The appendInt function previously performed a double pass over the formatted integer. We can avoid the second pass if we knew the exact length of formatted integer, allowing us to directly serialize into the output buffer. Rename formatNano to appendNano to be consistent with other append-like functionality. Performance: name old time/op new time/op delta FormatRFC3339Nano 109ns ± 1% 72ns ± 1% -34.06% (p=0.000 n=10+10) Change-Id: Id48f77eb4976fb1dcd6e27fb6a02d29cbf0c026a Reviewed-on: https://go-review.googlesource.com/c/go/+/444278 Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com>
2022-10-20time: implement strict RFC 3339 during marshal and unmarshalJoe Tsai
We add strict checking to marshal and unmarshal methods, rather than Parse to maintain compatibility in Parse behavior. Also, the Time.Format method has no ability to report errors. The Time.Marshal{Text,JSON} and Time.Unmarshal{Time,JSON} methods are already documented as complying with RFC 3339, but have edge cases on both marshal and unmarshal where it is incorrect. The Marshal methods already have at least one check to comply with RFC 3339, so it seems sensible to expand this to cover all known violations of the specification. This commit fixes all known edge cases for full compliance. Two optimizations are folded into this change: 1. parseRFC3339 is made generic so that it can operate directly on a []byte as well as string. This avoids allocating or redundant copying when converting from string to []byte. 2. When marshaling, we verify for correctness based on the serialized output, rather than calling attribute methods on the Time type. For example, it is faster to check that the 5th byte is '-' rather than check that Time.Year is within [0,9999], since Year is a relatively expensive operation. Performance: name old time/op new time/op delta MarshalJSON 109ns ± 2% 99ns ± 1% -9.43% (p=0.000 n=10+10) UnmarshalText 158ns ± 4% 143ns ± 1% -9.17% (p=0.000 n=9+9) Updates #54580 Updates #54568 Updates #54571 Change-Id: I1222e45a7625d1ffd0629be5738670a84188d301 Reviewed-on: https://go-review.googlesource.com/c/go/+/444277 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-20time: optimize Parse for []byte argumentsJoe Tsai
When one has a []byte on hand, but desires to call the Parse function, the conversion from []byte to string would allocate. This occurs frequently through UnmarshalText and UnmarshalJSON. This changes it such that the input string never escapes from any of the Parse functions. Together with the compiler optimization where the compiler stack allocates any string smaller than 32B this makes most valid inputs for Parse(layout, string(input)) not require an allocation for the input string. This optimization works well for most RFC3339 timestamps. All timestamps with second resolution (e.g., 2000-01-01T00:00:00Z or 2000-01-01T00:00:00+23:59) or timestamps with nanosecond resolution in UTC (e.g., 2000-01-01T00:00:00.123456789Z) are less than 32B and benefit from this optimization. Unfortunately, nanosecond timestamps with non-UTC timezones (e.g., 2000-01-01T00:00:00.123456789+23:59) do not benefit since they are 35B long. Previously, this was not possible since the input leaked to the error and calls to FixedZone with the zone name, which causes the prover to give up and heap copy the []byte. We fix this by copying the input string in both cases. The advantage of this change is that you can now call Parse with a []byte without allocating (most of the times). The detriment is that the timezone and error path has an extra allocation. Handling of timezones were already expensive (3 allocations and 160B allocated), so the additional cost of another string allocation is relatively minor. We should optimize for the common case, rather than the exceptional case. Performance: name old time/op new time/op delta ParseRFC3339UTCBytes 54.4ns ± 1% 40.3ns ± 1% -25.91% (p=0.000 n=9+10) Now that parsing of RFC3339 has been heavily optimized in CL 425197, the performance gains by this optimization becomes relatively more notable. Related to CL 345488. Change-Id: I2a8a9cd6354b3bd46c2f57818ed2646a2e485f36 Reviewed-on: https://go-review.googlesource.com/c/go/+/429862 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-15time: move RFC 3339 optimizations to separate fileJoe Tsai
The optimizations were added in CL 425197 and CL 421877. Move this functionality to a separate file to keep format.go smaller and to document the justification for why this optimization exists. Change-Id: I1e4e1ace19f9d596d8c0cf49ab6062f63a87b5bf Reviewed-on: https://go-review.googlesource.com/c/go/+/430675 Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Jenny Rakoczy <jenny@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Jenny Rakoczy <jenny@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Jenny Rakoczy <jenny@golang.org> Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
2022-09-13time: optimize Parse for RFC3339 and RFC3339NanoJoe Tsai
RFC 3339 is the most common time representation, being used in an overwhelming 57.3% of all specified formats, while the next competitor only holds 7.5% usage. Specially optimize parsing to handle the RFC 3339 format. To reduce the complexity of error checking, parseRFC3339 simply returns a bool indicating parsing success. It leaves error handling to the general parse path. To assist in fuzzing, the internal parse function was left unmodified so that we could test that parseRFC3339 and parse agree with each other. Performance: name old time/op new time/op delta ParseRFC3339UTC 112ns ± 1% 37ns ± 1% -67.37% (p=0.000 n=9+9) ParseRFC3339TZ 259ns ± 2% 67ns ± 1% -73.92% (p=0.000 n=10+9) Credit goes to Amarjeet Anand for a prior CL attemping to optimize this. See CL 425014. Fixes #54093 Change-Id: I14f4e8c52b092d44ceef6863f261842ed7e83f4c Reviewed-on: https://go-review.googlesource.com/c/go/+/425197 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Jenny Rakoczy <jenny@golang.org>
2022-08-29time: add fuzz test for Time.appendFormatRFC3339Joe Tsai
Time.appendFormatRFC3339 is a specialized implementation of Time.appendFormat. We expect the two to be identical. Add a fuzz test to ensure this property. Updates #54093 Change-Id: I0bc41ee6e016d3dac75d1ac372d8c9c7266d0299 Reviewed-on: https://go-review.googlesource.com/c/go/+/425100 Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-08-23all: append(bytes, str...) works out of the boxDaniel Martí
From the append docs in the builtin package: As a special case, it is legal to append a string to a byte slice, like this: slice = append([]byte("hello "), "world"...) Change-Id: Ib14039a7476873b12a3aefccd8863e8d628b9249 Reviewed-on: https://go-review.googlesource.com/c/go/+/425102 Reviewed-by: hopehook <hopehook@qq.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
2022-08-23time: fix Parse for time zonesJoe Tsai
The hours, minutes, and seconds fields for time zones should not have any plus or minus signs. Use getnum instead of atoi since the latter implicitly handles leading signs, while the former does not. Fixes #54570 Change-Id: If9600170af3af999739c27d81958e3649946913a Reviewed-on: https://go-review.googlesource.com/c/go/+/425038 Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Rob Pike <r@golang.org>
2022-08-23time: fix Parse for empty secondsJoe Tsai
The error return value of the seconds field is overwritten and not checked in the presence of a fractional second. Perform an explicit check for errors. Fixes #54569 Change-Id: I1204c8bdcd5a5a09b773d9e44748141ed1e5cb20 Reviewed-on: https://go-review.googlesource.com/c/go/+/425036 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Rob Pike <r@golang.org>
2022-08-23time: fix Parse to ignore extra sub-nanosecond digitsJoe Tsai
This modifies the code to match the comment such that the behavior truly is identical to stdSecond case. Also, it modifies the behavior to match the documented behavior where: Fractional seconds are truncated to nanosecond precision. Fixes #54567 Updates #48685 Change-Id: Ie64549e4372ab51624c105ad8ab4cc99b9b5a0b3 Reviewed-on: https://go-review.googlesource.com/c/go/+/425037 Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
2022-08-17time: optimize GoStringAmarjeet Anand
Optimize Time.GoString by avoiding multiple calls to absDate. name old time/op new time/op delta GoString-8 313ns ± 2% 197ns ± 1% -37.08% (p=0.008 n=5+5) name old alloc/op new alloc/op delta GoString-8 80.0B ± 0% 80.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta GoString-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) Fixes #54436 Change-Id: I8e6f8e7bbb9857b4bc0cdf6ed29a6b2415775db7 Reviewed-on: https://go-review.googlesource.com/c/go/+/423634 Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-08-11time: optimize Format for RFC3339 and RFC3339NanoAmarjeet Anand
Optimise Format for the most frequently used RFC3339 and RFC3339Nano layouts by avoiding parsing of layout. > benchstat oldBench.txt newBench.txt name old time/op new time/op delta FormatRFC3339-8 302ns ± 1% 203ns ± 0% -32.89% (p=0.016 n=5+4) FormatRFC3339Nano-8 337ns ± 1% 219ns ± 1% -34.91% (p=0.008 n=5+5) name old alloc/op new alloc/op delta FormatRFC3339-8 32.0B ± 0% 32.0B ± 0% ~ (all equal) FormatRFC3339Nano-8 32.0B ± 0% 32.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta FormatRFC3339-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) FormatRFC3339Nano-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) Fixes #54093 Change-Id: Ifc84fce6078e24514ecbcd234875bca4aaab5e0e Reviewed-on: https://go-review.googlesource.com/c/go/+/421877 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
2022-08-09time: add DateTime, DateOnly, and TimeOnlyJoe Tsai
Add named constants for the 3rd, 4th, and 13th most popular formats. Fixes #52746 Change-Id: I7ce92e44dcae18c089124f1d6f5bc2d6359d436c Reviewed-on: https://go-review.googlesource.com/c/go/+/412495 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
2022-07-25time: clarify documentation for allowed formats and add tests to prove themMarcus Watkins
The existing documentation for the time.Layout const states "Only these values are recognized", but then doesn't include the numeric forms for month leading to ambiguity and assumptions that may not be true. It's unclear, for example, that space padding is only available for day of the month. Finally I add tests to show the behaviors in specific scenarios. Change-Id: I4e08a14834c17b6bdf3b6b47d39dafa8c1a138fb Reviewed-on: https://go-review.googlesource.com/c/go/+/418875 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-04-27time: document hhmmss formatscuiweixie
Fixes #52516 Change-Id: I173fdb09c245563e09be4e1aacfd374c3a764d74 GitHub-Last-Rev: 14a81e50616e0f268fee9323d0621de861885475 GitHub-Pull-Request: golang/go#52538 Reviewed-on: https://go-review.googlesource.com/c/go/+/402058 Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-04-11all: gofmt main repoRuss Cox
[This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-04-01all: remove trailing blank doc comment linesRuss Cox
A future change to gofmt will rewrite // Doc comment. // func f() to // Doc comment. func f() Apply that change preemptively to all doc comments. For #51082. Change-Id: I4023e16cfb0729b64a8590f071cd92f17343081d Reviewed-on: https://go-review.googlesource.com/c/go/+/384259 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-02-15time: document that Parse truncates to nanosecond precisionIan Lance Taylor
For #48685 Fixes #50806 Change-Id: Ie8be40e5794c0998538890a651ef8ec92cb72d3a Reviewed-on: https://go-review.googlesource.com/c/go/+/381155 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Paul Jolly <paul@myitcv.org.uk> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2021-10-08time: allow minimum int64 in ParseDurationMeng Zhuo
ParseDuration should handle minimum int64 (-1<<63) nanosecond since type Duration is alias of int64 name old time/op new time/op delta ParseDuration 91.4ns ± 0% 86.4ns ± 1% -5.49% (p=0.000 n=9+8) Fixes: #48629 Change-Id: I81b7035b25cefb4c1e5b7801c20f2d335e29358a Reviewed-on: https://go-review.googlesource.com/c/go/+/352269 Trust: Meng Zhuo <mzh@golangcn.org> Run-TryBot: Meng Zhuo <mzh@golangcn.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-10-06time: truncate fractional seconds longer than 9 digitsAlexander Yastrebov
Fixes #48685 Change-Id: Id246708878c2902b407ab759537f6b545a1f459f GitHub-Last-Rev: 4d985192c5a66ae8891539f166ef88b53cd1cbea GitHub-Pull-Request: golang/go#48750 Reviewed-on: https://go-review.googlesource.com/c/go/+/353713 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2021-09-17time: re-add space-padded day of year to docsRiley Avron
CL 320252 reworked the time docs, but accidentally deleted the format __2 from the sentence describing the three-character day of year component. Change-Id: I3f583733028657c2a677358a25e062ea81835ce8 GitHub-Last-Rev: 2fa98324191500fd6a37097a9712ae23cc509269 GitHub-Pull-Request: golang/go#48387 Reviewed-on: https://go-review.googlesource.com/c/go/+/349929 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Carlos Amedee <carlos@golang.org>
2021-09-09time: propagate "," separator for fractional seconds into Formatkorzhao
In CL 300996 that fixed issue #6189, we made Parse recognize "," as a separator for fractional seconds. However, we didn't modify Format to propagate the separator verbatim from Parse. Without this change, we break prior functionality that relied on a comma being used in Format. Fixes #48037 Change-Id: I6565a25e8657ca3747a58b25acba58f27cdcddc0 Reviewed-on: https://go-review.googlesource.com/c/go/+/345438 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Cherry Mui <cherryyz@google.com>
2021-08-25time/format: avoid growslice in time.String()/time.GoString()korzhao
Pre-allocate the slice of buf with enough capacity to avoid growslice calls. benchmark old ns/op new ns/op delta BenchmarkTimeString-4 493 409 -17.12% BenchmarkTimeGoString-4 309 182 -41.30% benchmark old allocs new allocs delta BenchmarkTimeString-4 5 3 -40.00% BenchmarkTimeGoString-4 4 1 -75.00% benchmark old bytes new bytes delta BenchmarkTimeString-4 152 128 -15.79% BenchmarkTimeGoString-4 248 80 -67.74% Change-Id: I64eabe2ab0b3d4a846453c2e8e548a831d720b8c Reviewed-on: https://go-review.googlesource.com/c/go/+/343971 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Alexander Rakoczy <alex@golang.org>
2021-08-12time: fix docs for new comma layoutsRuss Cox
The current text is slightly inaccurate. Make it more correct. Change-Id: Iebe0051b74649d13982d7eefe3697f9e69c9b75d Reviewed-on: https://go-review.googlesource.com/c/go/+/340449 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-06-24time: handle invalid UTF-8 byte sequences in quote to prevent panicAndy Pan
Fixes #46883 Updates CL 267017 Change-Id: I15c307bfb0aaa2877a148d32527681f79df1a650 Reviewed-on: https://go-review.googlesource.com/c/go/+/330289 Reviewed-by: Kevin Burke <kev@inburke.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2021-05-18time: rewrite the documentation for layout stringsRob Pike
People continue to be confused by how these work. Address that by some rejiggering. Introduce a constant called Layout that both defines the time and provides a reference point for Parse and Format to refer to. We can then delete much redundancy, especially for Format's comments, but Parse tightens a bit too. Then change the way the concept of the layout string is introduced, and provide a clearer catalog of what its elements are. Fixes #38871 Change-Id: Ib967ae70c7d5798a97b865cdda1fda4daed8a99a Reviewed-on: https://go-review.googlesource.com/c/go/+/320252 Trust: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2021-05-02time: make time.Time print a valid Go string with %#vKevin Burke
Previously calling fmt.Sprintf("%#v", t) on a time.Time value would yield a result like: time.Time{wall:0x0, ext:63724924180, loc:(*time.Location)(nil)} which does not compile when embedded in a Go program, and does not tell you what value is represented at a glance. This change adds a GoString method that returns much more legible output: "time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.UTC)" which gives you more information about the time.Time and also can be usefully embedded in a Go program without additional work. Update Quote() to hex escape non-ASCII characters (copying logic from strconv), which makes it safer to embed them in the output of GoString(). Fixes #39034. Change-Id: Ic985bafe4e556f64e82223c643f65143c9a45c3b Reviewed-on: https://go-review.googlesource.com/c/go/+/267017 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
2021-04-06time: properly quote strings containing quotes and backslashesAhmet Aktürk
Fixes #45391 Change-Id: I43ea597f6a9596a621ae7b63eb05440d5b9e2d8f Reviewed-on: https://go-review.googlesource.com/c/go/+/307192 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-03-16time: support "," as separator for fractional secondsEmmanuel T Odeke
Accepts comma "," as a separator for fractional seconds hence we now accept: * 2006-01-02 15:04:05,999999999 -0700 MST * Mon Jan _2 15:04:05,120007 2006 * Mon Jan 2 15:04:05,120007 2006 This change follows the recommendations of ISO 8601 per https://en.wikipedia.org/wiki/ISO_8601#cite_note-26 which states ISO 8601:2004(E), ISO, 2004-12-01, "4.2.2.4 ... the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign." Unfortunately, I couldn't directly access the ISO 8601 document because suddenly it is behind a paywall on the ISO website, charging CHF 158 (USD 179) for 38 pages :-( However, this follows publicly available cited literature, as well as the recommendations from the proposal approval. Fixes #6189 Updates #27746 Updates #26002 Updates #36145 Updates #43813 Fixes #43823 Change-Id: Ibe96064e8ee27c239be78c880fa561a1a41e190c Reviewed-on: https://go-review.googlesource.com/c/go/+/300996 Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-03-15time: add Time.IsDST() to check if its Location is in Daylight Savings TimeJoel Courtney
Fixes #42102 Change-Id: I2cd2fdf67c794c3e99ed1c24786f7f779da73962 GitHub-Last-Rev: bbfa92135734cbd55895012fa492e51686a7b58b GitHub-Pull-Request: golang/go#42103 Reviewed-on: https://go-review.googlesource.com/c/go/+/264077 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Rob Pike <r@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2020-06-02time: note that formats may parse invalid stringsDavid Golden
The existing documentation for time format constants doesn't mention that they may parse technically-invalid strings, such as single-digit hours when a two-digit hour is required by a specification. This commit adds a short warning note to that effect. Fixes #37616 Change-Id: I6e5e12bd42dc368f8ca542b4c0527a2b7d30acaf Reviewed-on: https://go-review.googlesource.com/c/go/+/229460 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-04-14time: quote original value in errors returned by ParseDurationObeyda Djeffal
Quote original values passed as substring of ParseError.Message. Improves the user experience of ParseDuration by making it quote its original argument, for example: _, err := time.ParseDuration("for breakfast") will now produce an error, which when printed out is: time: invalid duration "for breakfast" instead of: time: invalid duration for breakfast Adapt test cases for format.Parse and format.ParseDuration. Fixes #38295 Change-Id: Ife322c8f3c859e1e4e8dd546d4cf0d519b4bfa81 Reviewed-on: https://go-review.googlesource.com/c/go/+/227878 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-23time: don't get confused about day 31 when parsing 002Ian Lance Taylor
The 002 parsing code had a bug that mishandled day 31. Fixes #37387 Change-Id: Ia5a492a4ddd09a4bc232ce9582aead42d5099bdd Reviewed-on: https://go-review.googlesource.com/c/go/+/220637 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2020-01-23time: document how Parse handles two-digit yearsKirill Tatchihin
Fixes #36549 Change-Id: Ia803330fc046d5807bbefd67acb419cb81640a13 GitHub-Last-Rev: bd354319083bf80c250e1915f2be6860d2f7d14b GitHub-Pull-Request: golang/go#36584 Reviewed-on: https://go-review.googlesource.com/c/go/+/214980 Reviewed-by: Rob Pike <r@golang.org>
2019-04-26time: fix misleading error with the leading zero formatLE Manh Cuong
When the leading zero format is used, we currently don't handle the month and year properly. For the month, we were reporting an out of range error when getnum returns zero of its own, as it also returns the month 0. That's confusing, so only check the range when getnum returns a nil error. For the year, we don't restore the value when parsing error occurs. For example, with the incorrect input "111-01", "01" will be used to report an error. So restore the value when an error occurs fix the problem. Fixes #29918 Fixes #29916 Change-Id: I3145f8c46813a0457766b7c302482e6b56f94ed6 Reviewed-on: https://go-review.googlesource.com/c/go/+/160338 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-03-08time: add support for day-of-year in Format and ParseRuss Cox
Day of year is 002 or __2, in contrast to day-in-month 2 or 02 or _2. This means there is no way to print a variable-width day-of-year, but that's probably OK. Fixes #25689. Change-Id: I1425d412cb7d2d360e9b3bf74e89566714e2477a Reviewed-on: https://go-review.googlesource.com/c/go/+/122876 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-08-24time: allow +00 as numeric timezone name and GMT offsetAlberto Donizetti
A timezone with a zero offset from UTC and without a three-letter abbreviation will have a numeric name in timestamps: "+00". There are currently two of them: $ zdump Atlantic/Azores America/Scoresbysund Atlantic/Azores Wed Aug 22 09:01:05 2018 +00 America/Scoresbysund Wed Aug 22 09:01:05 2018 +00 These two timestamp are rejected by Parse, since it doesn't allow for zero offsets: parsing time "Wed Aug 22 09:01:05 2018 +00": extra text: +00 This change modifies Parse to accept a +00 offset in numeric timezone names. As side effect of this change, Parse also now accepts "GMT+00". It was explicitely disallowed (with a unit test ensuring it got rejected), but the restriction seems incorrect. DATE(1), for example, allows it: $ date --debug --date="2009-01-02 03:04:05 GMT+00" date: parsed date part: (Y-M-D) 2009-01-02 date: parsed time part: 03:04:05 date: parsed zone part: UTC+00 date: input timezone: parsed date/time string (+00) date: using specified time as starting value: '03:04:05' date: starting date/time: '(Y-M-D) 2009-01-02 03:04:05 TZ=+00' date: '(Y-M-D) 2009-01-02 03:04:05 TZ=+00' = 1230865445 epoch-seconds date: timezone: system default date: final: 1230865445.000000000 (epoch-seconds) date: final: (Y-M-D) 2009-01-02 03:04:05 (UTC) date: final: (Y-M-D) 2009-01-02 04:04:05 (UTC+01) Fri 2 Jan 04:04:05 CET 2009 This fixes 2 of 17 time.Parse() failures listed in Issue #26032. Updates #26032 Change-Id: I01cd067044371322b7bb1dae452fb3c758ed3cc2 Reviewed-on: https://go-review.googlesource.com/130696 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-08-21time: accept anything between -23 and 23 as offset namezone nameAlberto Donizetti
time.Parse currently rejects numeric timezones names with UTC offsets bigger than +12, but this is incorrect: there's a +13 timezone and a +14 timezone: $ zdump Pacific/Kiritimati Pacific/Kiritimati Mon Jun 25 02:15:03 2018 +14 For convenience, this cl changes the ranges of accepted offsets from -14..+12 to -23..+23 (zero still excluded), i.e. every possible offset that makes sense. We don't validate three-letter abbreviations for the timezones names, so there's no need to be too strict on numeric names. This change also fixes a bug in the parseTimeZone, that is currently unconditionally returning true (i.e. valid timezone), without checking the value returned by parseSignedOffset. This fixes 5 of 17 time.Parse() failures listed in Issue #26032. Updates #26032 Change-Id: I2f08ca9aa41ea4c6149ed35ed2dd8f23eeb42bff Reviewed-on: https://go-review.googlesource.com/120558 Reviewed-by: Rob Pike <r@golang.org>
2018-03-24all: remove some unused return parametersDaniel Martí
As found by unparam. Picked the low-hanging fruit, consisting only of errors that were always nil and results that were never used. Left out those that were useful for consistency with other func signatures. Change-Id: I06b52bbd3541f8a5d66659c909bd93cb3e172018 Reviewed-on: https://go-review.googlesource.com/102418 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-03-08time: add support for parsing timezones denoted by sign and offsetMichael Kasch
IANA Zoneinfo does not provide names for all timezones. Some are denoted by a sign and an offset only. E.g: Europe/Turkey is currently +03 or America/La_Paz which is -04 (https://data.iana.org/time-zones/releases/tzdata2018c.tar.gz) Fixes #24071 Change-Id: I9c230a719945e1263c5b52bab82084d22861be3e Reviewed-on: https://go-review.googlesource.com/98157 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-03time: revert CL 78735 (was: space padding using underscore)Ian Lance Taylor
CL 78735 description: time: add space padding layout strings(using underscore) for not only day but others As mentioned in #22802, only day component of layout string has space padding(represented by one underscore before its placeholder). This commit expands the rule for month, hour, minute and second. Updates #22802 (maybe fixes it) Revert this CL because it breaks currently working formats that happen to use underscores. Fixes #23259 Change-Id: I64acaaca9b5b74785ee0f0be7910574e87daa649 Reviewed-on: https://go-review.googlesource.com/85998 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2017-12-13time: add space padding layout strings(using underscore) for not only day ↵Hanjun Kim
but others As mentioned in #22802, only day component of layout string has space padding(represented by one underscore before its placeholder). This commit expands the rule for month, hour, minute and second. Updates #22802 (maybe fixes it) Change-Id: I886998380489862ab9a324a6774f2e4cf7124122 Reviewed-on: https://go-review.googlesource.com/78735 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2017-11-02time: fix incorrect "zero padding" commentsKenny Grant
The comment on invalid time values in Constants and example refers to _ zero padding when it should refer to space padding. Change-Id: I5784356e389d324703e20eec6203f147db92880f Reviewed-on: https://go-review.googlesource.com/75410 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-11-01time: improve comments about valid layouts being invalid Parse valuesIan Lance Taylor
Updates #9346 Updates #22135 Change-Id: I7039c9f7d49600e877e35b7255c341fea35890e2 Reviewed-on: https://go-review.googlesource.com/74890 Reviewed-by: Rob Pike <r@golang.org>
2017-10-31time: document that valid layouts are not valid Parse valuesKenny Grant
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 <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-09-08time: don't match '---' month in time.ParseCholerae Hu
The existing implementation will panic when month in date string is '---'. Fixed #21113 Change-Id: I8058ae7a4102e882f8b7e9c65d80936b563265e4 Reviewed-on: https://go-review.googlesource.com/51010 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-08-14time: remove unused parameterKevin Burke
lookupName is only called in one location, and one of the return values is unused, so let's remove it. Change-Id: I35e22c7ec611e8eb349deb4f0561e212f7d9de0b Reviewed-on: https://go-review.googlesource.com/55232 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc>