互联网二十多年,已到十字路口。区块链出现前的互联网被称为古典互联网,而应用区块链技术的互联网才进入了后互联网时代。作为一项新兴的技术,区块链无疑正处于风口浪尖之上,其发展前景于普通大众而言也终将是利好。但目前由于区块链技术处于发展早期阶段,存在技术成熟度、落地应用场景有限等问题,兄弟连教育建议用户在选择专业Go语言+区块链培训机构前应进行仔细考量与辨别。
编程离不开时间,时间管理,严格的说分成两块,一个是当前的时刻,对应的是一个点,还有是一段时间间隔,本文简单的讲讲go的时间相关的编程,比较简单。
golang对时间的支持,是package time做的事儿,里面有好多的函数。
熟悉Linux下C编程的就是time函数的返回值:
#include
time_t now = time(NULL);
golang中一个很重要的表征时间的数据类型是Time,基本就是三个成员变量 sec ,nsec,Location,详细意思可以参看注释。
type Time struct {
// sec gives the number of seconds 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 be in the range [0, 999999999].
nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// Only the zero Time has a nil Location.
// In that case it is interpreted to mean UTC.
loc *Location
}
OK,如何取到UNIX epoch time.
now := 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 = now.Unix()
获取Year
func (t Time) Year() int
cur_year := now.Year()
获取Month
func (t Time) Month() Month
cur_month := now.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函数中一并返回:
func (t Time) Date() (year int, month Month, day int)
year,mon,day = now.Date()
获取Hour
func (t Time) Hour() int
cur_hour := now.Hour()
Minute可以通过Minute()返回,second可以通过Second()返回。
time还提供了Clock()的同时返回 hour,minute,second = now.Clock().
golang的版本是:
package main
import "fmt"
import "time"
func main(){
now := time.Now()
year,mon,day := now.UTC().Date()
hour,min,sec := now.UTC().Clock()
zone,_ := now.UTC().Zone()
fmt.Printf("UTC time is %d-%d-%d %02d:%02d:%02d %s\n",
year,mon,day,hour,min,sec,zone)
year,mon,day = now.Date()
hour,min,sec = now.Clock()
zone,_ = now.Zone()
fmt.Printf("local time is %d-%d-%d %02d:%02d:%02d %s\n",
year,mon,day,hour,min,sec,zone)
}
go版本的输出
---------------------
UTC time is 2013-11-22 15:51:22 UTC
local time is 2013-11-22 23:51:22 CST
-------------------------------------------------------------------------------------------------------------------------------------------------------------
我们另一个关心的话题,是时间间隔,比如我们profile一个以非常耗时的function,我们会在函数开始前记下时刻值,函数结束后,再次记录下时刻值,然后两者的差值,就是函数运行时间。
这表明Time是可以相减的,
start_time := time.Now()
expensive_function
end_time :=time.Now()
var duration Duration = end_time.Sub(start_time)
Duration是一种数据类型,其实是个int64类型,表征的是两个时刻之间的纳秒数。
type Duration int64
const (
Nanosecond Duration = 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()
var dur_time time.Duration = end_time.Sub(now)
var elapsed_min float64 = dur_time.Minutes()
var elapsed_sec float64 = dur_time.Seconds()
var elapsed_nano int64 = dur_time.Nanoseconds()
fmt.Printf("elasped %f minutes or \nelapsed %f seconds or \nelapsed %d nanoseconds\n",
elapsed_min,elapsed_sec,elapsed_nano)
输出如下:
elasped 0.050005 minutes or
elapsed 3.000292 seconds or
elapsed 3000292435 nanoseconds
------------------------------------------------------------------------------------------------------------------------------------------------
第二部分描述Duration明显用到了Sleep()函数,这个函数是以纳秒为单位的,相当于C语言中的nanosleep()
#include
nanosleep(): _POSIX_C_SOURCE >= 199309L
int nanosleep(const struct timespec *req, struct timespec *rem);
#include
unsigned int sleep(unsigned int seconds);
Go中的time.Sleep一律是以纳秒为单位的,当然本质是Duration类型:
如果sleep 3秒中需要写成:
time.Sleep(3000000000)
这太不方便了,所以,Golang可以写成
time.Sleep(3*time.Second);
高能预警,兄弟连教育区块链直播课程8月持续火爆来袭!
原价1188元的12节区块链进阶课程,现仅需1元!
还可免费领取《Go语言基础实战项目开发》与《Go语言高级实战项目开发》教材两本!!限时限量!!先到先得!!
http://www.ydma.cn/open/course/24
关注兄弟连区块链技术公众号领取更多技术干货哦!!!
有疑问加站长微信联系(非本文作者)