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.
12 func expensiveCall() {}
14 func ExampleDuration() {
18 fmt
.Printf("The call took %v to run.\n", t1
.Sub(t0
))
29 case <-time
.After(5 * time
.Minute
):
30 fmt
.Println("timed out")
35 time
.Sleep(100 * time
.Millisecond
)
38 func statusUpdate() string { return "" }
41 c
:= time
.Tick(1 * time
.Minute
)
43 fmt
.Printf("%v %s\n", now
, statusUpdate())
48 _
, month
, day
:= time
.Now().Date()
49 if month
== time
.November
&& day
== 10 {
50 fmt
.Println("Happy Go day!")
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
))
67 // Nov 10, 2009 at 3:00pm (PST)
68 // Nov 10, 2009 at 11:00pm (UTC)
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)")
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")
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
)
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
)
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
{
120 for _
, d
:= range round
{
121 fmt
.Printf("t.Round(%6s) = %s\n", d
, t
.Round(d
).Format("15:04:05.999999999"))
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
{
147 for _
, d
:= range trunc
{
148 fmt
.Printf("t.Truncate(%6s) = %s\n", d
, t
.Truncate(d
).Format("15:04:05.999999999"))
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