2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / time / example_test.go
blobcfa5b38c5f13e86fb8b902b77368935adacd3124
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.
5 package time_test
7 import (
8 "fmt"
9 "time"
12 func expensiveCall() {}
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))
21 var c chan int
23 func handle(int) {}
25 func ExampleAfter() {
26 select {
27 case m := <-c:
28 handle(m)
29 case <-time.After(5 * time.Minute):
30 fmt.Println("timed out")
34 func ExampleSleep() {
35 time.Sleep(100 * time.Millisecond)
38 func statusUpdate() string { return "" }
40 func ExampleTick() {
41 c := time.Tick(1 * time.Minute)
42 for now := range c {
43 fmt.Printf("%v %s\n", now, statusUpdate())
47 func ExampleMonth() {
48 _, month, day := time.Now().Date()
49 if month == time.November && day == 10 {
50 fmt.Println("Happy Go day!")
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
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)
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)
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)
85 // Output:
86 // 2013-02-03 19:54:00 -0800 PST
87 // 2013-02-03 00:00:00 +0000 UTC
90 func ExampleParseInLocation() {
91 loc, _ := time.LoadLocation("Europe/Berlin")
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)
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)
102 // Output:
103 // 2012-07-09 05:02:00 +0200 CEST
104 // 2012-07-09 00:00:00 +0200 CEST
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,
120 for _, d := range round {
121 fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999"))
123 // Output:
124 // t.Round( 1ns) = 12:15:30.918273645
125 // t.Round( 1us) = 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
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,
147 for _, d := range trunc {
148 fmt.Printf("t.Truncate(%6s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
151 // Output:
152 // t.Truncate( 1ns) = 12:15:30.918273645
153 // t.Truncate( 1us) = 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