aboutsummaryrefslogtreecommitdiff
path: root/src/time/format.go
AgeCommit message (Collapse)Author
2025-09-19time: improve ParseDuration performance for invalid inputapocelipes
Add "parseDurationError" to reduce memory allocation in the error path of "ParseDuration" and delay the generation of error messages. This improves the performance when dealing with invalid input. The format of the error message remains unchanged. Benchmarks: │ old │ new │ │ sec/op │ sec/op vs base │ ParseDurationError-10 132.10n ± 4% 45.93n ± 2% -65.23% (p=0.000 n=10) │ old │ new │ │ B/op │ B/op vs base │ ParseDurationError-10 192.00 ± 0% 64.00 ± 0% -66.67% (p=0.000 n=10) │ old │ new │ │ allocs/op │ allocs/op vs base │ ParseDurationError-10 6.000 ± 0% 2.000 ± 0% -66.67% (p=0.000 n=10) Fixes #75521 Change-Id: I0dc9f28c9601b6be07b70d0a98613757d76e2c97 GitHub-Last-Rev: 737273936ad0cafd710fa58d99333416e71e657e GitHub-Pull-Request: golang/go#75531 Reviewed-on: https://go-review.googlesource.com/c/go/+/705195 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-03-14time: optimize quote using byte(c) for ASCII1911860538
Since c < runeSelf && c >= ' ' (i.e., 32 <= c < 128), using buf = append(buf, byte(c)) instead of buf = append(buf, string(c)...) is a better choice, as it provides better performance. Change-Id: Ic0ab25c71634a1814267f4d85be2ebd8a3d44676 GitHub-Last-Rev: 5445b547712bbfc77a5c17d76194291c22eb4a05 GitHub-Pull-Request: golang/go#72820 Reviewed-on: https://go-review.googlesource.com/c/go/+/657055 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-11-05time: add examples for Since, Until, Abs and fix some commentscuishuang
Change-Id: I33b61629dfabffa15065a14fccdb418bab11350d Reviewed-on: https://go-review.googlesource.com/c/go/+/623915 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com>
2024-09-10time: don't check non-nil err twice in parseTobias Klauser
Change-Id: I40a1b49035321b05032eacf4525a62b8c562e0d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/612195 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-09-03all: omit unnecessary 0 in slice expressionnlwkobe30
All changes are related to the code, except for the comments in src/regexp/syntax/parse.go and src/slices/slices.go. Change-Id: I73c5d3c54099749b62210aa7f3182c5eb84bb6a6 GitHub-Last-Rev: 794aa9b0539811d00e1cd42be1e8d9fe9afe0281 GitHub-Pull-Request: golang/go#69170 Reviewed-on: https://go-review.googlesource.com/c/go/+/609678 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-07-31time: optimize time <-> date conversionsRuss Cox
Optimize the time -> date and date -> time conversions using the methods outlined in: Cassio Neri and Lorenz Schneider, “Euclidean affine functions and their application to calendar algorithms,” SP&E 2023. https://doi.org/10.1002/spe.3172 I took the opportunity to introduce some types to make the code significantly clearer and optimize a few other parts I noticed along the way. The result is noticeably faster across the board. Probably this doesn't matter much in real programs, but all the other languages are picking this up, and it is less code than what we had before. Proposal #63844 suggested adopting this algorithm and simultaneously restricting the range of valid years supported by the package from its current ±292277022399 (plenty for anyone) to a mere ±32767. This CL does NOT make any such restriction. The range of valid years is almost exactly what it was before. (It is the same size but shifted 10 months earlier, which no one will ever care about.) This CL removes any real need to consider the proposal, since it would be a breaking change for truly insignificant benefit. Thanks to Normandes Junior and Cassio Neri for CL 548155 and for discussion on #63844, which prompted me to write this CL. This CL is all new code and does not include code from CL 548155 except as noted in the isLeap function implementation. For #63844. goos: linux goarch: amd64 pkg: time cpu: AMD Ryzen 9 7950X 16-Core Processor │ timeold.txt │ timenew.txt │ │ sec/op │ sec/op vs base │ Format-32 156.5n ± 1% 148.1n ± 1% -5.37% (n=125) FormatRFC3339-32 118.5n ± 1% 112.1n ± 1% -5.40% (n=125) FormatRFC3339Nano-32 119.2n ± 1% 113.0n ± 1% -5.20% (n=125) FormatNow-32 96.88n ± 2% 97.22n ± 1% ~ (p=0.173 n=125) MarshalJSON-32 79.77n ± 1% 75.82n ± 1% -4.95% (n=125) MarshalText-32 79.25n ± 1% 76.18n ± 1% -3.87% (p=0.000 n=125) Parse-32 79.80n ± 1% 78.28n ± 1% -1.90% (p=0.000 n=125) ParseRFC3339UTC-32 29.10n ± 1% 28.90n ± 0% ~ (p=0.094 n=125) ParseRFC3339UTCBytes-32 30.72n ± 1% 30.88n ± 1% ~ (p=0.894 n=125) ParseRFC3339TZ-32 92.29n ± 0% 90.27n ± 1% -2.19% (p=0.000 n=125) ParseRFC3339TZBytes-32 133.4n ± 1% 132.0n ± 1% ~ (p=0.004 n=125) ParseDuration-32 41.11n ± 3% 44.08n ± 2% ~ (p=0.088 n=125) Hour-32 2.834n ± 0% 2.829n ± 1% ~ (p=0.891 n=125) Second-32 2.811n ± 1% 2.828n ± 1% ~ (p=0.208 n=125) Date-32 9.228n ± 1% 5.788n ± 0% -37.28% (n=125) Year-32 6.404n ± 1% 4.673n ± 1% -27.03% (n=125) YearDay-32 6.399n ± 1% 5.802n ± 0% -9.33% (n=125) Month-32 9.108n ± 1% 4.700n ± 1% -48.40% (n=125) Day-32 9.106n ± 1% 4.686n ± 1% -48.54% (n=125) ISOWeek-32 10.060n ± 0% 7.998n ± 1% -20.50% (n=125) GoString-32 84.59n ± 1% 83.82n ± 1% ~ (p=0.027 n=125) DateFunc-32 6.993n ± 0% 6.144n ± 1% -12.14% (n=125) UnmarshalText-32 94.78n ± 2% 89.49n ± 1% -5.58% (n=125) geomean 29.60n 26.13n -11.70% goos: darwin goarch: arm64 pkg: time cpu: Apple M3 Pro │ timeold-m3.txt │ timenew-m3.txt │ │ sec/op │ sec/op vs base │ Format-12 152.6n ± 0% 147.4n ± 0% -3.41% (n=125) FormatRFC3339-12 101.50n ± 0% 92.02n ± 0% -9.34% (n=125) FormatRFC3339Nano-12 101.30n ± 0% 92.68n ± 0% -8.51% (n=125) FormatNow-12 93.50n ± 0% 94.65n ± 0% +1.23% (p=0.000 n=125) MarshalJSON-12 50.06n ± 0% 48.25n ± 0% -3.62% (n=125) MarshalText-12 49.70n ± 0% 47.51n ± 0% -4.41% (n=125) Parse-12 97.91n ± 0% 95.90n ± 0% -2.05% (n=125) ParseRFC3339UTC-12 36.45n ± 0% 35.78n ± 1% -1.84% (n=125) ParseRFC3339UTCBytes-12 38.11n ± 0% 37.42n ± 0% -1.81% (n=125) ParseRFC3339TZ-12 100.80n ± 1% 97.58n ± 0% -3.19% (n=125) ParseRFC3339TZBytes-12 111.8n ± 1% 107.4n ± 0% -3.94% (n=125) ParseDuration-12 52.70n ± 0% 52.84n ± 0% ~ (p=0.028 n=125) Hour-12 2.657n ± 0% 2.655n ± 0% ~ (p=0.018 n=125) Second-12 2.656n ± 0% 2.654n ± 0% ~ (p=0.084 n=125) Date-12 8.201n ± 0% 5.055n ± 0% -38.36% (n=125) Year-12 5.694n ± 0% 4.086n ± 0% -28.24% (n=125) YearDay-12 5.693n ± 0% 4.828n ± 0% -15.19% (n=125) Month-12 8.206n ± 0% 4.231n ± 0% -48.44% (n=125) Day-12 8.199n ± 0% 4.551n ± 0% -44.49% (n=125) ISOWeek-12 9.032n ± 0% 7.298n ± 0% -19.20% (n=125) GoString-12 62.78n ± 0% 60.61n ± 0% -3.46% (n=125) DateFunc-12 7.318n ± 0% 6.431n ± 0% -12.12% (n=125) UnmarshalText-12 99.66n ± 0% 95.64n ± 0% -4.03% (n=125) Change-Id: I089a072a731914702f8087018d00960e129f86b0 Reviewed-on: https://go-review.googlesource.com/c/go/+/586257 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-07-02time: fix time zone parsing when format includes time zone secondsDarin Krauss
The current implementation fails to parse a time string with a "Z" time zone using a time format that includes time zone seconds. This fix correctly parses the "Z" time zone for any Z-base time format that includes seconds (i.e. "Z070000" or "Z07:00:00"). Fixes #68263 Change-Id: Idf8fa06b5f96383f050c4ffbd2bc5804fd408650 Reviewed-on: https://go-review.googlesource.com/c/go/+/595897 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-06-11time: add to format documentation about dangers of using RFC1123{,Z} for parsingDana Burkart
When using time.RFC1123Z to parse the date header value out of an email, an error is returned for dates that occur in the first 9 days of a month. This is because the format strings for RFC 1123 defined in the time package indicate that the day should be prefixed with a leading 0. Reading the spec, the line that talks about it seems to indicate that days can be either 1 or 2 digits: `date = 1*2DIGIT month 2*4DIGIT` So a date header with a day like `7` with no leading zero should be accepted. Fixes #67887 Change-Id: Ie7ee40d94da2c8c0417957e8b89f9987314949c8 GitHub-Last-Rev: 22a5a52fcb7ee25e5005bb9a014ef8a94d52fb32 GitHub-Pull-Request: golang/go#67888 Reviewed-on: https://go-review.googlesource.com/c/go/+/591335 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-05-23all: document legacy //go:linkname for modules with ≥200 dependentsRuss Cox
Ignored these linknames which have not worked for a while: github.com/xtls/xray-core: context.newCancelCtx removed in CL 463999 (Feb 2023) github.com/u-root/u-root: funcPC removed in CL 513837 (Jul 2023) tinygo.org/x/drivers: net.useNetdev never existed For #67401. Change-Id: I9293f4ef197bb5552b431de8939fa94988a060ce Reviewed-on: https://go-review.googlesource.com/c/go/+/587576 Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-23time: check for time zone offset overflowIan Lance Taylor
Fixes #67470 Change-Id: Idc5997859602ff6155aa9ae875b327fbcb53513d Reviewed-on: https://go-review.googlesource.com/c/go/+/586717 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-05time: use stringslite.Cloneqiulaidongfeng
Change-Id: I82f0e7c0c0c80a3cc0e4a732a59ae1debb37d8d9 GitHub-Last-Rev: c8a081f5b37e26058dd0278464950e81e045ab95 GitHub-Pull-Request: golang/go#67166 Reviewed-on: https://go-review.googlesource.com/c/go/+/583195 Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-04-04time: add available godoc linkcui fliter
Change-Id: Idfe9cf2f2e4750d6673455f98deef2efc2d292a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/539837 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org>
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>