diff --git a/core/sysx/host.go b/core/sysx/host.go index 8cfb54a1..fd221218 100644 --- a/core/sysx/host.go +++ b/core/sysx/host.go @@ -16,6 +16,7 @@ func init() { } } +// Hostname returns the name of the host, if no hostname, a random id is returned. func Hostname() string { return hostname } diff --git a/core/timex/relativetime.go b/core/timex/relativetime.go index b11aeb4b..25a31d97 100644 --- a/core/timex/relativetime.go +++ b/core/timex/relativetime.go @@ -5,14 +5,18 @@ import "time" // Use the long enough past time as start time, in case timex.Now() - lastTime equals 0. var initTime = time.Now().AddDate(-1, -1, -1) +// Now returns a relative time duration since initTime, which is not important. +// The caller only needs to care about the relative value. func Now() time.Duration { return time.Since(initTime) } +// Since returns a diff since given d. func Since(d time.Duration) time.Duration { return time.Since(initTime) - d } +// Time returns current time, the same as time.Now(). func Time() time.Time { return initTime.Add(Now()) } diff --git a/core/timex/repr.go b/core/timex/repr.go index 519423ed..856eb966 100644 --- a/core/timex/repr.go +++ b/core/timex/repr.go @@ -5,6 +5,7 @@ import ( "time" ) +// ReprOfDuration returns the string representation of given duration in ms. func ReprOfDuration(duration time.Duration) string { return fmt.Sprintf("%.1fms", float32(duration)/float32(time.Millisecond)) } diff --git a/core/timex/ticker.go b/core/timex/ticker.go index 1d86d9dc..cdd86d86 100644 --- a/core/timex/ticker.go +++ b/core/timex/ticker.go @@ -8,11 +8,13 @@ import ( ) type ( + // Ticker interface wraps the Chan and Stop methods. Ticker interface { Chan() <-chan time.Time Stop() } + // FakeTicker interface is used for unit testing. FakeTicker interface { Ticker Done() @@ -30,6 +32,7 @@ type ( } ) +// NewTicker returns a Ticker. func NewTicker(d time.Duration) Ticker { return &realTicker{ Ticker: time.NewTicker(d), @@ -40,6 +43,7 @@ func (rt *realTicker) Chan() <-chan time.Time { return rt.C } +// NewFakeTicker returns a FakeTicker. func NewFakeTicker() FakeTicker { return &fakeTicker{ c: make(chan time.Time, 1),