时间类型,包含了秒和纳秒以及Locationjson
type Month int 月份.定义了十二个月的常量函数
type Weekday int 周,定义了一周的七天编码
type Duration int64 持续时间.定义了如下持续时间类型.多用于时间的加减 须要传入Duration作为参数的时候.能够直接传入time.Secondspa
const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute )
在time包里有两个时区变量:
time.UTC utc时间
time.Local 本地时间
FixedZone(name string, offset int) *Location
设置时区名,以及与UTC0的时间误差.返回Locationcode
在其余语言通常格式化字符串是yyyy-MM-dd HH:mm:ss
这种.
这个的Go语言的话是2006-01-02 15:04:05
,这方式比较特别,按照123456来记忆吧:01月02号 下午3点04分05秒2006年.orm
传入目标模板(Mon Jan 02 15:04:05 -0700 2006).时间以这个为准字符串
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())
将字符窜转换为Time类型.string
p := fmt.Println withNanos := "2006-01-02 15:04:05" t, _ := time.Parse(withNanos, "2013-10-05 18:30:50") p(t.Year())
将字duration符串(“ns”, “us” (or “ns”), “ms”, “s”, “m”, “h”.)转换为Duration类型.就是纳秒it
p := fmt.Println t, _ := time.ParseDuration("1h") p(t.Seconds())
time经常使用函数io
获取当前时间,返回Time类型
根据秒数和纳秒,返回Time类型
设置年月日返回,Time类型
返回与当前时间的时间差
时间类型比较,是否在Time以后
时间类型比较,是否在Time以前
比较两个时间是否相等
判断时间是否为零值,若是sec和nsec两个属性都是0的话,则该时间类型为0
返回年月日,三个参数
返回年份
返回月份.是Month类型
返回多少号
返回星期几,是Weekday类型
返回年份,和该填是在这年的第几周.
返回小时,分钟,秒
返回小时
返回分钟
返回秒数
返回纳秒
为一个时间,添加的时间类型为Duration.更精确到纳秒.比起AddDate
计算两个时间的差.返回类型Duration
添加时间.以年月日为参数
设置location为UTC,而后返回时间.就是utc为0.比中国晚了八个小时.
设置location为本地时间.就是电脑时间.
设置location为指定location
获取时间的Location,若是是nic,返回UTC,若是为空,则表明本地
返回时区,以及与utc的时间误差
返回时间戳,自从1970年1月1号到如今
返回时间戳.包含纳秒
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)) }
编码为gob
从gob解码
编列为json
解码为json
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)) }