go语言常用的时间段学习 老男孩go开发培训班

    /    2019-05-22

更多资料,请关注公众号:Go程序员开发

常用的时间段

type Duration int64

Duration类型代表两个时间点之间经过的时间,以纳秒为单位。可表示的最长时间段大约290年。

const (
    Nanosecond  Duration = 1                  // 表示1纳秒
    Microsecond          = 1000 * Nanosecond  // 表示1微妙
    Millisecond          = 1000 * Microsecond // 表示1毫秒
    Second               = 1000 * Millisecond // 表示1秒
    Minute               = 60 * Second        // 表示1分钟
    Hour                 = 60 * Minute        // 表示1小时
)
func Sleep(d Duration)

Sleep阻塞当前go程至少d代表的时间段。d<=0时,sleep会立刻返回。< span="">

func (t Time) Before(u Time) bool

如果t代表的时间点在u之前,返回真;否则返回假。

func (t Time) After(u Time) bool

如果t代表的时间点在u之后,返回真;否则返回假。

代码实现:

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    after_time := time.Unix((time.Now().Unix() - 5), 0)
    before_time := time.Unix((time.Now().Unix() + 5), 0)
    after := now.After(after_time)
    before := now.Before(before_time)
    time.Sleep(5 * time.Second)
    fmt.Printf("after => %v , before => %v\n", after, before)
}


(4)

分享至