欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Golang time

程序员文章站 2022-06-25 19:22:04
...
package main

import (
    "time"
    "fmt"
)
func main() {
    fmt.Println(time.Now().Unix() - 7*24*60*60)
    t1 := time.Now()
    t2 := t1.Add(time.Hour)
    t3 := time.Unix(1487780010, 0)
    //分别指定年,月,日,时,分,秒,纳秒,时区
    ft := time.Date(2018, time.Month(1), 11, 5, 13, 32, 0, t1.Location())
    fmt.Println(ft)
    fmt.Println(t1, t2, t3)

    p := t1.Format("2006-01-02 15:04:05")
    fmt.Println(p)
    t3 := time.Unix(1487780010, 0)
    fmt.Println(t1, t2, t3)
    if t3.After(t1) {
        fmt.Println("t3 is after t1")
    } else {
        fmt.Println("t3 is before t1")
    }
    fmt.Println(time.Since(t3))
}

输出结果:

1515662587
2018-01-11 05:13:32 +0800 CST
2018-01-18 17:23:07.614331553 +0800 CST m=+0.000305934 2018-01-18 18:23:07.614331553 +0800 CST m=+3600.000305934 2017-02-23 00:13:30 +0800 CST

2018-01-18 17:38:35
t3 is before t1
8026h46m15.524359287s

time.After超时应用

package main

import (
    "fmt"
    "time"
)

func main() {
    c := make(chan bool)
    select {
    case v:= <-c:
        fmt.Println(v)
    case <-time.After(3 * time.Second):
        fmt.Println("Timeout")
    }
}

注意:使用time Format的时,最好使用常量类型的类型。比如

const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // Handy time stamps.
    Stamp      = "Jan _2 15:04:05"
    StampMilli = "Jan _2 15:04:05.000"
    StampMicro = "Jan _2 15:04:05.000000"
    StampNano  = "Jan _2 15:04:05.000000000"
)

上述都是 time包中的常量。在format 时间格式时最好使用他们,否则可能出现时间的变动。

package main


import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()
    t2 := t1.Format(time.Kitchen)
    fmt.Println(t2)
}

Output:

9:47AM

Since

func Since(t Time) Duration {
    return Now().Sub(t)
}

返回从t时刻已经过去多久

Until

func Until(t Time) Duration {
    return t.Sub(Now())
}

返回持续了多久,直到t.

t1 := time.Now()
t3 := time.Unix(t1.Unix()-7*24*60*60, 0)
fmt.Println(time.Until(t3).Seconds())

示例中,因为传入的参数是过去的一个时间点,所以,结果是负值。

Hours

func (d Duration) Hours() float64 {
    hour := d / Hour
    nsec := d % Hour
    return float64(hour) + float64(nsec)/(60*60*1e9)
}
t4,_ := time.ParseDuration("4h30m")
fmt.Printf("%v\n",t4.Hours())

Output:

4.5

Minutes

func (d Duration) Minutes() float64 {
    min := d / Minute
    nsec := d % Minute
    return float64(min) + float64(nsec)/(60*1e9)
}
t4,_ := time.ParseDuration("4h30m")
fmt.Printf("%v\n",t4.Minutes())

Output:

270

Nanoseconds纳秒

func (d Duration) Nanoseconds() int64 {
        return int64(d) 
}
ns, _ := time.ParseDuration("1000ns")
fmt.Printf("one microsecond has %d nanoseconds.", ns.Nanoseconds())

Output:

one microsecond has 1000 nanoseconds.

NewTicker

    ticker := time.NewTicker(time.Second)
    defer ticker.Stop()
    done := make(chan bool)
    go func() {
        time.Sleep(10 * time.Second)
        done <- true
    }()
    for {
        select {
        case <-done:
            fmt.Println("Done!")
            return
        case t := <-ticker.C:
            fmt.Println("Current time: ", t)
        }
    }

Output:

Current time:  2018-02-05 10:41:37.204211269 +0800 CST m=+1.001535613
Current time:  2018-02-05 10:41:38.204068714 +0800 CST m=+2.001320058
Current time:  2018-02-05 10:41:39.204252518 +0800 CST m=+3.001503862
Current time:  2018-02-05 10:41:40.204103403 +0800 CST m=+4.001281747
Current time:  2018-02-05 10:41:41.204360898 +0800 CST m=+5.001539242
Current time:  2018-02-05 10:41:42.204120805 +0800 CST m=+6.001227149
Current time:  2018-02-05 10:41:43.204406187 +0800 CST m=+7.001439531
Current time:  2018-02-05 10:41:44.203708482 +0800 CST m=+8.000741826
Current time:  2018-02-05 10:41:45.204431933 +0800 CST m=+9.001392277
Current time:  2018-02-05 10:41:46.204367246 +0800 CST m=+10.001327590
Done!