aboutsummaryrefslogtreecommitdiff
path: root/src/time/example_test.go
AgeCommit message (Collapse)Author
2025-07-28all: omit unnecessary type conversionsJes Cok
Found by github.com/mdempsky/unconvert Change-Id: Ib78cceb718146509d96dbb6da87b27dbaeba1306 GitHub-Last-Rev: dedf354811701ce8920c305b6f7aa78914a4171c GitHub-Pull-Request: golang/go#74771 Reviewed-on: https://go-review.googlesource.com/c/go/+/690735 Reviewed-by: Mark Freeman <mark@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2025-04-08time: add examples for AppendBinary and AppendTextcuishuang
Change-Id: I61529b5162f8a77d3bbffcbbac98c834a7626e3a Reviewed-on: https://go-review.googlesource.com/c/go/+/661935 Reviewed-by: Sean Liao <sean@liao.dev> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Carlos Amedee <carlos@golang.org>
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-02-13time: fix typo in ExampleParseDurationRob Pike
A typo without consequences, but confusing nonetheless. The last line prints micro2 and then micro, instead of micro2 twice. One-character fix. Fixes #65666 Change-Id: I61d636382a2223d53eac58d6ddbcc7c15b4efc85 Reviewed-on: https://go-review.googlesource.com/c/go/+/563275 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
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>
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>
2021-11-05time: add some examplesjiahua wang
Change-Id: I2668cdea64f75bee87d424730d404834d69362a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/357270 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Mui <cherryyz@google.com>
2020-05-26runtime, time: gofmtTobias Klauser
Change-Id: Ib36a5f239db5af497aae122eba049c15d0d4c4a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/235139 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-22time: simplify Duration.String exampleRuss Cox
The existing example is needlessly complex. You have to know that t.Sub returns a Duration and also have to mentally subtract the two times to understand what duration should be printed. Rewrite to focus on just the Duration.String operation. Change-Id: I00765b6019c07a6ff03022625b556c2b9ba87c09 Reviewed-on: https://go-review.googlesource.com/c/go/+/234893 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@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-03-02time: use values larger than 24 for day for time.Format examplesJean de Klerk
Currently, the time.Format docs use 7 Mar 2015 as the day/month/year. In numeric form, that is either 7/3/2015 or 3/7/2015 depending on which part of the world you're from. This is extremely confusing. In fact, the reference time being defined in a very US-centric way is quite confusing for the rest of the world, too [1]. We can't change that, but we can make the time.Format docs more comprehendable to the rest of the world without sacrificing by simply choosing a day that is not ambiguous (a value greater than 24 for day). This CL does makes the necessary change. Note: this CL moves some of the padding examples into their own example, since those examples do need a <10 day to demonstrate padding. 1: Additional context: a very old golang-nuts thread in which Rob expresses some regret about the format being the USA standard, rather than the alternative: https://groups.google.com/forum/m/#!msg/golang-nuts/0nQbfyNzk9E/LWbMgpRQNOgJ. Change-Id: If0a07c5e0dab86f8420cbf59543405eb857aa7f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/221612 Run-TryBot: Jean de Klerk <deklerk@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2020-02-25time: add basic YYYY/MM/DD example to time docsJean de Klerk
This is a _very_ common question [1]. Let's just make an example for it. 1: https://www.google.com/search?q=golang+yyyy-mm-dd&oq=golang+yyyy-mm-dd&aqs=chrome..69i57j0l4j69i64l3.6015j0j7&sourceid=chrome&ie=UTF-8 Change-Id: I32ae689b91018d326f31a2442a1beaf68dddf13c Reviewed-on: https://go-review.googlesource.com/c/go/+/220595 Run-TryBot: Jean de Klerk <deklerk@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-10time: change variable name to nextAgniva De Sarker
The variable now implies that the next tick always returns the current time which is not always the case. Change it to next to clarify that it returns the time of the next tick which is more appropriate. Fixes #30271 Change-Id: Ie7719cb8c7180bc6345b436f9b3e950ee349d6e4 Reviewed-on: https://go-review.googlesource.com/c/go/+/206123 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-10-15time: avoid or clarify CESTIan Lance Taylor
In the tzdata database CEST is not recognized as a timezone name. It is used as the abbreviated name for daylight saving time in Central Europe. Avoid using CEST in documentation as it suggests that programs can parse dates that use CEST, which will typically fail on Unix systems. Updates #34913 Change-Id: I4b22f7d06607eb5b066812a48af58edd95498286 Reviewed-on: https://go-review.googlesource.com/c/go/+/201197 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-09-17time: add examples for microseconds and milliseconds methodsPantelis Sampaziotis
This change adds testable examples for the new Microseconds and Milliseconds methods that were introduced in Go 1.13. Fixes #34354 Change-Id: Ibdbfd770ca2192f9086f756918325f7327ce0482 GitHub-Last-Rev: 4575f48f5feb8e49742304d17776e28302647931 GitHub-Pull-Request: golang/go#34355 Reviewed-on: https://go-review.googlesource.com/c/go/+/195979 Reviewed-by: Alexander Rakoczy <alex@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Alexander Rakoczy <alex@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-02-28time: move the explanation of u/micro to the ParseDuration exampleRob Pike
Fix a few missing capitalizations in drive-by. Change-Id: I7353c12f3ccddefc0f26a98590caf9e446129558 Reviewed-on: https://go-review.googlesource.com/c/163918 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-02-27time: rewrite ExampleDuration_Nanoseconds to be more idiomatic.Rob Pike
Fix the punctuation and use the proper units for microseconds, while explaining the incorrect but common variant 'us'. Change-Id: I9e96694ef27ab4761efccd8616ac7b6700f60d39 Reviewed-on: https://go-review.googlesource.com/c/163917 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-02-26time: parse 1us in Nanoseconds exampleAlberto Donizetti
The example for Nanoseconds() currently reads: ns, _ := time.ParseDuration("1000ns") fmt.Printf("one microsecond has %d nanoseconds.", ns.Nanoseconds()) which is not terribly interesting: it seems obvious that parsing "1000ns" and then calling Nanoseconds() will print 1000. The mention of microseconds in the text suggests that the author's intention was, instead, to write something like this: u, _ := time.ParseDuration("1us") i.e. build a time value by parsing 1 microsecond, and then print the value in nanoseconds. Change the example to do this. Change-Id: I4ddb123f0935a12cda3b5d6f1ca919bfcd6383d6 Reviewed-on: https://go-review.googlesource.com/c/163622 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-31time: display results in examples rather soonYury Smolsky
We have fixed the playground to display results of the program when it was timed out. This CL fixes how soon results will be displayed to the user. Change-Id: Ifb75828e0de12c726c8ca6e2d04947e01913dc73 Reviewed-on: https://go-review.googlesource.com/c/146237 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-30time: add example for LoadLocationVenil Noronha
Change-Id: I8e55e9397eb6844b5856f8bde9c26185c446a80e Reviewed-on: https://go-review.googlesource.com/132238 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-07-12time: fix typo in Truncate exampleKeith Rarick
The existing example code uses variable name d, but prints t in its output. It's needlessly confusing. Change-Id: I67bef3c732e84d2d89819f96b4b62663630fd69e Reviewed-on: https://go-review.googlesource.com/123516 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-02-13time: add example for FixedZoneKevin Burke
Change-Id: I8a6c3e225038cbeb315433fabf8835f582836d3e Reviewed-on: https://go-review.googlesource.com/93657 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-12-06time: condense, expand Time.Unix exampleRuss Cox
The new example is shorter but illustrates the interesting parts of the Unix function and methods. Change-Id: Ief8ec38909d4ed7829e8d3da58e7b7f712537f99 Reviewed-on: https://go-review.googlesource.com/82079 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-11-13time: add example for Time.UnixAdrian Hesketh
Change-Id: Ie64eba5b57b609a343ddb381fe83c01f172c0bf4 Reviewed-on: https://go-review.googlesource.com/60890 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2017-11-06time: example in doc for time.AppendFormat funcRadek Sohlich
The simple example would contribute to better understanding what function does. Change-Id: I36a2952df8b0e1762ec0cd908a867c457f39366e Reviewed-on: https://go-review.googlesource.com/75970 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@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-02time: fix incorrect "zero pad" comment in exampleBlixt
The comment currently implies that a zero will be added, but the underscore is used to add a space for single-digit dates. Change-Id: Ib3bac8a16bc2d1fcb26ab3bb7ad172b89e1a4a24 Reviewed-on: https://go-review.googlesource.com/75230 Reviewed-by: Ian Lance Taylor <iant@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-09time: add a number of new examplesAdrian Hesketh
Change-Id: I14d19a3951fcae24e2c2ce2eb76312851e050fdd Reviewed-on: https://go-review.googlesource.com/61033 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-09-09time: change wording in duration hours exampleJuan Carlos
Change-Id: I86728a1c6c20471beaa3546ca7a43a8edeb9f0b7 Reviewed-on: https://go-review.googlesource.com/50691 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Adrian Hesketh <adrianhesketh@hushmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-09-09time: add an example to the NewTicker functionElbert Fliek
Change-Id: Idad9cdee36679373ee223ff2bd4c021ea0afcce1 Reviewed-on: https://go-review.googlesource.com/61710 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-08-11time: add examples for Duration functionsmolivier
Change-Id: I78f4ec32c6445015ce626a552edcba561eb650fa Reviewed-on: https://go-review.googlesource.com/54710 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
2017-06-28time: show how to get midnight on the current dayKevin Burke
A common task is trying to get today's date in the local time zone with zero values for the hour, minute, second, and nanosecond fields. I tried this recently and incorrectly used Truncate(24*time.Hour), which truncates based on a UTC clock, and gave me 5pm Pacific time instead of midnight Pacific. I thought it would be helpful to show a "correct" way to do this. Change-Id: I479e6b0cc56367068530981ca69882b34febf945 Reviewed-on: https://go-review.googlesource.com/46833 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-09-12time: improve Truncate and Round documentationQuentin Smith
Truncate and Round operate on absolute time, which means that Truncate(Hour) may return a time with non-zero Minute(). Document that more clearly, and remove the misleading example which suggests it is safe. Updates #16647 Change-Id: I930584ca030dd12849195d45e49ed2fb74e0c9ac Reviewed-on: https://go-review.googlesource.com/28730 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-01all: make copyright headers consistent with one space after periodBrad Fitzpatrick
This is a subset of https://golang.org/cl/20022 with only the copyright header lines, so the next CL will be smaller and more reviewable. Go policy has been single space after periods in comments for some time. The copyright header template at: https://golang.org/doc/contribute.html#copyright also uses a single space. Make them all consistent. Change-Id: Icc26c6b8495c3820da6b171ca96a74701b4a01b0 Reviewed-on: https://go-review.googlesource.com/20111 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-03-26time: add a thorough example for time.FormatRob Pike
People will still not look at it, but at least we will have a stronger defense. Change-Id: Ieea6a3d42d06e1067e424e35b87dbcb01c9523cb Reviewed-on: https://go-review.googlesource.com/7859 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2014-09-08build: move package sources from src/pkg to srcRuss Cox
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.