• go 时间


    时分秒互转

    //秒转成时分秒
    func C1(sec int) string {
        var h, m, s int
        s = sec % 60
        h = sec / (60 * 60)
        m = (sec - h*60*60) / 60
        return fmt.Sprintf("%v时%v分%v秒", h, m, s)
    }
    
    //成时分秒转秒
    func C2(h, m, s int) int {
        return h*60*60 + m*60 + s
    }
    View Code

    发送心跳

    func Heartbeat() {
        tick := time.Tick(5 * time.Second)
        for {
            x, _ := <-tick
            fmt.Println(x.Format(time.RFC3339))
        }
    }
    View Code

    倒计时

    //after使用
    //倒计时
    func Delay() {
        //2.使用After(),返回值<-chan Time,同Timer.C
        ch1 := time.After(5 * time.Second)
        data := <-ch1
        fmt.Printf("data_type=%T
    ", data)
        fmt.Println("data", data)
    }
    View Code

    倒计时加定时心跳

    //可触发结束
    //
    func CycleDo() {
        // 1.获取ticker对象
        ticker := time.NewTicker(2 * time.Second)
        i := 0
        for {
            <-ticker.C
            i++
            fmt.Println("执行", time.Now())
            if i == 5 {
                //停止
                ticker.Stop()
            }
        }
        //
        fmt.Println("time over")
    }
    View Code

    倒计时

    //倒计时执行返回时间,只能被读取一次
    func DelayTime() {
        //1.timer的基本使用
        timer1 := time.NewTimer(3 * time.Second)
        timer1.Reset(3 * time.Second) //重置倒计时
        timer1.Stop()
        t1 := time.Now()
        fmt.Printf("t1:%v
    ", t1)
        t2 := <-timer1.C
        fmt.Printf("t2:%v
    ", t2)
    }
    View Code

    计时

    //统计执行时间
    func CompareTime(f func()) time.Duration {
        start := time.Now()
        f()
        return time.Now().Sub(start)
    }
    View Code

  • 相关阅读:
    Maven private reprository 更新
    Spark运行模式:cluster与client
    Spark脚本调用
    Java中hashCode与equal方法详解
    String值传递剖析
    Comparator 与 Comparable
    深入理解Java的接口和抽象类
    HitHub使用
    二叉树的递归与非递归遍历
    P1137 旅行计划
  • 原文地址:https://www.cnblogs.com/huay/p/15044970.html
Copyright © 2020-2023  润新知