aboutsummaryrefslogtreecommitdiff
path: root/src/time
AgeCommit message (Collapse)Author
2024-01-25all: prealloc slice with possible minimum capabilitiesShulhan
2024-01-22time: add a regression test for time.AfterFunc goroutine starvationBryan C. Mills
The test is skipped on wasm platforms for now, because it successfully detects a starvation bug on those platforms. For #65178. Change-Id: I05d28f1c7be99fcab67ec4dfaa38f412e11fd3cb Reviewed-on: https://go-review.googlesource.com/c/go/+/557038 Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
2023-10-14all: fix function namescui fliter
Change-Id: Iba8878420c59d705066d1d9955e91a5c2eb4faf5 Reviewed-on: https://go-review.googlesource.com/c/go/+/507615 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2023-10-03time: fix Time godocTobias Klauser
The additional empty line was inadvertently introduced by CL 526676, causing only part of the Time godoc to be rendered on pkg.go.dev. Change-Id: I868315752eb160ebaab227c8b5369054c557cb7e Reviewed-on: https://go-review.googlesource.com/c/go/+/531877 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
2023-10-03time: clarify docs to avoid date calculation pitfallsBrendan Jackman
I recently reviewed some code that did time calculations using `time.UnixMicro(0).UTC()`. I commented that because time calculations are independent of the location, they should drop the `.UTC()`, and they replied that it made their tests fail. I looked into it and eventually discovered it was because they were using AddDate. Dramatically simplified, their code did something like:     orig := time.Date(2013, time.March, 23, 12, 00, 0, 0, time.UTC)     want := time.Date(2013, time.March, 23, 0, 0, 0, 0, time.UTC)     epoch := time.UnixMicro(0)     days := int(orig.Sub(epoch).Hours() / 24)     got := epoch.AddDate(0, 0, days)     if !got.Equal(want) {         t.Errorf("ay caramba: %v vs %v", got.UTC(), want.UTC())     } The issue is that their tests run in Pacific time, which is currently PST (UTC-8) but was PDT (UTC-7) in January 1970. It turns out they were implementing some business policy that really cares abut calendar days so AddDate is correct, but it's certainly a bit confusing! The idea with this change is to remove the risk that readers make a false shortcut in their mind: "Locations do not affect time calculations". To do this we remove some text from the core time.Time doc and shift it to the areas of the library that deal with these intrinsically confusing operations. Change-Id: I8200e9edef7d1cdd8516719e34814eb4b78d30a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/526676 Reviewed-by: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-09-22all: clean unnecessary casts for test filesJes Cok
This is a follow up of CL 528696. Change-Id: I5b71eabedb12567c4b1b36f7182a3d2b0ed662a5 GitHub-Last-Rev: acaf3ac11c38042ad27b99e1c70a3c9f1a554a15 GitHub-Pull-Request: golang/go#62713 Reviewed-on: https://go-review.googlesource.com/c/go/+/529197 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-09-14time: unconditionally use RegLoadMUIString on Windowsqmuntal
RegLoadMUIString is supported on Windows Vista and later, so this should be safe to do unconditionally. While here, also unconditionally use GetDynamicTimeZoneInformation in syscall/windows/registry tests, since it's also supported on Windows Vista and later. Change-Id: Ifa32a81f4727d4810f2b9ccc814aa77a43513e59 Reviewed-on: https://go-review.googlesource.com/c/go/+/527595 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
2023-09-09all: calculate the median uniformlyJes Cok
Like sort.Search, use "h := int(uint(i+j) >> 1)" style code to calculate the median. Change-Id: Ifb1a19dde1c6ed6c1654bc642fc9565a8b6c5fc4 GitHub-Last-Rev: e2213b738832f1674948d6507f40e2c0b98cb972 GitHub-Pull-Request: golang/go#62503 Reviewed-on: https://go-review.googlesource.com/c/go/+/526496 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-08-25time: make time.Until a few nanoseconds fasterkorzhao
This is similar to CL 518336. For #61765. Change-Id: I7c1d92a3b3e2b6c1c0058a2094997d93082ad139 Reviewed-on: https://go-review.googlesource.com/c/go/+/521237 Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-08-18time: make Duration.String inlineableJoe Tsai
Perform the [32]byte to string conversion in an inlinable method. Thus, if the result does not escape in the context of the caller, we can entirely avoid a call to runtime.slicebytetostring. Change-Id: Iae8ec2a532776ed6cf99597f19e3f7f21c694c3a Reviewed-on: https://go-review.googlesource.com/c/go/+/520602 Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-08-11time: make time.Since a few nanoseconds fasterRuss Cox
time.Since(base) is an idiom that can be used to read the system monotonic time as efficiently as possible, when that matters. The current code structure adds a few nanoseconds on top of the 15-20ns the time read already takes. Remove those few. After this CL, there is no reason at all for anyone to //go:linkname runtime.nanotime1 instead. Came up while investigating #61765. Change-Id: Ic9e688af039babfc2a5a8e67dcbb02847a5eb686 Reviewed-on: https://go-review.googlesource.com/c/go/+/518336 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
2023-08-07time: amend time resolution docsqmuntal
Updates #44343 Change-Id: Id1497f0236fe7b4937e64877fa943329e280d192 Reviewed-on: https://go-review.googlesource.com/c/go/+/515495 Reviewed-by: Michael Knyszek <mknyszek@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-08-01time: mention time resolution in docsqmuntal
Updates #44343 Updates #53824 Change-Id: Ia7234fac4b1b88b3c331328aaa98dc85205e09ba Reviewed-on: https://go-review.googlesource.com/c/go/+/514275 Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
2023-07-20time: document that AfterFunc Timer's C field is nilIan Lance Taylor
Fixes #60811 Change-Id: Ica947a4789e71826284f9f6e41c298baa3d033e4 Reviewed-on: https://go-review.googlesource.com/c/go/+/503922 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2023-07-11time: increase arbitrary upper bound in TestReset to 10sBryan C. Mills
The previous upper bound was around 0.375 s, which is empirically too short on a slow, heavily-loaded builder. Since the test doesn't seem to depend on the actual duration in any meaningful way, let's make it several orders of magnitude larger. Fixes #61266. Change-Id: I6dde5e174966ee385db67e3cb87334f463c7e597 Reviewed-on: https://go-review.googlesource.com/c/go/+/508695 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-05-12time: update windows zoneinfo_abbrsCarlos Amedee
The primary branch of the github.com/unicode-org/cldr repository has changed to main instead of master. This changes the branch used to download the Windows zone file. Ran: go generate time For #58113 Change-Id: Idb3dcaf44fc52d4b6abfed5a3ca6cd6f745dc3f8 Reviewed-on: https://go-review.googlesource.com/c/go/+/493477 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> Auto-Submit: Carlos Amedee <carlos@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Carlos Amedee <carlos@golang.org>
2023-04-11time: add wasip1 supportJohan Brandhorst-Satzkorn
Fixes #58141 Co-authored-by: Richard Musiol <neelance@gmail.com> Co-authored-by: Achille Roussel <achille.roussel@gmail.com> Co-authored-by: Julien Fabre <ju.pryz@gmail.com> Co-authored-by: Evan Phoenix <evan@phx.io> Change-Id: I51dabbf17b8f5540c964f90223d18457f6f9fbef Reviewed-on: https://go-review.googlesource.com/c/go/+/483236 Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-04-11time: update windows zoneinfo_abbrsIan Lance Taylor
While running "go generate" I noticed that there has been a change. For #58113 Change-Id: Icca349d0a100dd4b11d34e4bbcfd1aef57346db7 Reviewed-on: https://go-review.googlesource.com/c/go/+/483716 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
2023-03-13time: fix timezone lookup logic for non-DST zonesGeon Kim
This change fixes time.LoadLocationFromTZData and time.Location.lookup logic if the given time is after the last transition and the extend string doesn't have the DST rule. Fixes #58682 Change-Id: Ie34a6d658d14c2b33098b29ab83c041ef0d34266 GitHub-Last-Rev: f6681eb44c0ea0772004e56eb68fcbd9023d971e GitHub-Pull-Request: golang/go#58684 Reviewed-on: https://go-review.googlesource.com/c/go/+/471020 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
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>
2023-02-04time: add /etc/zoneinfo as valid path for tzdata for NixOSDavid Anderson
NixOS has no /usr/share, but does have tzdata at /etc/zoneinfo. Change-Id: Ic7d7f42a215e06c2e4f5c54ee11db82240f27167 GitHub-Last-Rev: 9969dd3e2cc49b30c3cd6b0858cb60b00bcbdf60 GitHub-Pull-Request: golang/go#58267 Reviewed-on: https://go-review.googlesource.com/c/go/+/464995 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2023-01-29time: update windows zoneinfo_abbrsqmuntal
zoneinfo_abbrs hasn't been updated since go 1.14, it's time to regenerate it. Updates #58113 Change-Id: Ic156ae607c46f1f5a9408b1fc0b56de6c14a4ed4 Reviewed-on: https://go-review.googlesource.com/c/go/+/463838 Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-01-20time: no need to wrap error with errors.Newfangguizhen
Change-Id: Ibd519ed6419f8f21c89a111a0326d0788aca9d19 GitHub-Last-Rev: 45e3224f9afef57f49eebf3cbdf6b1d01cfd7346 GitHub-Pull-Request: golang/go#57819 Reviewed-on: https://go-review.googlesource.com/c/go/+/462046 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-01-18time: revert strict parsing of RFC 3339Joe Tsai
CL 444277 fixed Time.UnmarshalText and Time.UnmarshalJSON to properly unmarshal timestamps according to RFC 3339 instead of according to Go's bespoke time syntax that is a superset of RFC 3339. However, this change seems to have broken an AWS S3 unit test that relies on parsing timestamps with single digit hours. It is unclear whether S3 emits these timestamps in production or whether this is simply a testing artifact that has been cargo culted across many code bases. Either way, disable strict parsing for now and re-enable later with better GODEBUG support. Updates #54580 Change-Id: Icced2c7f9a6b2fc06bbd9c7e90f90edce24c2306 Reviewed-on: https://go-review.googlesource.com/c/go/+/462286 Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2023-01-17time/tzdata: generate zip constant during cmd/distRuss Cox
We have a make.bash-time generation capability, so use it to generate the embedded zip file for time/tzdata. This is one less file to try to review in CLs like CL 455356. For #22487. Fixes #43350. Change-Id: I2fcd0665fa0b1c830baec5fb4cd714483fea25a4 Reviewed-on: https://go-review.googlesource.com/c/go/+/455357 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-12-07lib/time: update to 2022g/2022gRuss Cox
Commit generated by update.bash. For #22487. Change-Id: I6a995a3baea7c511b9bd5155f81d8b8e2cdff09d Reviewed-on: https://go-review.googlesource.com/c/go/+/455356 Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Russ Cox <rsc@golang.org> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-24lib/time, time/tzdata: update to 2022fTobias Klauser
Version 2022f was released on 2022-10-29, see https://mm.icann.org/pipermail/tz-announce/2022-October/000075.html for the release announcement. For #22487 Change-Id: I6130def7fcd389b30ffed0eb9003d0fcf5eabb82 Reviewed-on: https://go-review.googlesource.com/c/go/+/453055 Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
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-28time: add comment explaining >>33 in NowIan Lance Taylor
Change-Id: I022b617cd345b412bee0b50a862676a1dca94e01 Reviewed-on: https://go-review.googlesource.com/c/go/+/418376 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Benny Siegert <bsiegert@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
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-19time: implement Compare methodRoger Peppe
Fixes #50770. Change-Id: If0104883bb409ec85827fa5b570f68099ad4fd1d Reviewed-on: https://go-review.googlesource.com/c/go/+/382734 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: hopehook <hopehook@golangcn.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
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-09-08time: optimize FixedZone by caching unnamed zones by the hourJoe Tsai
FixedZone is transitively called by Time.UnmarshalJSON or Time.UnmarshalText for any RFC 3339 timestamp that is not in UTC. This function is relatively slow since it allocates 3 times. Given that RFC 3339 never has a zone name and most offsets are by the hour, we can cache unnamed zones on hour offsets. Caching a Location should be safe since it has no exported fields or methods that can mutate the Location. It is functionally immutable. The only way to observe that the Location was cached is either by pointer comparison or by shallow copying the struct value. Neither operation seems sensible to do with a *time.Location. Performance: name old time/op new time/op delta UnmarshalText 268ns ± 2% 182ns ± 1% -32.01% (p=0.000 n=10+10) Change-Id: Iab5432f34bdbb485512bb8b5464e076c03fd106f Reviewed-on: https://go-review.googlesource.com/c/go/+/425116 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-05runtime: convert local var stop at TestAfterStress to atomic typecuiweixie
For #53821 Change-Id: I7e86dac34691f7752f68879ff379061f3435cd45 Reviewed-on: https://go-review.googlesource.com/c/go/+/427139 Reviewed-by: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: xie cui <523516579@qq.com> Reviewed-by: Heschi Kreinick <heschi@google.com>
2022-08-29time: use internal/itoaTobias Klauser
In initLocal for GOOS=js, use internal/itoa introduced in CL 301549 instead of a local implementation. Change-Id: If107d5cf0ce56f4d926507db2cbd6da422c6d15a Reviewed-on: https://go-review.googlesource.com/c/go/+/425302 Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gopher Robot <gobot@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-18time: fix LoadLocation documentation formattingAbhinav Gupta
The documentation for LoadLocation contains an enumerated list, but does not render as such because it's missing leading spaces. Output verified with the go doc command and godoc server. Change-Id: I88b61d34048b7d01ee5cd77c32849af266e2f4c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/423297 Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
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-11lib/time, time/tzdata: update to 2022bDmitri Shuralyov
Version 2022b was released on 2022-08-10 and we can start using it. Its release announcement was https://mm.icann.org/pipermail/tz-announce/2022-August/000071.html. For #22487. Change-Id: I0ec68968d82561da566f945608bfacb3c8b0e958 Reviewed-on: https://go-review.googlesource.com/c/go/+/422875 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
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>