先看看有哪些类型
Time
时间类型,包含了秒和纳秒以及Location
Month
type Month int 月份.定义了十二个月的常量
Weekday
type Weekday int 周,定义了一周的七天
Duration
type Duration int64 持续时间.定义了以下持续时间类型.多用于时间的加减 需要传入Duration做为参数的时候.可以直接传入time.Second
时间类型,包含了秒和纳秒以及Location
type Month int 月份.定义了十二个月的常量
type Weekday int 周,定义了一周的七天
type Duration int64 持续时间.定义了以下持续时间类型.多用于时间的加减 需要传入Duration做为参数的时候.可以直接传入time.Second
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
在time包里有两个时区变量:
FixedZone(name string, offset int) *Location
设置时区名,以及与UTC0的时间偏差.返回Location
p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
p(t.Format("2006-01-02T15:04:05Z07:00"))
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
Parse(layout, value string) (Time, error)
p := fmt.Println
t, _ := time.ParseDuration("1h")
p(t.Seconds())
func main() {
now := time.Now()
secs := now.Unix()
nanos := now.UnixNano()
fmt.Println(now)
millis := nanos / 1000000
fmt.Println(secs)
fmt.Println(millis)
fmt.Println(nanos)
fmt.Println(time.Unix(secs, 0))
fmt.Println(time.Unix(0, nanos))
}
GobEncode() ([]byte, error)
func main() {
p := fmt.Println
now := time.Now()
p(now)
d := time.Duration(7200 * 1000 * 1000 * 1000)
p(d)
then := time.Date(
2013, 1, 7, 20, 34, 58, 651387237, time.UTC)
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
p(then.Date())
p(then.ISOWeek())
p("----------")
p(now.UTC())
p(now.Local())
p(now.Location())
p(now.Zone())
p(now.Unix())
p(time.Unix(now.Unix(), 0))
p(now.UnixNano())
p(time.Unix(0, now.UnixNano()))
p(now.GobEncode())
p(now.MarshalJSON())
p(time.Since(now))
p("----------")
diff := now.Sub(then)
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
p(then.Add(diff))
p(then.Add(-diff))
p(d)
p(d.Hours())
p(d.Minutes())
p(d.Seconds())
p(d.Nanoseconds())
p(then.Add(d))
}