|
|
@@ -9,10 +9,10 @@ Duration 类型表示两个连续时刻所相差的纳秒数,类型为 int64
|
|
|
包中的一个预定义函数 `func (t Time) Format(layout string) string` 可以根据一个格式化字符串来将一个时间 t 转换为相应格式的字符串,你可以使用一些预定义的格式,如:`time.ANSIC` 或 `time.RFC822`。
|
|
|
|
|
|
一般的格式化设计是通过对于一个标准时间的格式化描述来展现的,这听起来很奇怪,但看下面这个例子你就会一目了然:
|
|
|
-
|
|
|
+
|
|
|
```go
|
|
|
fmt.Println(t.Format("02 Jan 2006 15:04"))
|
|
|
-```
|
|
|
+```
|
|
|
|
|
|
输出:
|
|
|
|
|
|
@@ -21,7 +21,7 @@ fmt.Println(t.Format("02 Jan 2006 15:04"))
|
|
|
其它有关时间操作的文档请参阅 [官方文档](http://golang.org/pkg/time/)( **译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/time/)** )。
|
|
|
|
|
|
示例 4.20 [time.go](examples/chapter_4/time.go)
|
|
|
-
|
|
|
+
|
|
|
```go
|
|
|
package main
|
|
|
import (
|
|
|
@@ -40,7 +40,7 @@ func main() {
|
|
|
fmt.Println(time.Now()) // Wed Dec 21 09:52:14 +0100 RST 2011
|
|
|
// calculating times:
|
|
|
week = 60 * 60 * 24 * 7 * 1e9 // must be in nanosec
|
|
|
- week_from_now := t.Add(week)
|
|
|
+ week_from_now := t.Add(time.Duration(week))
|
|
|
fmt.Println(week_from_now) // Wed Dec 28 08:52:14 +0000 UTC 2011
|
|
|
// formatting times:
|
|
|
fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
|
|
|
@@ -49,7 +49,7 @@ func main() {
|
|
|
s := t.Format("20060102")
|
|
|
fmt.Println(t, "=>", s)
|
|
|
// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
|
|
|
-}
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
输出的结果已经写在每行 `//` 的后面。
|