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