Files
gf/g/os/gtime/time.go
2017-12-07 10:42:37 +08:00

58 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gtime
import (
"time"
)
// 类似与js中的SetTimeout一段时间后执行回调函数
func SetTimeout(t time.Duration, callback func()) {
go func() {
time.Sleep(t)
callback()
}()
}
// 类似与js中的SetInterval每隔一段时间后执行回调函数当回调函数返回true那么继续执行否则终止执行该方法是异步的
// 注意:由于采用的是循环而不是递归操作,因此间隔时间将会以上一次回调函数执行完成的时间来计算
func SetInterval(t time.Duration, callback func() bool) {
go func() {
for {
time.Sleep(t)
r := callback()
if !r {
break;
}
}
}()
}
// 获取当前的纳秒数
func Nanosecond() int64 {
return time.Now().UnixNano()
}
// 获取当前的微秒数
func Microsecond() int64 {
return time.Now().UnixNano()/1e3
}
// 获取当前的毫秒数
func Millisecond() int64 {
return time.Now().UnixNano()/1e6
}
// 获取当前的秒数(时间戳)
func Second() int64 {
return time.Now().UnixNano()/1e9
}
// 获得当前的日期(例如2006-01-02)
func Date() string {
return time.Now().Format("2006-01-02")
}
// 获得当前的时间(例如2006-01-02 15:04:05)
func Datetime() string {
return time.Now().Format("2006-01-02 15:04:05")
}