golang中time包用法
time包中包括两类时间:时间点(某一时刻)和时常(某一段时间) 1时间常量(时间格式化)
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday,02-Jan-06 15:04:05 MST" RFC1123 = "Mon,02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon,02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )这些常量是在time包中进行time 格式化 和time解析而预定义的一些常量,其实他们使用的都是一个特定的时间: Mon Jan 2 15:04:05 MST 2006这个时间是Unix time 1136239445,因为MST是GMT-0700,所以这个指定的时间也可以看做
01/02 03:04:05PM '06 -0700 可见程序猿也有调皮的一面. 因此我们只需要利用上面这些时间变可以随意的指定自己的时间格式,例如: layout := "01__02-2006 3.04.05 PM" fmt.Println(time.Now().Format(layout)) 便会输出类似的时间:11__26-2014 8.40.00 PM 2 函数 time 组成: time.Duration(时间长度,消耗时间) time.Time(时间点) time.C(放时间的channel通道)(注:Time.C:=make(chan time.Time)) After函数: 1)func After(d Duration) <-chan Time 表示多少时间之后,但是在取出channel内容之前不阻塞,后续程序可以继续执行 2)func Sleep(d Duration) 表示休眠多少时间,休眠时处于阻塞状态,后续程序无法执行. 举例说明二者区别: fmt.Println("hello") chan := time.After(time.Secone*1) fmt.Println("World") 则程序在执行完输出hello后,接着便输出world,不用等待1s,但是1s后,chan中有数据,此时chan阻塞 mt.Println("hello") chan := time.Sleep(time.Secone*1) 则表示在输出hello后,程序变休眠1s中,然后才输出World.由此可见阻塞和非阻塞便是这两个函数的本质区别. 鉴于After特性,其通常用来处理程序超时问题,如下所示:
select { case m := <-c: handle(m) case <-time.After(5 * time.Minute): fmt.Println("timed out") } 3)func Tick(d Duration) <-chan Time time.Tick(time.Duration)用法和time.After差不多, 但是它是表示每隔多少时间之后,是一个重复的过程 ,其他与After一致 4)type Duration int64 //时间长度,其对应的时间单位有Nanosecond,Microsecond,Millisecond,Second,Minute,Hour (7)func(d Duration) String() string //与ParseDuration函数相反,该函数是将时间段转化为字符串输出 5) type Location 例子如下:
使用时间控制停止tickerticker := time.NewTicker(time.Millisecond * 500) go func() { for t := range ticker.C { fmt.Println("Tick at",t) } }() time.Sleep(time.Millisecond * 1500) //阻塞,则执行次数为sleep的休眠时间/ticker的时间 ticker.Stop() fmt.Println("Ticker stopped") 使用channel控制停止tickerticker := time.NewTicker(time.Millisecond * 500) c := make(chan int,num) //num为指定的执行次数 go func() { for t := range ticker.C { c<-1 fmt.Println("Tick at",t) } }()ticker.Stop()这种情况下,在执行num次以Ticker时间为单位的函数之后,c channel中已满,以后便不会再执行对应的函数. 9)type Time //包括日期和时间 func Date(year int,month Month,day,hour,min,sec,nsec int,loc *Location) Time //按照指定格式输入数据后,便会按照如下格式输出对应的时间,输出格式为 yyyy-mm-dd hh:mm:ss + nsec nanoseconds, 其中loc必须指定,否则便会panic, 例子如下: t := time.Date(2009,time.November,10,23,time.UTC) fmt.Printf("Go launched at %sn",t.Local()) 输出为:func Now() Time //返回当前时间,包括日期,时间和时区Go launched at 2009-11-10 15:00:00 -0800 PST func (t Time) Clock() (hour,sec int) //获取时间t的hour,min和second func (t Time) Format(layout string) string //时间字符串格式化 func (t Time) GobEncode() ([]byte,error)//解码god 举例说明:
a := time.Now()
fmt.Println(a) 输出:2014-11-27 14:58:31.583853811 +0800 CST
b,_ := a.MarshalText()
fmt.Println(b) 输出:[50 48 49 52 45 49 49 45 50 55 84 49 52 58 53 56 58 51 49 46 53 56 51 56 53 51 56 49 49 43 48 56 58 48 48]
var c = new(time.Time)
fmt.Println(c) 输出:0001-01-01 00:00:00 +0000 UTC
c.UnmarshalText(b)
fmt.Println(c) 输出:2014-11-27 14:58:31.583853811 +0800 CST
可见Marshal类的函数只是提供一个将时间t序列化为[]byte数组的功能,利用UnMarshal类的函数可以获取到原来的时间t
示例如下: 代码: t := time.Date(0,12,15,30,918273645,time.UTC) round := []time.Duration{ time.Nanosecond,time.Microsecond,time.Millisecond,time.Second,2 * time.Second,time.Minute,10 * time.Minute,time.Hour,} for _,d := range round { fmt.Printf("t.Round(%6s) = %sn",d,t.Round(d).Format("15:04:05.999999999")) } 输出: t.Round( 1ns) = 12:15:30.918273645 t.Round( 1μs) = 12:15:30.918274 t.Round( 1ms) = 12:15:30.918 t.Round( 1s) = 12:15:31 t.Round( 2s) = 12:15:30 t.Round( 1m0s) = 12:16:00 t.Round( 10m0s) = 12:20:00 t.Round(1h0m0s) = 12:00:00 func (t Time) Second() int //获取时间t的秒 示例代码如下: 代码: t,_ := time.Parse("2006 Jan 02 15:04:05","2012 Dec 07 12:15:30.918273645") trunc := []time.Duration{ time.Nanosecond,d := range trunc { fmt.Printf("t.Truncate(%6s) = %sn",t.Truncate(d).Format("15:04:05.999999999")) } t.Truncate( 1ns) = 12:15:30.918273645
t.Truncate( 1μs) = 12:15:30.918273
t.Truncate( 1ms) = 12:15:30.918
t.Truncate( 1s) = 12:15:30
t.Truncate( 2s) = 12:15:30
t.Truncate( 1m0s) = 12:15:00
t.Truncate( 10m0s) = 12:10:00
t.Truncate(1h0m0s) = 12:00:00
11)type Weekday func (d Weekday) String() string //获取一周的字符串 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |