go时间、定时器与休眠函数(time包)

time包提供了时间显示和测量相关的函数。包括时间处理与定时器处理(Timer、Ticker、Sleep等)


格式化

go语音中时间格式化非常特别,可简记为‘123456’(月日时分秒年:"2006-01-02 15:04:05.999999999 -0700 MST",15表示24小时制,3表示12小时制(会带AM或PM))

如:time.Now().Format("2006-01-02 15:04:05")

预定义格式:

const (

    ANSIC = "Mon Jan _2 15:04:05 2006"

    UnixDate = "Mon Jan _2 15:04:05 MST 2006"

    RubyDat = "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"

)


时间操作

  • 获取时间

    • Now() // 当前本地时间

    • Unix(sec int64, nsec int64) // 根据时间戳返回本地时间

    • Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) // 返回指定时间

  • 转换与格式化

    • UTC() // 获取指定时间在UTC 时区的时间表示

    • Local() // 以本地时区表示

    • In(loc *Location) // 时间在指定时区的表示

    • Format(layout string) // 按指定格式显示时间

  • 比较与计算

    • IsZero() bool {} // 是否是零时时间

    • After(u Time) bool {} // 时间在u之前

    • Before(u Time) bool {} // 时间在u之后

    • Equal(u Time) bool {} // 时间与u相同

    • Add(d Duration) Time {} // 返回t +d 的时间点

    • Sub(u Time) Duration {} // 返回 t-u

    • AddDate(years int, months int, days int) Time {} // 返回增加后的时间点


定时器

定时器(Ticker会重复,Timer只执行一次;不需要时都可通过Stop来停止,但若返回的是chan则就无法停止)

  • timer

    • AfterFunc(d Duration, f func()) *Timer:过指定时间后执行f;

    • NewTimer(d Duration) *Timer:可通过reset 重新开始;

    • After(d Duration) <-chan Time:指定时间后触发(若放在select中,则每次重新select都重新计时);

  • ticker

    • NewTicker(d Duration) *Ticker:返回一个只读的channel,定时触发一次,不用时调用Stop停止;

    • Tick(d Duration) <-chan Time:对NewTicker的封装(直接返回了NewTicker.C,可直接读取,但无法停止定时器了);

    type Ticker struct {

     C     <-chan Time //周期性传递时间信息的通道

     r     runtimeTimer // 定时器相关信息(开始时间、周期、回调、参数等)

    }

    ticker := time.NewTicker(5 * time.Second)

    // for _ = range ticker.C {

    //     fmt.Println(time.Now())

    // }

   for {

    select{

        case <- ticker.C: ...

        case <- time.After(10*time.Second):  ...    #永远没机会执行(每5秒会重新开始)

      }

    }

  • Sleep(d Duration):休眠指定时间


时间相关类型

时间处理相关的主要数据类型有:time.Time, time.Month, time.Weekday, time.Duration, time.Location等。

  • time.Time精确到纳秒的时间点

type Time struct {

    sec int64 // 从1年1月1日 00:00:00 UTC 至今过去的秒数

    nsec int32 // 最近一秒到下一秒过去的纳秒数

    loc *Location // 时区

}

  • time.Month一年中的某个月

type Month int

const (

    January Month = 1 + iota

    February

    March

    April

    May

    June

    July

    August

    September

    October

    November

    December

)

  • time.Weekday一周的周几

type Weekday int

const (

    Sunday Weekday = iota

    Monday

    Tuesday

    Wednesday

    Thursday

    Friday

    Saturday

)

  • time.Duration 两个时间点之间经过的纳秒数

可表示的最长时间段约为290年

type Duration int64

const (

    Nanosecond  Duration = 1

    Microsecond = 1000 * Nanosecond

    Millisecond = 1000 * Microsecond

    Second  = 1000 * Millisecond

    Minute = 60 * Second

    Hour = 60 * Minute

)

  • Location一个地点及所在的时区信息

北京时间可以使用 Asia/Shanghai

type Location struct {

    name string

    zone []zone

    tx   []zoneTrans

    cacheStart int64

    cacheEnd   int64

    cacheZone  *zone

}

猜你喜欢

转载自blog.csdn.net/alwaysrun/article/details/82919138