|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
`time` 包为我们提供了一个数据类型 `time.Time`(作为值使用)以及显示和测量时间和日期的功能函数。
|
|
|
|
|
|
-当前时间可以使用 `time.Now()` 获取,或者使用 `t.Day()`、`t.Minute()` 等等来获取时间的一部分;你甚至可以自定义时间格式化字符串,例如: `fmt.Printf("%02d.%02d.%4d\n”, t.Day(), t.Month(), t.Year())` 将会输出 `21.07.2011`。
|
|
|
+当前时间可以使用 `time.Now()` 获取,或者使用 `t.Day()`、`t.Minute()` 等等来获取时间的一部分;你甚至可以自定义时间格式化字符串,例如: `fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())` 将会输出 `21.07.2011`。
|
|
|
|
|
|
Duration 类型表示两个连续时刻所相差的纳秒数,类型为 int64。Location 类型映射某个时区的时间,UTC 表示通用协调世界时间。
|
|
|
|
|
|
@@ -10,7 +10,7 @@ Duration 类型表示两个连续时刻所相差的纳秒数,类型为 int64
|
|
|
|
|
|
一般的格式化设计是通过对于一个标准时间的格式化描述来展现的,这听起来很奇怪,但看下面这个例子你就会一目了然:
|
|
|
|
|
|
- fmt.Println(t.Format(“02 Jan 2006 15:04”))
|
|
|
+ fmt.Println(t.Format("02 Jan 2006 15:04"))
|
|
|
|
|
|
输出:
|
|
|
|
|
|
@@ -22,15 +22,15 @@ Example 4.20 [time.go](examples/chapter_4/time.go)
|
|
|
|
|
|
package main
|
|
|
import (
|
|
|
- “fmt”
|
|
|
- “time”
|
|
|
+ "fmt"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
var week time.Duration
|
|
|
func main() {
|
|
|
t := time.Now()
|
|
|
fmt.Println(t) // e.g. Wed Dec 21 09:52:14 +0100 RST 2011
|
|
|
- fmt.Printf(“%02d.%02d.%4d\n”, t.Day(), t.Month(), t.Year())
|
|
|
+ fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())
|
|
|
// 21.12.2011
|
|
|
t = time.Now().UTC()
|
|
|
fmt.Println(t) // Wed Dec 21 08:52:14 +0000 UTC 2011
|
|
|
@@ -42,9 +42,9 @@ Example 4.20 [time.go](examples/chapter_4/time.go)
|
|
|
// formatting times:
|
|
|
fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
|
|
|
fmt.Println(t.Format(time.ANSIC)) // Wed Dec 21 08:56:34 2011
|
|
|
- fmt.Println(t.Format(“02 Jan 2006 15:04”)) // 21 Dec 2011 08:52
|
|
|
- s := t.Format(“20060102”)
|
|
|
- fmt.Println(t, “=>”, s)
|
|
|
+ fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
|
|
|
+ s := t.Format("20060102")
|
|
|
+ fmt.Println(t, "=>", s)
|
|
|
// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
|
|
|
}
|
|
|
|
|
|
@@ -56,4 +56,4 @@ Example 4.20 [time.go](examples/chapter_4/time.go)
|
|
|
|
|
|
- [目录](directory.md)
|
|
|
- 上一节:[strings 和 strconv 包](04.7.md)
|
|
|
-- 下一节:[指针](04.9.md)
|
|
|
+- 下一节:[指针](04.9.md)
|