time

最后更新于:2022-04-02 02:46:30

[TOC] ## 语法 ### Func ``` func After(d Duration) <-chan Time func Sleep(d Duration) func Tick(d Duration) <-chan Time ``` ### Type ``` type Duration // 字符串转时间 如 "2s" func ParseDuration(s string) (Duration, error) // Since返回从t到现在经过的时间,等价于time.Now().Sub(t) func Since(t Time) Duration func Until(t Time) Duration func (d Duration) Hours() float64 func (d Duration) Microseconds() int64 func (d Duration) Milliseconds() int64 func (d Duration) Minutes() float64 func (d Duration) Nanoseconds() int64 func (d Duration) Seconds() float64 func (d Duration) String() string func (d Duration) Round(m Duration) Duration // 取整 func (d Duration) Truncate(m Duration) Duration type Location func FixedZone(name string, offset int) *Location func LoadLocation(name string) (*Location, error) func LoadLocationFromTZData(name string, data []byte) (*Location, error) func (l *Location) String() string type Ticker func NewTicker(d Duration) *Ticker func (t *Ticker) Reset(d Duration) func (t *Ticker) Stop() type Time func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time func Now() Time func Parse(layout, value string) (Time, error) func ParseInLocation(layout, value string, loc *Location) (Time, error) func Unix(sec int64, nsec int64) Time func (t Time) Add(d Duration) Time func (t Time) AddDate(years int, months int, days int) Time func (t Time) AppendFormat(b []byte, layout string) []byte func (t Time) Sub(u Time) Duration func (t Time) After(u Time) bool func (t Time) Before(u Time) bool func (t Time) Clock() (hour, min, sec int) func (t Time) Date() (year int, month Month, day int) func (t Time) Year() int func (t Time) YearDay() int func (t Time) Minute() int func (t Time) Month() Month func (t Time) Day() int func (t Time) Hour() int func (t Time) Second() int func (t Time) Nanosecond() int func (t Time) Unix() int64 func (t Time) UnixNano() int64 func (t Time) Weekday() Weekday func (t Time) String() string func (t Time) Format(layout string) string func (t *Time) GobDecode(data []byte) error //对时间进行编解码 func (t Time) GobEncode() ([]byte, error) func (t Time) MarshalBinary() ([]byte, error) func (t Time) MarshalJSON() ([]byte, error) func (t Time) MarshalText() ([]byte, error) func (t Time) In(loc *Location) Time func (t Time) IsZero() bool func (t Time) ISOWeek() (year, week int) func (t Time) Zone() (name string, offset int) func (t Time) Local() Time func (t Time) UTC() Time func (t Time) Location() *Location func (t Time) Equal(u Time) bool func (t Time) Round(d Duration) Time // 单位时间取整 func (t Time) Truncate(d Duration) Time func (t *Time) UnmarshalBinary(data []byte) error func (t *Time) UnmarshalJSON(data []byte) error func (t *Time) UnmarshalText(data []byte) error type Timer func AfterFunc(d Duration, f func()) *Timer func NewTimer(d Duration) *Timer func (t *Timer) Reset(d Duration) bool func (t *Timer) Stop() bool type Month func (m Month) String() string type ParseError func (e *ParseError) Error() string type Weekday func (d Weekday) String() string ``` ## 示例 ### ParseDuration 字符串转时间 ``` // "300ms", "-1.5h" or "2h45m". // "ns", "us" (or "µs"), "ms", "s", "m", "h". duration, err := time.ParseDuration("2s") if err != nil { log.Fatal(err) } fmt.Printf("%+v\n", duration.Seconds()) // 2 ``` ### time.Ticker 间隔执行功能 每秒输出 hello word,可用于结构体的定时任务 ``` for { select { case <-time.Tick(time.Second): fmt.Printf("%+v\n", "hell world") } } ``` ### time.Round 时间取整 ``` t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC) round := []time.Duration{ time.Nanosecond, time.Microsecond, time.Millisecond, time.Second, 2 * time.Second, time.Minute, 10 * time.Minute, time.Hour, } for _, d := range round { fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999")) } // output t.Round( 1ns) = 12:15:30.918273645 t.Round( 1us) = 12:15:30.918274 t.Round( 1ms) = 12:15:30.918 t.Round( 1s) = 12:15:31 t.Round( 2s) = 12:15:30 t.Round( 1m0s) = 12:16:00 t.Round( 10m0s) = 12:20:00 t.Round(1h0m0s) = 12:00:00 ``` ### Truncate 时间取整,但不超出该时间 类似Round,但是返回的是最接近但早于t的时间点 ``` t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645") trunc := []time.Duration{ time.Nanosecond, time.Microsecond, time.Millisecond, time.Second, 2 * time.Second, time.Minute, 10 * time.Minute, time.Hour, } for _, d := range trunc { fmt.Printf("t.Truncate(%6s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999")) } // output t.Truncate( 1ns) = 12:15:30.918273645 t.Truncate( 1us) = 12:15:30.918273 t.Truncate( 1ms) = 12:15:30.918 t.Truncate( 1s) = 12:15:30 t.Truncate( 2s) = 12:15:30 t.Truncate( 1m0s) = 12:15:00 t.Truncate( 10m0s) = 12:10:00 t.Truncate(1h0m0s) = 12:00:00 ```
';