timeutil

package
v1.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const ClocklessMaxOffset = math.MaxInt64

ClocklessMaxOffset is a special-cased value that is used when the cluster runs in "clockless" mode. In that (experimental) mode, we operate without assuming any bound on the clock drift.

Variables

View Source
var UnixEpoch = time.Unix(0, 0).UTC()

UnixEpoch represents the Unix epoch, January 1, 1970 UTC.

Functions

func FromUnixMicros added in v1.1.0

func FromUnixMicros(us int64) time.Time

FromUnixMicros returns the UTC time.Time corresponding to the given Unix time, usec microseconds since UnixEpoch. In Go's current time.Time implementation, all possible values for us can be represented as a time.Time.

func LoadLocation

func LoadLocation(name string) (*time.Location, error)

LoadLocation returns the time.Location with the given name. The name is taken to be a location name corresponding to a file in the IANA Time Zone database, such as "America/New_York".

We do not use Go's time.LoadLocation() directly because: 1) it maps "Local" to the local time zone, whereas we want UTC. 2) when a tz is not found, it reports some garbage message related to zoneinfo.zip, which we don't ship, instead of a more useful message like "the tz file with such name is not present in one of the standard tz locations".

func Now

func Now() time.Time

Now returns the current UTC time.

func Since

func Since(t time.Time) time.Duration

Since returns the time elapsed since t. It is shorthand for Now().Sub(t).

func ToUnixMicros added in v1.1.0

func ToUnixMicros(t time.Time) int64

ToUnixMicros returns t as the number of microseconds elapsed since UnixEpoch. Fractional microseconds are rounded, half up, using time.Round. Similar to time.Time.UnixNano, the result is undefined if the Unix time in microseconds cannot be represented by an int64.

func Unix added in v1.1.0

func Unix(sec, nsec int64) time.Time

Unix wraps time.Unix ensuring that the result is in UTC instead of Local.

Types

type Timer

type Timer struct {

	// C is a local "copy" of timer.C that can be used in a select case before
	// the timer has been initialized (via Reset).
	C    <-chan time.Time
	Read bool
	// contains filtered or unexported fields
}

The Timer type represents a single event. When the Timer expires, the current time will be sent on Timer.C.

This timer implementation is an abstraction around the standard library's time.Timer that provides a temporary workaround for the issue described in https://github.com/golang/go/issues/14038. As such, this timer should only be used when Reset is planned to be called continually in a loop. For this Reset pattern to work, Timer.Read must be set to true whenever a timestamp is read from the Timer.C channel. If Timer.Read is not set to true when the channel is read from, the next call to Timer.Reset will deadlock. This pattern looks something like:

var timer timeutil.Timer
defer timer.Stop()
for {
    timer.Reset(wait)
    switch {
    case <-timer.C:
        timer.Read = true
        ...
    }
}

Note that unlike the standard library's Timer type, this Timer will not begin counting down until Reset is called for the first time, as there is no constructor function.

func NewTimer

func NewTimer() *Timer

NewTimer allocates a new timer.

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration)

Reset changes the timer to expire after duration d and returns the new value of the timer. This method includes the fix proposed in https://github.com/golang/go/issues/11513#issuecomment-157062583, but requires users of Timer to set Timer.Read to true whenever they successfully read from the Timer's channel.

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired, been stopped previously, or had never been initialized with a call to Timer.Reset. Stop does not close the channel, to prevent a read from succeeding incorrectly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL