golang 时间编程
编程离不开时间,时间管理,严格的说分成两块,一个是当前的时刻,对应的是一个点,还有是一段时间间隔。本文简单的讲讲go的时间相关的编程,比较简单,高手可以一笑而过。 golang对时间的支持,是package time做的事儿,里面有好多的函数,我就不一一举例学习,毕竟这是官方文档干的事情。我们初步的学习下常用的函数。 第一个是UNIX epoch time,确切的说就是自1970-01-01 00:00:00 GMT以来的秒数,不知道如何获取的,可以在shell下执行 date +%s golang中一个很重要的表征时间的数据类型是Time,基本就是三个成员变量 sec ,nsec,Location,详细意思可以参看注释。 typeTimestruct{ //secgivesthenumberofsecondselapsedsince //January1,year100:00:00UTC. secint64 //nsecspecifiesanon-negativenanosecond //offsetwithinthesecondnamedbySeconds. //Itmustbeintherange[0,999999999]. nsecint32 //locspecifiestheLocationthatshouldbeusedto //determinetheminute,hour,month,day,andyear //thatcorrespondtothisTime. //OnlythezeroTimehasanilLocation. //InthatcaseitisinterpretedtomeanUTC. loc*Location } OK,如何取到UNIX epoch time. now:=time.Now() 用time package中Now()函数获取到当前的时间信息,Now()函数非常的重要,他是后面一切转换的起始点。从Now()我们获取到了Time,从Time类型我们从容的获取到UNIX epoch time,自然获取到year ,month ,day,weekday,minute,second,nanosecond. 获取UNIX epoch time: varepoch_secondsint64=now.Unix() 获取Year func(tTime)Year()int cur_year:=now.Year() 获取Month func(tTime)Month()Month cur_month:=now.Month() ifcur_month==time.November{ ... } Month是int类型,fmt.Printf("%v") 或者fmt.Println可以打印出November来,同时Month type有String()函数,输出“November”这样的字符串 const( JanuaryMonth=1+iota February March April May June July August September October November December ) year mon day,这些都可以在Date函数中一并返回: func(tTime)Date()(yearint,monthMonth,dayint) year,mon,day=now.Date() 获取Hour func(tTime)Hour()int cur_hour:=now.Hour() Minute可以通过Minute()返回,second可以通过Second()返回。 time还提供了Clock()的同时返回 hour,second = now.Clock(). packagemain import"fmt" import"time" funcmain(){ now:=time.Now() year,day:=now.UTC().Date() hour,min,sec:=now.UTC().Clock() zone,_:=now.UTC().Zone() fmt.Printf("UTCtimeis%d-%d-%d%02d:%02d:%02d%sn",year,sec,zone) year,day=now.Date() hour,sec=now.Clock() zone,_=now.Zone() fmt.Printf("localtimeis%d-%d-%d%02d:%02d:%02d%sn",zone) } go版本的输出 --------------------- UTCtimeis2013-11-2215:51:22UTC localtimeis2013-11-2223:51:22CST 我们另一个关心的话题,是时间间隔,比如我们profile一个以非常耗时的function,我们会在函数开始前记下时刻值,函数结束后,再次记录下时刻值,然后两者的差值,就是函数运行时间。 这表明Time是可以相减的 start_time:=time.Now() expensive_function end_time:=time.Now() vardurationDuration=end_time.Sub(start_time) Duration是一种数据类型,其实是个int64类型,表征的是两个时刻之间的纳秒数。 typeDurationint64 const( NanosecondDuration=1 Microsecond=1000*Nanosecond Millisecond=1000*Microsecond Second=1000*Millisecond Minute=60*Second Hour=60*Minute ) Duration类型有Minutes()/Second()/Nanoseconds(),将duration折算成分钟/秒/纳秒。 now:=time.Now() time.Sleep(3*time.Second); end_time:=time.Now() vardur_timetime.Duration=end_time.Sub(now) varelapsed_minfloat64=dur_time.Minutes() varelapsed_secfloat64=dur_time.Seconds() varelapsed_nanoint64=dur_time.Nanoseconds() fmt.Printf("elasped%fminutesornelapsed%fsecondsornelapsed%dnanosecondsn",elapsed_min,elapsed_sec,elapsed_nano) 输出如下 elasped0.050005minutesor elapsed3.000292secondsor elapsed3000292435nanoseconds Go中的time.Sleep一律是以纳秒为单位的,当然本质是Duration类型: sleep 3秒可以写成 time.Sleep(3000000000) time.Sleep(3*time.Second); packagemain import( "fmt" "time" ) const( date="2006-01-02" shortdate="06-01-02" times="15:04:02" shorttime="15:04" datetime="2006-01-0215:04:02" newdatetime="2006/01/0215~04~02" newtime="15~04~02" ) funcmain(){ thisdate:="2014-03-1714:55:06" timeformatdate,_:=time.Parse(datetime,thisdate) fmt.Println(timeformatdate) convdate:=timeformatdate.Format(date) convshortdate:=timeformatdate.Format(shortdate) convtime:=timeformatdate.Format(times) convshorttime:=timeformatdate.Format(shorttime) convnewdatetime:=timeformatdate.Format(newdatetime) convnewtime:=timeformatdate.Format(newtime) fmt.Println(convdate) fmt.Println(convshortdate) fmt.Println(convtime) fmt.Println(convshorttime) fmt.Println(convnewdatetime) fmt.Println(convnewtime) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |