http://blog.chinaunix.net/uid-24774106-id-4006530.html
编程离不开时间,时间管理,严格的说分成两块,一个是当前的时刻,对应的是一个点,还有是一段时间间隔。本文简单的讲讲go的时间相关的编程,比较简单,高手可以一笑而过。 golang对时间的支持,是package time做的事儿,里面有好多的函数,我就不一一举例学习,毕竟这是官方文档干的事情。我们初步的学习下常用的函数。 第一个是UNIX epoch time,确切的说就是自1970-01-01 00:00:00 GMT以来的秒数,不知道如何获取的,可以在shell下执行date +%s
- manu@manu-hacks:~/code/go/self$ date+%s
- 1385131172
熟悉Linux下C编程的就是time函数的返回值:
#include<time.h>
time_tnow=(NULL);
golang中一个很重要的表征时间的数据类型是Time,基本就是三个成员变量 sec ,nsec,Location,详细意思可以参看注释。
typeTimestruct{
// sec gives the numberofseconds elapsed since
// January 1,year 1 00:00:00 UTC.
sec int64
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must beinthe range[0]nsec int32
// loc specifies the Location that should be used to
// determine the minutemonthandyear
// that correspond tothisTime.
// Only the zero Time has a nil Location.
//Inthat case it is interpreted to mean UTCloc*Location
}
OK,如何取到UNIX epoch time.
.Now)
用time package中Now()函数获取到当前的时间信息,Now()函数非常的重要,他是后面一切转换的起始点。从Now()我们获取到了Time,从Time类型我们从容的获取到UNIX epoch time,自然获取到year ,month ,day,weekday,hour,minute,second,nanosecond.
获取UNIX epoch time:
var epoch_seconds int64.Unix)
获取Year
func(tTime)Yearint
cur_year)
获取Month
MonthMonth
cur_month)
if cur_month ==time.November{
...
}
Month是int类型,fmt.Printf("%v") 或者fmt.Println可以打印出November来,同时Month type有String()函数,输出“November”这样的字符串
const(
January Month=1+iota
February
March
April
May
June
July
August
September
October
November
December
year mon day,这些都可以在Date函数中一并返回:
(t Time)Date(yearint)
year=now.Date获取Hour
- Hourint
cur_hour
Minute可以通过Minute()返回,second可以通过Second()返回。
time还提供了Clock()的同时返回 hour,second = now.Clock().
在C语言中,我们用gmtime_r获取UTC时间,localtime_r获取本地时间,Golang我们也可以做到
#include<stdio>
#include<stdlib>
#include>
intmain)
{
time_t;
printf("elapsed %d second since 1970-01-01 00:00:00n";
struct tm now_utc_tm={0};
if(gmtime_r&&now_utc_tm!{
printf"UTC time is %d-%02d-%02d %02d:%02d:%02d %sn"
now_utc_tm.tm_year+1900.tm_mon.tm_mday.tm_hour.tm_min.tm_sec.tm_zone}
struct tm now_local_tm(localtime_r&now_local_tm"local time is %d-%02d-%02d %02d:%02d:%02d %sn"
now_local_tm}
return 0;
golang的版本是:
package main
import"fmt"
import"time"
func main{
)
yearday.UTCDatehoursec.Clock)
zone.Zone)
fmt.Printf"UTC time is %d-%d-%d %02d:%02d:%02d %sn"
)
"local time is %d-%d-%d %02d:%02d:%02d %sn"输出分别是:
C版本的输出
--
UTCis2013-10-22 15:49:18 GMT
local-22 23:18 CST
go版本的输出
-
UTC -11:51:22 UTC
local:22 CST
-------------------------------------------------------------------------------------------------------------------------------------------------------------
我们另一个关心的话题,是时间间隔,比如我们profile一个以非常耗时的function,我们会在函数开始前记下时刻值,函数结束后,再次记录下时刻值,然后两者的差值,就是函数运行时间。
这表明Time是可以相减的,
start_time)
expensive_function
end_time)
var duration Duration=end_time.Sub(start_time Duration是一种数据类型,其实是个int64类型,表征的是两个时刻之间的纳秒数。
typeDuration int64
const(
Nanosecond Duration=1
Microsecond=1000*Nanosecond
Millisecond*Microsecond
Second*Millisecond
Minute=60*Second
Hour*Minute
Duration类型有Minutes()/Second()/Nanoseconds(),将duration折算成分钟/秒/纳秒。
now := time.Now()
time.Sleep(3*Second;
end_time)
var dur_time.Duration)
var elapsed_min float64=dur_time.Minutes)
var elapsed_sec float64.Seconds)
var elapsed_nano int64.Nanoseconds"elasped %f minutes or nelapsed %f seconds or nelapsed %d nanosecondsn"
elapsed_minelapsed_nano输出如下:
elasped 0.050005 minutesor
elapsed 3.000292 secondsor
elapsed 3000292435 nanoseconds
------------------------------------------------------------------------------------------------------------------------------------------------
第二部分描述Duration明显用到了Sleep()函数,这个函数是以纳秒为单位的,相当于C语言中的nanosleep()
>
nanosleep(): _POSIX_C_SOURCE >= 199309L
intnanosleepconststruct timespec*req*rem;
#include unsigned int sleep(unsigned int seconds);
Go中的time.Sleep一律是以纳秒为单位的,当然本质是Duration类型:
如果sleep 3秒中需要写成:
.Sleep(3000000000)
这太不方便了,所以,Golang可以写成
.;
这样可读性就好多了,当然还有time.Minute,time.Hour
这个time package还有很多其他的内容,我就不一一赘述了。
参考文献:
1 golang package time (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|