1
0
mirror of https://github.com/golang/go synced 2024-11-21 17:04:42 -07:00

doc/go1: time

R=rsc
CC=golang-dev
https://golang.org/cl/5477077
This commit is contained in:
Rob Pike 2011-12-12 21:08:03 -08:00
parent f76bd4fe0f
commit 5fa18e1061
3 changed files with 173 additions and 24 deletions

View File

@ -417,17 +417,17 @@ As a result, structs and arrays can now be used as map keys:
</p> </p>
<pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}} <pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}}
--> // type Day struct { --> type Day struct {
// long string long string
// short string short string
// } }
// Christmas := Day{&#34;Christmas&#34;, &#34;XMas&#34;} Christmas := Day{&#34;Christmas&#34;, &#34;XMas&#34;}
// Thanksgiving := Day{&#34;Thanksgiving&#34;, &#34;Turkey&#34;} Thanksgiving := Day{&#34;Thanksgiving&#34;, &#34;Turkey&#34;}
// holiday := map[Day]bool { holiday := map[Day]bool{
// Christmas: true, Christmas: true,
// Thanksgiving: true, Thanksgiving: true,
// } }
// fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas]) fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas])
</pre> </pre>
<p> <p>
@ -626,6 +626,78 @@ rather than <code>syscall</code> and so will be unaffected.
<h3 id="time">Time</h3> <h3 id="time">Time</h3>
<p>
One of the most sweeping changes in the Go 1 library is the
complete redesign of the
<a href="/pkg/time/"><code>time</code></a> package.
Instead of an integer number of nanoseconds as an <code>int64</code>,
and a separate <code>*time.Time</code> type to deal with human
units such as hours and years,
there are now two fundamental types:
<a href="/pkg/time/#Time"><code>time.Time</code></a>
(a value, so the <code>*</code> is gone), which represents a moment in time;
and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>,
which represents an interval.
Both have nanosecond resolution.
A <code>Time</code> can represent any time into the ancient
past and remote future, while a <code>Duration</code> can
span plus or minus only about 290 years.
There are methods on these types, plus a number of helpful
predefined constant durations such as <code>time.Second</code>.
</p>
<p>
Among the new methods are things like
<a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>,
which adds a <code>Duration</code> to a <code>Time</code>, and
<a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>,
which subtracts two <code>Times</code> to yield a <code>Duration</code>.
</p>
<p>
The most important semantic change is that the Unix epoch (Jan 1, 1970) is now
relevant only for those functions and methods that mention Unix:
<a href="/pkg/time/#Unix"><code>time.Unix</code></a>
and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a>
and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods
of the <code>Time</code> type.
In particular,
<a href="/pkg/time/#Now"><code>time.Now</code></a>
returns a <code>time.Time</code> value rather than, in the old
API, an integer nanosecond count since the Unix epoch.
</p>
<pre><!--{{code "progs/go1.go" `/sleepUntil/` `/^}/`}}
-->// sleepUntil sleeps until the specified time. It returns immediately if it&#39;s too late.
func sleepUntil(wakeup time.Time) {
now := time.Now() // A Time.
if !wakeup.After(now) {
return
}
delta := wakeup.Sub(now) // A Duration.
log.Printf(&#34;Sleeping for %.3fs&#34;, delta.Seconds())
time.Sleep(delta)
}
</pre>
<p>
The new types, methods, and constants have been propagated through
all the standard packages that use time, such as <code>os</code> and
its representation of file time stamps.
</p>
<p>
<em>Updating</em>:
Gofix will update many uses of the old <code>time</code> package to use the new
types and methods, although it does not replace values such as <code>1e9</code>
representing nanoseconds per second.
Also, because of type changes in some of the values that arise,
some of the expressions rewritten by gofix may require
further hand editing; in such cases the rewrite will include
the correct function or method for the old functionality, but
may have the wrong type or require further analysis.
</p>
<h3 id="html">The html package</h3> <h3 id="html">The html package</h3>
<p> <p>

View File

@ -529,6 +529,67 @@ rather than <code>syscall</code> and so will be unaffected.
<h3 id="time">Time</h3> <h3 id="time">Time</h3>
<p>
One of the most sweeping changes in the Go 1 library is the
complete redesign of the
<a href="/pkg/time/"><code>time</code></a> package.
Instead of an integer number of nanoseconds as an <code>int64</code>,
and a separate <code>*time.Time</code> type to deal with human
units such as hours and years,
there are now two fundamental types:
<a href="/pkg/time/#Time"><code>time.Time</code></a>
(a value, so the <code>*</code> is gone), which represents a moment in time;
and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>,
which represents an interval.
Both have nanosecond resolution.
A <code>Time</code> can represent any time into the ancient
past and remote future, while a <code>Duration</code> can
span plus or minus only about 290 years.
There are methods on these types, plus a number of helpful
predefined constant durations such as <code>time.Second</code>.
</p>
<p>
Among the new methods are things like
<a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>,
which adds a <code>Duration</code> to a <code>Time</code>, and
<a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>,
which subtracts two <code>Times</code> to yield a <code>Duration</code>.
</p>
<p>
The most important semantic change is that the Unix epoch (Jan 1, 1970) is now
relevant only for those functions and methods that mention Unix:
<a href="/pkg/time/#Unix"><code>time.Unix</code></a>
and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a>
and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods
of the <code>Time</code> type.
In particular,
<a href="/pkg/time/#Now"><code>time.Now</code></a>
returns a <code>time.Time</code> value rather than, in the old
API, an integer nanosecond count since the Unix epoch.
</p>
{{code "progs/go1.go" `/sleepUntil/` `/^}/`}}
<p>
The new types, methods, and constants have been propagated through
all the standard packages that use time, such as <code>os</code> and
its representation of file time stamps.
</p>
<p>
<em>Updating</em>:
Gofix will update many uses of the old <code>time</code> package to use the new
types and methods, although it does not replace values such as <code>1e9</code>
representing nanoseconds per second.
Also, because of type changes in some of the values that arise,
some of the expressions rewritten by gofix may require
further hand editing; in such cases the rewrite will include
the correct function or method for the old functionality, but
may have the wrong type or require further analysis.
</p>
<h3 id="html">The html package</h3> <h3 id="html">The html package</h3>
<p> <p>

View File

@ -10,6 +10,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"time"
"unicode" "unicode"
) )
@ -22,6 +23,7 @@ func main() {
compositeLiterals() compositeLiterals()
runeType() runeType()
errorExample() errorExample()
timePackage()
} }
func mapDelete() { func mapDelete() {
@ -50,6 +52,9 @@ func mapIteration() {
} }
} }
func f(string, int) {
}
func assert(t bool) { func assert(t bool) {
if !t { if !t {
log.Panic("assertion fail") log.Panic("assertion fail")
@ -74,18 +79,17 @@ func multipleAssignment() {
} }
func structEquality() { func structEquality() {
// Feature not net in repo. type Day struct {
// type Day struct { long string
// long string short string
// short string }
// } Christmas := Day{"Christmas", "XMas"}
// Christmas := Day{"Christmas", "XMas"} Thanksgiving := Day{"Thanksgiving", "Turkey"}
// Thanksgiving := Day{"Thanksgiving", "Turkey"} holiday := map[Day]bool{
// holiday := map[Day]bool { Christmas: true,
// Christmas: true, Thanksgiving: true,
// Thanksgiving: true, }
// } fmt.Printf("Christmas is a holiday: %t\n", holiday[Christmas])
// fmt.Printf("Christmas is a holiday: %t\n", holiday[Christmas])
} }
func compositeLiterals() { func compositeLiterals() {
@ -156,7 +160,19 @@ func errorExample() {
} }
} }
func f(string, int) { // sleepUntil sleeps until the specified time. It returns immediately if it's too late.
func sleepUntil(wakeup time.Time) {
now := time.Now() // A Time.
if !wakeup.After(now) {
return
}
delta := wakeup.Sub(now) // A Duration.
log.Printf("Sleeping for %.3fs", delta.Seconds())
time.Sleep(delta)
}
func timePackage() {
sleepUntil(time.Now().Add(123 * time.Millisecond))
} }
func initializationFunction(c chan int) { func initializationFunction(c chan int) {