example_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package time_test
  5. import (
  6. "fmt"
  7. "time"
  8. )
  9. func expensiveCall() {}
  10. func ExampleDuration() {
  11. t0 := time.Now()
  12. expensiveCall()
  13. t1 := time.Now()
  14. fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
  15. }
  16. func ExampleDuration_Round() {
  17. d, err := time.ParseDuration("1h15m30.918273645s")
  18. if err != nil {
  19. panic(err)
  20. }
  21. round := []time.Duration{
  22. time.Nanosecond,
  23. time.Microsecond,
  24. time.Millisecond,
  25. time.Second,
  26. 2 * time.Second,
  27. time.Minute,
  28. 10 * time.Minute,
  29. time.Hour,
  30. }
  31. for _, r := range round {
  32. fmt.Printf("d.Round(%6s) = %s\n", r, d.Round(r).String())
  33. }
  34. // Output:
  35. // d.Round( 1ns) = 1h15m30.918273645s
  36. // d.Round( 1µs) = 1h15m30.918274s
  37. // d.Round( 1ms) = 1h15m30.918s
  38. // d.Round( 1s) = 1h15m31s
  39. // d.Round( 2s) = 1h15m30s
  40. // d.Round( 1m0s) = 1h16m0s
  41. // d.Round( 10m0s) = 1h20m0s
  42. // d.Round(1h0m0s) = 1h0m0s
  43. }
  44. func ExampleDuration_String() {
  45. fmt.Println(1*time.Hour + 2*time.Minute + 300*time.Millisecond)
  46. fmt.Println(300 * time.Millisecond)
  47. // Output:
  48. // 1h2m0.3s
  49. // 300ms
  50. }
  51. func ExampleDuration_Truncate() {
  52. d, err := time.ParseDuration("1h15m30.918273645s")
  53. if err != nil {
  54. panic(err)
  55. }
  56. trunc := []time.Duration{
  57. time.Nanosecond,
  58. time.Microsecond,
  59. time.Millisecond,
  60. time.Second,
  61. 2 * time.Second,
  62. time.Minute,
  63. 10 * time.Minute,
  64. time.Hour,
  65. }
  66. for _, t := range trunc {
  67. fmt.Printf("d.Truncate(%6s) = %s\n", t, d.Truncate(t).String())
  68. }
  69. // Output:
  70. // d.Truncate( 1ns) = 1h15m30.918273645s
  71. // d.Truncate( 1µs) = 1h15m30.918273s
  72. // d.Truncate( 1ms) = 1h15m30.918s
  73. // d.Truncate( 1s) = 1h15m30s
  74. // d.Truncate( 2s) = 1h15m30s
  75. // d.Truncate( 1m0s) = 1h15m0s
  76. // d.Truncate( 10m0s) = 1h10m0s
  77. // d.Truncate(1h0m0s) = 1h0m0s
  78. }
  79. func ExampleParseDuration() {
  80. hours, _ := time.ParseDuration("10h")
  81. complex, _ := time.ParseDuration("1h10m10s")
  82. micro, _ := time.ParseDuration("1µs")
  83. // The package also accepts the incorrect but common prefix u for micro.
  84. micro2, _ := time.ParseDuration("1us")
  85. fmt.Println(hours)
  86. fmt.Println(complex)
  87. fmt.Printf("There are %.0f seconds in %v.\n", complex.Seconds(), complex)
  88. fmt.Printf("There are %d nanoseconds in %v.\n", micro.Nanoseconds(), micro)
  89. fmt.Printf("There are %6.2e seconds in %v.\n", micro2.Seconds(), micro)
  90. // Output:
  91. // 10h0m0s
  92. // 1h10m10s
  93. // There are 4210 seconds in 1h10m10s.
  94. // There are 1000 nanoseconds in 1µs.
  95. // There are 1.00e-06 seconds in 1µs.
  96. }
  97. func ExampleDuration_Hours() {
  98. h, _ := time.ParseDuration("4h30m")
  99. fmt.Printf("I've got %.1f hours of work left.", h.Hours())
  100. // Output: I've got 4.5 hours of work left.
  101. }
  102. func ExampleDuration_Microseconds() {
  103. u, _ := time.ParseDuration("1s")
  104. fmt.Printf("One second is %d microseconds.\n", u.Microseconds())
  105. // Output:
  106. // One second is 1000000 microseconds.
  107. }
  108. func ExampleDuration_Milliseconds() {
  109. u, _ := time.ParseDuration("1s")
  110. fmt.Printf("One second is %d milliseconds.\n", u.Milliseconds())
  111. // Output:
  112. // One second is 1000 milliseconds.
  113. }
  114. func ExampleDuration_Minutes() {
  115. m, _ := time.ParseDuration("1h30m")
  116. fmt.Printf("The movie is %.0f minutes long.", m.Minutes())
  117. // Output: The movie is 90 minutes long.
  118. }
  119. func ExampleDuration_Nanoseconds() {
  120. u, _ := time.ParseDuration("1µs")
  121. fmt.Printf("One microsecond is %d nanoseconds.\n", u.Nanoseconds())
  122. // Output:
  123. // One microsecond is 1000 nanoseconds.
  124. }
  125. func ExampleDuration_Seconds() {
  126. m, _ := time.ParseDuration("1m30s")
  127. fmt.Printf("Take off in t-%.0f seconds.", m.Seconds())
  128. // Output: Take off in t-90 seconds.
  129. }
  130. var c chan int
  131. func handle(int) {}
  132. func ExampleAfter() {
  133. select {
  134. case m := <-c:
  135. handle(m)
  136. case <-time.After(10 * time.Second):
  137. fmt.Println("timed out")
  138. }
  139. }
  140. func ExampleSleep() {
  141. time.Sleep(100 * time.Millisecond)
  142. }
  143. func statusUpdate() string { return "" }
  144. func ExampleTick() {
  145. c := time.Tick(5 * time.Second)
  146. for next := range c {
  147. fmt.Printf("%v %s\n", next, statusUpdate())
  148. }
  149. }
  150. func ExampleMonth() {
  151. _, month, day := time.Now().Date()
  152. if month == time.November && day == 10 {
  153. fmt.Println("Happy Go day!")
  154. }
  155. }
  156. func ExampleDate() {
  157. t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  158. fmt.Printf("Go launched at %s\n", t.Local())
  159. // Output: Go launched at 2009-11-10 15:00:00 -0800 PST
  160. }
  161. func ExampleNewTicker() {
  162. ticker := time.NewTicker(time.Second)
  163. defer ticker.Stop()
  164. done := make(chan bool)
  165. go func() {
  166. time.Sleep(10 * time.Second)
  167. done <- true
  168. }()
  169. for {
  170. select {
  171. case <-done:
  172. fmt.Println("Done!")
  173. return
  174. case t := <-ticker.C:
  175. fmt.Println("Current time: ", t)
  176. }
  177. }
  178. }
  179. func ExampleTime_Format() {
  180. // Parse a time value from a string in the standard Unix format.
  181. t, err := time.Parse(time.UnixDate, "Wed Feb 25 11:06:39 PST 2015")
  182. if err != nil { // Always check errors even if they should not happen.
  183. panic(err)
  184. }
  185. // time.Time's Stringer method is useful without any format.
  186. fmt.Println("default format:", t)
  187. // Predefined constants in the package implement common layouts.
  188. fmt.Println("Unix format:", t.Format(time.UnixDate))
  189. // The time zone attached to the time value affects its output.
  190. fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate))
  191. // The rest of this function demonstrates the properties of the
  192. // layout string used in the format.
  193. // The layout string used by the Parse function and Format method
  194. // shows by example how the reference time should be represented.
  195. // We stress that one must show how the reference time is formatted,
  196. // not a time of the user's choosing. Thus each layout string is a
  197. // representation of the time stamp,
  198. // Jan 2 15:04:05 2006 MST
  199. // An easy way to remember this value is that it holds, when presented
  200. // in this order, the values (lined up with the elements above):
  201. // 1 2 3 4 5 6 -7
  202. // There are some wrinkles illustrated below.
  203. // Most uses of Format and Parse use constant layout strings such as
  204. // the ones defined in this package, but the interface is flexible,
  205. // as these examples show.
  206. // Define a helper function to make the examples' output look nice.
  207. do := func(name, layout, want string) {
  208. got := t.Format(layout)
  209. if want != got {
  210. fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want)
  211. return
  212. }
  213. fmt.Printf("%-16s %q gives %q\n", name, layout, got)
  214. }
  215. // Print a header in our output.
  216. fmt.Printf("\nFormats:\n\n")
  217. // Simple starter examples.
  218. do("Basic full date", "Mon Jan 2 15:04:05 MST 2006", "Wed Feb 25 11:06:39 PST 2015")
  219. do("Basic short date", "2006/01/02", "2015/02/25")
  220. // The hour of the reference time is 15, or 3PM. The layout can express
  221. // it either way, and since our value is the morning we should see it as
  222. // an AM time. We show both in one format string. Lower case too.
  223. do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h")
  224. // When parsing, if the seconds value is followed by a decimal point
  225. // and some digits, that is taken as a fraction of a second even if
  226. // the layout string does not represent the fractional second.
  227. // Here we add a fractional second to our time value used above.
  228. t, err = time.Parse(time.UnixDate, "Wed Feb 25 11:06:39.1234 PST 2015")
  229. if err != nil {
  230. panic(err)
  231. }
  232. // It does not appear in the output if the layout string does not contain
  233. // a representation of the fractional second.
  234. do("No fraction", time.UnixDate, "Wed Feb 25 11:06:39 PST 2015")
  235. // Fractional seconds can be printed by adding a run of 0s or 9s after
  236. // a decimal point in the seconds value in the layout string.
  237. // If the layout digits are 0s, the fractional second is of the specified
  238. // width. Note that the output has a trailing zero.
  239. do("0s for fraction", "15:04:05.00000", "11:06:39.12340")
  240. // If the fraction in the layout is 9s, trailing zeros are dropped.
  241. do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")
  242. // Output:
  243. // default format: 2015-02-25 11:06:39 -0800 PST
  244. // Unix format: Wed Feb 25 11:06:39 PST 2015
  245. // Same, in UTC: Wed Feb 25 19:06:39 UTC 2015
  246. //
  247. // Formats:
  248. //
  249. // Basic full date "Mon Jan 2 15:04:05 MST 2006" gives "Wed Feb 25 11:06:39 PST 2015"
  250. // Basic short date "2006/01/02" gives "2015/02/25"
  251. // AM/PM "3PM==3pm==15h" gives "11AM==11am==11h"
  252. // No fraction "Mon Jan _2 15:04:05 MST 2006" gives "Wed Feb 25 11:06:39 PST 2015"
  253. // 0s for fraction "15:04:05.00000" gives "11:06:39.12340"
  254. // 9s for fraction "15:04:05.99999999" gives "11:06:39.1234"
  255. }
  256. func ExampleTime_Format_pad() {
  257. // Parse a time value from a string in the standard Unix format.
  258. t, err := time.Parse(time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
  259. if err != nil { // Always check errors even if they should not happen.
  260. panic(err)
  261. }
  262. // Define a helper function to make the examples' output look nice.
  263. do := func(name, layout, want string) {
  264. got := t.Format(layout)
  265. if want != got {
  266. fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want)
  267. return
  268. }
  269. fmt.Printf("%-16s %q gives %q\n", name, layout, got)
  270. }
  271. // The predefined constant Unix uses an underscore to pad the day.
  272. do("Unix", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
  273. // For fixed-width printing of values, such as the date, that may be one or
  274. // two characters (7 vs. 07), use an _ instead of a space in the layout string.
  275. // Here we print just the day, which is 2 in our layout string and 7 in our
  276. // value.
  277. do("No pad", "<2>", "<7>")
  278. // An underscore represents a space pad, if the date only has one digit.
  279. do("Spaces", "<_2>", "< 7>")
  280. // A "0" indicates zero padding for single-digit values.
  281. do("Zeros", "<02>", "<07>")
  282. // If the value is already the right width, padding is not used.
  283. // For instance, the second (05 in the reference time) in our value is 39,
  284. // so it doesn't need padding, but the minutes (04, 06) does.
  285. do("Suppressed pad", "04:05", "06:39")
  286. // Output:
  287. // Unix "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015"
  288. // No pad "<2>" gives "<7>"
  289. // Spaces "<_2>" gives "< 7>"
  290. // Zeros "<02>" gives "<07>"
  291. // Suppressed pad "04:05" gives "06:39"
  292. }
  293. func ExampleTime_GoString() {
  294. t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  295. fmt.Println(t.GoString())
  296. t = t.Add(1 * time.Minute)
  297. fmt.Println(t.GoString())
  298. t = t.AddDate(0, 1, 0)
  299. fmt.Println(t.GoString())
  300. t, _ = time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Feb 3, 2013 at 7:54pm (UTC)")
  301. fmt.Println(t.GoString())
  302. // Output:
  303. // time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  304. // time.Date(2009, time.November, 10, 23, 1, 0, 0, time.UTC)
  305. // time.Date(2009, time.December, 10, 23, 1, 0, 0, time.UTC)
  306. // time.Date(2013, time.February, 3, 19, 54, 0, 0, time.UTC)
  307. }
  308. func ExampleParse() {
  309. // See the example for Time.Format for a thorough description of how
  310. // to define the layout string to parse a time.Time value; Parse and
  311. // Format use the same model to describe their input and output.
  312. // longForm shows by example how the reference time would be represented in
  313. // the desired layout.
  314. const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  315. t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
  316. fmt.Println(t)
  317. // shortForm is another way the reference time would be represented
  318. // in the desired layout; it has no time zone present.
  319. // Note: without explicit zone, returns time in UTC.
  320. const shortForm = "2006-Jan-02"
  321. t, _ = time.Parse(shortForm, "2013-Feb-03")
  322. fmt.Println(t)
  323. // Some valid layouts are invalid time values, due to format specifiers
  324. // such as _ for space padding and Z for zone information.
  325. // For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
  326. // contains both Z and a time zone offset in order to handle both valid options:
  327. // 2006-01-02T15:04:05Z
  328. // 2006-01-02T15:04:05+07:00
  329. t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
  330. fmt.Println(t)
  331. t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
  332. fmt.Println(t)
  333. _, err := time.Parse(time.RFC3339, time.RFC3339)
  334. fmt.Println("error", err) // Returns an error as the layout is not a valid time value
  335. // Output:
  336. // 2013-02-03 19:54:00 -0800 PST
  337. // 2013-02-03 00:00:00 +0000 UTC
  338. // 2006-01-02 15:04:05 +0000 UTC
  339. // 2006-01-02 15:04:05 +0700 +0700
  340. // error parsing time "2006-01-02T15:04:05Z07:00": extra text: "07:00"
  341. }
  342. func ExampleParseInLocation() {
  343. loc, _ := time.LoadLocation("Europe/Berlin")
  344. // This will look for the name CEST in the Europe/Berlin time zone.
  345. const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  346. t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
  347. fmt.Println(t)
  348. // Note: without explicit zone, returns time in given location.
  349. const shortForm = "2006-Jan-02"
  350. t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
  351. fmt.Println(t)
  352. // Output:
  353. // 2012-07-09 05:02:00 +0200 CEST
  354. // 2012-07-09 00:00:00 +0200 CEST
  355. }
  356. func ExampleUnix() {
  357. unixTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  358. fmt.Println(unixTime.Unix())
  359. t := time.Unix(unixTime.Unix(), 0).UTC()
  360. fmt.Println(t)
  361. // Output:
  362. // 1257894000
  363. // 2009-11-10 23:00:00 +0000 UTC
  364. }
  365. func ExampleUnixMicro() {
  366. umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  367. fmt.Println(umt.UnixMicro())
  368. t := time.UnixMicro(umt.UnixMicro()).UTC()
  369. fmt.Println(t)
  370. // Output:
  371. // 1257894000000000
  372. // 2009-11-10 23:00:00 +0000 UTC
  373. }
  374. func ExampleUnixMilli() {
  375. umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  376. fmt.Println(umt.UnixMilli())
  377. t := time.UnixMilli(umt.UnixMilli()).UTC()
  378. fmt.Println(t)
  379. // Output:
  380. // 1257894000000
  381. // 2009-11-10 23:00:00 +0000 UTC
  382. }
  383. func ExampleTime_Unix() {
  384. // 1 billion seconds of Unix, three ways.
  385. fmt.Println(time.Unix(1e9, 0).UTC()) // 1e9 seconds
  386. fmt.Println(time.Unix(0, 1e18).UTC()) // 1e18 nanoseconds
  387. fmt.Println(time.Unix(2e9, -1e18).UTC()) // 2e9 seconds - 1e18 nanoseconds
  388. t := time.Date(2001, time.September, 9, 1, 46, 40, 0, time.UTC)
  389. fmt.Println(t.Unix()) // seconds since 1970
  390. fmt.Println(t.UnixNano()) // nanoseconds since 1970
  391. // Output:
  392. // 2001-09-09 01:46:40 +0000 UTC
  393. // 2001-09-09 01:46:40 +0000 UTC
  394. // 2001-09-09 01:46:40 +0000 UTC
  395. // 1000000000
  396. // 1000000000000000000
  397. }
  398. func ExampleTime_Round() {
  399. t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC)
  400. round := []time.Duration{
  401. time.Nanosecond,
  402. time.Microsecond,
  403. time.Millisecond,
  404. time.Second,
  405. 2 * time.Second,
  406. time.Minute,
  407. 10 * time.Minute,
  408. time.Hour,
  409. }
  410. for _, d := range round {
  411. fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999"))
  412. }
  413. // Output:
  414. // t.Round( 1ns) = 12:15:30.918273645
  415. // t.Round( 1µs) = 12:15:30.918274
  416. // t.Round( 1ms) = 12:15:30.918
  417. // t.Round( 1s) = 12:15:31
  418. // t.Round( 2s) = 12:15:30
  419. // t.Round( 1m0s) = 12:16:00
  420. // t.Round( 10m0s) = 12:20:00
  421. // t.Round(1h0m0s) = 12:00:00
  422. }
  423. func ExampleTime_Truncate() {
  424. t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
  425. trunc := []time.Duration{
  426. time.Nanosecond,
  427. time.Microsecond,
  428. time.Millisecond,
  429. time.Second,
  430. 2 * time.Second,
  431. time.Minute,
  432. 10 * time.Minute,
  433. }
  434. for _, d := range trunc {
  435. fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
  436. }
  437. // To round to the last midnight in the local timezone, create a new Date.
  438. midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  439. _ = midnight
  440. // Output:
  441. // t.Truncate( 1ns) = 12:15:30.918273645
  442. // t.Truncate( 1µs) = 12:15:30.918273
  443. // t.Truncate( 1ms) = 12:15:30.918
  444. // t.Truncate( 1s) = 12:15:30
  445. // t.Truncate( 2s) = 12:15:30
  446. // t.Truncate( 1m0s) = 12:15:00
  447. // t.Truncate(10m0s) = 12:10:00
  448. }
  449. func ExampleLoadLocation() {
  450. location, err := time.LoadLocation("America/Los_Angeles")
  451. if err != nil {
  452. panic(err)
  453. }
  454. timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC)
  455. fmt.Println(timeInUTC.In(location))
  456. // Output: 2018-08-30 05:00:00 -0700 PDT
  457. }
  458. func ExampleLocation() {
  459. // China doesn't have daylight saving. It uses a fixed 8 hour offset from UTC.
  460. secondsEastOfUTC := int((8 * time.Hour).Seconds())
  461. beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
  462. // If the system has a timezone database present, it's possible to load a location
  463. // from that, e.g.:
  464. // newYork, err := time.LoadLocation("America/New_York")
  465. // Creating a time requires a location. Common locations are time.Local and time.UTC.
  466. timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
  467. sameTimeInBeijing := time.Date(2009, 1, 1, 20, 0, 0, 0, beijing)
  468. // Although the UTC clock time is 1200 and the Beijing clock time is 2000, Beijing is
  469. // 8 hours ahead so the two dates actually represent the same instant.
  470. timesAreEqual := timeInUTC.Equal(sameTimeInBeijing)
  471. fmt.Println(timesAreEqual)
  472. // Output:
  473. // true
  474. }
  475. func ExampleTime_Add() {
  476. start := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
  477. afterTenSeconds := start.Add(time.Second * 10)
  478. afterTenMinutes := start.Add(time.Minute * 10)
  479. afterTenHours := start.Add(time.Hour * 10)
  480. afterTenDays := start.Add(time.Hour * 24 * 10)
  481. fmt.Printf("start = %v\n", start)
  482. fmt.Printf("start.Add(time.Second * 10) = %v\n", afterTenSeconds)
  483. fmt.Printf("start.Add(time.Minute * 10) = %v\n", afterTenMinutes)
  484. fmt.Printf("start.Add(time.Hour * 10) = %v\n", afterTenHours)
  485. fmt.Printf("start.Add(time.Hour * 24 * 10) = %v\n", afterTenDays)
  486. // Output:
  487. // start = 2009-01-01 12:00:00 +0000 UTC
  488. // start.Add(time.Second * 10) = 2009-01-01 12:00:10 +0000 UTC
  489. // start.Add(time.Minute * 10) = 2009-01-01 12:10:00 +0000 UTC
  490. // start.Add(time.Hour * 10) = 2009-01-01 22:00:00 +0000 UTC
  491. // start.Add(time.Hour * 24 * 10) = 2009-01-11 12:00:00 +0000 UTC
  492. }
  493. func ExampleTime_AddDate() {
  494. start := time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC)
  495. oneDayLater := start.AddDate(0, 0, 1)
  496. oneMonthLater := start.AddDate(0, 1, 0)
  497. oneYearLater := start.AddDate(1, 0, 0)
  498. fmt.Printf("oneDayLater: start.AddDate(0, 0, 1) = %v\n", oneDayLater)
  499. fmt.Printf("oneMonthLater: start.AddDate(0, 1, 0) = %v\n", oneMonthLater)
  500. fmt.Printf("oneYearLater: start.AddDate(1, 0, 0) = %v\n", oneYearLater)
  501. // Output:
  502. // oneDayLater: start.AddDate(0, 0, 1) = 2009-01-02 00:00:00 +0000 UTC
  503. // oneMonthLater: start.AddDate(0, 1, 0) = 2009-02-01 00:00:00 +0000 UTC
  504. // oneYearLater: start.AddDate(1, 0, 0) = 2010-01-01 00:00:00 +0000 UTC
  505. }
  506. func ExampleTime_After() {
  507. year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
  508. year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)
  509. isYear3000AfterYear2000 := year3000.After(year2000) // True
  510. isYear2000AfterYear3000 := year2000.After(year3000) // False
  511. fmt.Printf("year3000.After(year2000) = %v\n", isYear3000AfterYear2000)
  512. fmt.Printf("year2000.After(year3000) = %v\n", isYear2000AfterYear3000)
  513. // Output:
  514. // year3000.After(year2000) = true
  515. // year2000.After(year3000) = false
  516. }
  517. func ExampleTime_Before() {
  518. year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
  519. year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)
  520. isYear2000BeforeYear3000 := year2000.Before(year3000) // True
  521. isYear3000BeforeYear2000 := year3000.Before(year2000) // False
  522. fmt.Printf("year2000.Before(year3000) = %v\n", isYear2000BeforeYear3000)
  523. fmt.Printf("year3000.Before(year2000) = %v\n", isYear3000BeforeYear2000)
  524. // Output:
  525. // year2000.Before(year3000) = true
  526. // year3000.Before(year2000) = false
  527. }
  528. func ExampleTime_Date() {
  529. d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
  530. year, month, day := d.Date()
  531. fmt.Printf("year = %v\n", year)
  532. fmt.Printf("month = %v\n", month)
  533. fmt.Printf("day = %v\n", day)
  534. // Output:
  535. // year = 2000
  536. // month = February
  537. // day = 1
  538. }
  539. func ExampleTime_Day() {
  540. d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
  541. day := d.Day()
  542. fmt.Printf("day = %v\n", day)
  543. // Output:
  544. // day = 1
  545. }
  546. func ExampleTime_Equal() {
  547. secondsEastOfUTC := int((8 * time.Hour).Seconds())
  548. beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
  549. // Unlike the equal operator, Equal is aware that d1 and d2 are the
  550. // same instant but in different time zones.
  551. d1 := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
  552. d2 := time.Date(2000, 2, 1, 20, 30, 0, 0, beijing)
  553. datesEqualUsingEqualOperator := d1 == d2
  554. datesEqualUsingFunction := d1.Equal(d2)
  555. fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator)
  556. fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction)
  557. // Output:
  558. // datesEqualUsingEqualOperator = false
  559. // datesEqualUsingFunction = true
  560. }
  561. func ExampleTime_String() {
  562. timeWithNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 15, time.UTC)
  563. withNanoseconds := timeWithNanoseconds.String()
  564. timeWithoutNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 0, time.UTC)
  565. withoutNanoseconds := timeWithoutNanoseconds.String()
  566. fmt.Printf("withNanoseconds = %v\n", string(withNanoseconds))
  567. fmt.Printf("withoutNanoseconds = %v\n", string(withoutNanoseconds))
  568. // Output:
  569. // withNanoseconds = 2000-02-01 12:13:14.000000015 +0000 UTC
  570. // withoutNanoseconds = 2000-02-01 12:13:14 +0000 UTC
  571. }
  572. func ExampleTime_Sub() {
  573. start := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
  574. end := time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC)
  575. difference := end.Sub(start)
  576. fmt.Printf("difference = %v\n", difference)
  577. // Output:
  578. // difference = 12h0m0s
  579. }
  580. func ExampleTime_AppendFormat() {
  581. t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC)
  582. text := []byte("Time: ")
  583. text = t.AppendFormat(text, time.Kitchen)
  584. fmt.Println(string(text))
  585. // Output:
  586. // Time: 11:00AM
  587. }
  588. func ExampleFixedZone() {
  589. loc := time.FixedZone("UTC-8", -8*60*60)
  590. t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)
  591. fmt.Println("The time is:", t.Format(time.RFC822))
  592. // Output: The time is: 10 Nov 09 23:00 UTC-8
  593. }