github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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 func ExampleDuration_Round() { 22 d, err := time.ParseDuration("1h15m30.918273645s") 23 if err != nil { 24 panic(err) 25 } 26 27 round := []time.Duration{ 28 time.Nanosecond, 29 time.Microsecond, 30 time.Millisecond, 31 time.Second, 32 2 * time.Second, 33 time.Minute, 34 10 * time.Minute, 35 time.Hour, 36 } 37 38 for _, r := range round { 39 fmt.Printf("d.Round(%6s) = %s\n", r, d.Round(r).String()) 40 } 41 // Output: 42 // d.Round( 1ns) = 1h15m30.918273645s 43 // d.Round( 1µs) = 1h15m30.918274s 44 // d.Round( 1ms) = 1h15m30.918s 45 // d.Round( 1s) = 1h15m31s 46 // d.Round( 2s) = 1h15m30s 47 // d.Round( 1m0s) = 1h16m0s 48 // d.Round( 10m0s) = 1h20m0s 49 // d.Round(1h0m0s) = 1h0m0s 50 } 51 52 func ExampleDuration_String() { 53 t1 := time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC) 54 t2 := time.Date(2017, time.February, 16, 0, 0, 0, 0, time.UTC) 55 fmt.Println(t2.Sub(t1).String()) 56 // Output: 4440h0m0s 57 } 58 59 func ExampleDuration_Truncate() { 60 d, err := time.ParseDuration("1h15m30.918273645s") 61 if err != nil { 62 panic(err) 63 } 64 65 trunc := []time.Duration{ 66 time.Nanosecond, 67 time.Microsecond, 68 time.Millisecond, 69 time.Second, 70 2 * time.Second, 71 time.Minute, 72 10 * time.Minute, 73 time.Hour, 74 } 75 76 for _, t := range trunc { 77 fmt.Printf("t.Truncate(%6s) = %s\n", t, d.Truncate(t).String()) 78 } 79 // Output: 80 // t.Truncate( 1ns) = 1h15m30.918273645s 81 // t.Truncate( 1µs) = 1h15m30.918273s 82 // t.Truncate( 1ms) = 1h15m30.918s 83 // t.Truncate( 1s) = 1h15m30s 84 // t.Truncate( 2s) = 1h15m30s 85 // t.Truncate( 1m0s) = 1h15m0s 86 // t.Truncate( 10m0s) = 1h10m0s 87 // t.Truncate(1h0m0s) = 1h0m0s 88 89 } 90 91 var c chan int 92 93 func handle(int) {} 94 95 func ExampleAfter() { 96 select { 97 case m := <-c: 98 handle(m) 99 case <-time.After(5 * time.Minute): 100 fmt.Println("timed out") 101 } 102 } 103 104 func ExampleSleep() { 105 time.Sleep(100 * time.Millisecond) 106 } 107 108 func statusUpdate() string { return "" } 109 110 func ExampleTick() { 111 c := time.Tick(1 * time.Minute) 112 for now := range c { 113 fmt.Printf("%v %s\n", now, statusUpdate()) 114 } 115 } 116 117 func ExampleMonth() { 118 _, month, day := time.Now().Date() 119 if month == time.November && day == 10 { 120 fmt.Println("Happy Go day!") 121 } 122 } 123 124 func ExampleDate() { 125 t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) 126 fmt.Printf("Go launched at %s\n", t.Local()) 127 // Output: Go launched at 2009-11-10 15:00:00 -0800 PST 128 } 129 130 func ExampleTime_Format() { 131 // Parse a time value from a string in the standard Unix format. 132 t, err := time.Parse(time.UnixDate, "Sat Mar 7 11:06:39 PST 2015") 133 if err != nil { // Always check errors even if they should not happen. 134 panic(err) 135 } 136 137 // time.Time's Stringer method is useful without any format. 138 fmt.Println("default format:", t) 139 140 // Predefined constants in the package implement common layouts. 141 fmt.Println("Unix format:", t.Format(time.UnixDate)) 142 143 // The time zone attached to the time value affects its output. 144 fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate)) 145 146 // The rest of this function demonstrates the properties of the 147 // layout string used in the format. 148 149 // The layout string used by the Parse function and Format method 150 // shows by example how the reference time should be represented. 151 // We stress that one must show how the reference time is formatted, 152 // not a time of the user's choosing. Thus each layout string is a 153 // representation of the time stamp, 154 // Jan 2 15:04:05 2006 MST 155 // An easy way to remember this value is that it holds, when presented 156 // in this order, the values (lined up with the elements above): 157 // 1 2 3 4 5 6 -7 158 // There are some wrinkles illustrated below. 159 160 // Most uses of Format and Parse use constant layout strings such as 161 // the ones defined in this package, but the interface is flexible, 162 // as these examples show. 163 164 // Define a helper function to make the examples' output look nice. 165 do := func(name, layout, want string) { 166 got := t.Format(layout) 167 if want != got { 168 fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want) 169 return 170 } 171 fmt.Printf("%-15s %q gives %q\n", name, layout, got) 172 } 173 174 // Print a header in our output. 175 fmt.Printf("\nFormats:\n\n") 176 177 // A simple starter example. 178 do("Basic", "Mon Jan 2 15:04:05 MST 2006", "Sat Mar 7 11:06:39 PST 2015") 179 180 // For fixed-width printing of values, such as the date, that may be one or 181 // two characters (7 vs. 07), use an _ instead of a space in the layout string. 182 // Here we print just the day, which is 2 in our layout string and 7 in our 183 // value. 184 do("No pad", "<2>", "<7>") 185 186 // An underscore represents a zero pad, if required. 187 do("Spaces", "<_2>", "< 7>") 188 189 // Similarly, a 0 indicates zero padding. 190 do("Zeros", "<02>", "<07>") 191 192 // If the value is already the right width, padding is not used. 193 // For instance, the second (05 in the reference time) in our value is 39, 194 // so it doesn't need padding, but the minutes (04, 06) does. 195 do("Suppressed pad", "04:05", "06:39") 196 197 // The predefined constant Unix uses an underscore to pad the day. 198 // Compare with our simple starter example. 199 do("Unix", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015") 200 201 // The hour of the reference time is 15, or 3PM. The layout can express 202 // it either way, and since our value is the morning we should see it as 203 // an AM time. We show both in one format string. Lower case too. 204 do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h") 205 206 // When parsing, if the seconds value is followed by a decimal point 207 // and some digits, that is taken as a fraction of a second even if 208 // the layout string does not represent the fractional second. 209 // Here we add a fractional second to our time value used above. 210 t, err = time.Parse(time.UnixDate, "Sat Mar 7 11:06:39.1234 PST 2015") 211 if err != nil { 212 panic(err) 213 } 214 // It does not appear in the output if the layout string does not contain 215 // a representation of the fractional second. 216 do("No fraction", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015") 217 218 // Fractional seconds can be printed by adding a run of 0s or 9s after 219 // a decimal point in the seconds value in the layout string. 220 // If the layout digits are 0s, the fractional second is of the specified 221 // width. Note that the output has a trailing zero. 222 do("0s for fraction", "15:04:05.00000", "11:06:39.12340") 223 224 // If the fraction in the layout is 9s, trailing zeros are dropped. 225 do("9s for fraction", "15:04:05.99999999", "11:06:39.1234") 226 227 // Output: 228 // default format: 2015-03-07 11:06:39 -0800 PST 229 // Unix format: Sat Mar 7 11:06:39 PST 2015 230 // Same, in UTC: Sat Mar 7 19:06:39 UTC 2015 231 // 232 // Formats: 233 // 234 // Basic "Mon Jan 2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015" 235 // No pad "<2>" gives "<7>" 236 // Spaces "<_2>" gives "< 7>" 237 // Zeros "<02>" gives "<07>" 238 // Suppressed pad "04:05" gives "06:39" 239 // Unix "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015" 240 // AM/PM "3PM==3pm==15h" gives "11AM==11am==11h" 241 // No fraction "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015" 242 // 0s for fraction "15:04:05.00000" gives "11:06:39.12340" 243 // 9s for fraction "15:04:05.99999999" gives "11:06:39.1234" 244 245 } 246 247 func ExampleParse() { 248 // See the example for time.Format for a thorough description of how 249 // to define the layout string to parse a time.Time value; Parse and 250 // Format use the same model to describe their input and output. 251 252 // longForm shows by example how the reference time would be represented in 253 // the desired layout. 254 const longForm = "Jan 2, 2006 at 3:04pm (MST)" 255 t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") 256 fmt.Println(t) 257 258 // shortForm is another way the reference time would be represented 259 // in the desired layout; it has no time zone present. 260 // Note: without explicit zone, returns time in UTC. 261 const shortForm = "2006-Jan-02" 262 t, _ = time.Parse(shortForm, "2013-Feb-03") 263 fmt.Println(t) 264 265 // Output: 266 // 2013-02-03 19:54:00 -0800 PST 267 // 2013-02-03 00:00:00 +0000 UTC 268 } 269 270 func ExampleParseInLocation() { 271 loc, _ := time.LoadLocation("Europe/Berlin") 272 273 const longForm = "Jan 2, 2006 at 3:04pm (MST)" 274 t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc) 275 fmt.Println(t) 276 277 // Note: without explicit zone, returns time in given location. 278 const shortForm = "2006-Jan-02" 279 t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc) 280 fmt.Println(t) 281 282 // Output: 283 // 2012-07-09 05:02:00 +0200 CEST 284 // 2012-07-09 00:00:00 +0200 CEST 285 } 286 287 func ExampleTime_Round() { 288 t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC) 289 round := []time.Duration{ 290 time.Nanosecond, 291 time.Microsecond, 292 time.Millisecond, 293 time.Second, 294 2 * time.Second, 295 time.Minute, 296 10 * time.Minute, 297 time.Hour, 298 } 299 300 for _, d := range round { 301 fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999")) 302 } 303 // Output: 304 // t.Round( 1ns) = 12:15:30.918273645 305 // t.Round( 1µs) = 12:15:30.918274 306 // t.Round( 1ms) = 12:15:30.918 307 // t.Round( 1s) = 12:15:31 308 // t.Round( 2s) = 12:15:30 309 // t.Round( 1m0s) = 12:16:00 310 // t.Round( 10m0s) = 12:20:00 311 // t.Round(1h0m0s) = 12:00:00 312 } 313 314 func ExampleTime_Truncate() { 315 t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645") 316 trunc := []time.Duration{ 317 time.Nanosecond, 318 time.Microsecond, 319 time.Millisecond, 320 time.Second, 321 2 * time.Second, 322 time.Minute, 323 10 * time.Minute, 324 } 325 326 for _, d := range trunc { 327 fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999")) 328 } 329 // To round to the last midnight in the local timezone, create a new Date. 330 midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) 331 _ = midnight 332 333 // Output: 334 // t.Truncate( 1ns) = 12:15:30.918273645 335 // t.Truncate( 1µs) = 12:15:30.918273 336 // t.Truncate( 1ms) = 12:15:30.918 337 // t.Truncate( 1s) = 12:15:30 338 // t.Truncate( 2s) = 12:15:30 339 // t.Truncate( 1m0s) = 12:15:00 340 // t.Truncate(10m0s) = 12:10:00 341 }