example_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var c chan int
  17. func handle(int) {}
  18. func ExampleAfter() {
  19. select {
  20. case m := <-c:
  21. handle(m)
  22. case <-time.After(5 * time.Minute):
  23. fmt.Println("timed out")
  24. }
  25. }
  26. func ExampleSleep() {
  27. time.Sleep(100 * time.Millisecond)
  28. }
  29. func statusUpdate() string { return "" }
  30. func ExampleTick() {
  31. c := time.Tick(1 * time.Minute)
  32. for now := range c {
  33. fmt.Printf("%v %s\n", now, statusUpdate())
  34. }
  35. }
  36. func ExampleMonth() {
  37. _, month, day := time.Now().Date()
  38. if month == time.November && day == 10 {
  39. fmt.Println("Happy Go day!")
  40. }
  41. }
  42. func ExampleDate() {
  43. t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  44. fmt.Printf("Go launched at %s\n", t.Local())
  45. // Output: Go launched at 2009-11-10 15:00:00 -0800 PST
  46. }
  47. func ExampleTime_Format() {
  48. // layout shows by example how the reference time should be represented.
  49. const layout = "Jan 2, 2006 at 3:04pm (MST)"
  50. t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
  51. fmt.Println(t.Format(layout))
  52. fmt.Println(t.UTC().Format(layout))
  53. // Output:
  54. // Nov 10, 2009 at 3:00pm (PST)
  55. // Nov 10, 2009 at 11:00pm (UTC)
  56. }
  57. func ExampleParse() {
  58. // longForm shows by example how the reference time would be represented in
  59. // the desired layout.
  60. const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  61. t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
  62. fmt.Println(t)
  63. // shortForm is another way the reference time would be represented
  64. // in the desired layout; it has no time zone present.
  65. // Note: without explicit zone, returns time in UTC.
  66. const shortForm = "2006-Jan-02"
  67. t, _ = time.Parse(shortForm, "2013-Feb-03")
  68. fmt.Println(t)
  69. // Output:
  70. // 2013-02-03 19:54:00 -0800 PST
  71. // 2013-02-03 00:00:00 +0000 UTC
  72. }
  73. func ExampleParseInLocation() {
  74. loc, _ := time.LoadLocation("Europe/Berlin")
  75. const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  76. t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
  77. fmt.Println(t)
  78. // Note: without explicit zone, returns time in given location.
  79. const shortForm = "2006-Jan-02"
  80. t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
  81. fmt.Println(t)
  82. // Output:
  83. // 2012-07-09 05:02:00 +0200 CEST
  84. // 2012-07-09 00:00:00 +0200 CEST
  85. }
  86. func ExampleTime_Round() {
  87. t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC)
  88. round := []time.Duration{
  89. time.Nanosecond,
  90. time.Microsecond,
  91. time.Millisecond,
  92. time.Second,
  93. 2 * time.Second,
  94. time.Minute,
  95. 10 * time.Minute,
  96. time.Hour,
  97. }
  98. for _, d := range round {
  99. fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999"))
  100. }
  101. // Output:
  102. // t.Round( 1ns) = 12:15:30.918273645
  103. // t.Round( 1µs) = 12:15:30.918274
  104. // t.Round( 1ms) = 12:15:30.918
  105. // t.Round( 1s) = 12:15:31
  106. // t.Round( 2s) = 12:15:30
  107. // t.Round( 1m0s) = 12:16:00
  108. // t.Round( 10m0s) = 12:20:00
  109. // t.Round(1h0m0s) = 12:00:00
  110. }
  111. func ExampleTime_Truncate() {
  112. t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
  113. trunc := []time.Duration{
  114. time.Nanosecond,
  115. time.Microsecond,
  116. time.Millisecond,
  117. time.Second,
  118. 2 * time.Second,
  119. time.Minute,
  120. 10 * time.Minute,
  121. time.Hour,
  122. }
  123. for _, d := range trunc {
  124. fmt.Printf("t.Truncate(%6s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
  125. }
  126. // Output:
  127. // t.Truncate( 1ns) = 12:15:30.918273645
  128. // t.Truncate( 1µs) = 12:15:30.918273
  129. // t.Truncate( 1ms) = 12:15:30.918
  130. // t.Truncate( 1s) = 12:15:30
  131. // t.Truncate( 2s) = 12:15:30
  132. // t.Truncate( 1m0s) = 12:15:00
  133. // t.Truncate( 10m0s) = 12:10:00
  134. // t.Truncate(1h0m0s) = 12:00:00
  135. }