time _ golang

Go's offers extensive support for times and durations; here are some example

package main

import (
    "fmt"
    "time"
)

func main() {

    p := fmt.Println

    now := time.Now()
    p(now)

    then := time.Date(
        2009, 11, 17, 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.Before(now))
    p(then.After(now))
    p(then.Equal(now))

    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))
}
2015-03-25 14:12:14.302895576 +0800 CST
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
true
false
false
46881h37m15.651508339s
46881.62101430787
2.8128972608584724e+06
1.6877383565150833e+08
168773835651508339
2015-03-25 06:12:14.302895576 +0000 UTC
2004-07-13 10:57:42.999878898 +0000 UTC

 

posted on 2015-03-25 14:13  xjk112  阅读(266)  评论(0编辑  收藏  举报