time

package
Version: v0.0.0-...-c8a214a Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package time provides utility functions for go time.Time instances.

Index

Constants

View Source
const (
	// TurbineFormat is the canonical format we want to display
	// timestamps in. All timestamps within Turbine code should be
	// UTC.
	TurbineFormat = "2006-01-02 15:04:05.000"

	// CookieFormat is the cannonical format for representing a time as
	// a cookie as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date
	CookieFormat = "Mon, 2 Jan 2006 3:04:05 MST"
)

Variables

This section is empty.

Functions

func Equal

func Equal(t, o *time.Time) bool

Equal compares pointers to two times for equality. If both pointers are nil or if they both point to a time that is equivalent then they are equal.

func Format

func Format(t *time.Time) string

Format uses the canonical Turbine time format and converts the *time.Time to a string using it. Returns empty string if t is nil. If t is not nil we also will force the timezone to be UTC before rendering to string.

func FromUnixMicro

func FromUnixMicro(micros int64) time.Time

FromUnixMicro returns a time.Time from the given microseconds from "January 1, 1970 UTC". The timezone of the returns Time is UTC.

func FromUnixMilli

func FromUnixMilli(millis int64) time.Time

FromUnixMilli returns a time.Time from the given milliseconds from "January 1, 1970 UTC". The timezone of the returns Time is UTC.

func Max

func Max(a time.Time, bs ...time.Time) time.Time

Max returns the latest of several time.Time instances.

func Min

func Min(a time.Time, bs ...time.Time) time.Time

Min returns the earliest of several time.Time instances.

func Parse

func Parse(s string) (*time.Time, error)

Parse takes a timestamp in the Turbine canonical format and returns a pointer to the represented time. An empty string produces a nil pointer; an error parsing the input produces a nil pointer and an error.

func ToUnixMicro

func ToUnixMicro(t time.Time) int64

ToUnixMicro returns a Unix timestamp in microseconds from "January 1, 1970 UTC". The result is undefined if the Unix time cannot be represented by an int64. For example, calling ToUnixMicro on a zero Time is undefined.

See Go stdlib https://golang.org/pkg/time/#Time.UnixNano for more information.

func ToUnixMilli

func ToUnixMilli(t time.Time) int64

ToUnixMilli returns a Unix timestamp in milliseconds from "January 1, 1970 UTC". The result is undefined if the Unix time cannot be represented by an int64. For example, calling ToUnixMilli on a zero Time is undefined.

This utility is useful for service API's such as AWS CloudWatch Logs which require their unix time values to be in milliseconds.

See Go stdlib https://golang.org/pkg/time/#Time.UnixNano for more information.

func TruncUnixMicro

func TruncUnixMicro(t time.Time) time.Time

TruncUnixMicro truncates the given time to microsecond resolution.

func TruncUnixMilli

func TruncUnixMilli(t time.Time) time.Time

TruncUnixMilli truncates the given time to millisecond resolution.

func WithCurrentTimeFrozen

func WithCurrentTimeFrozen(f func(ControlledSource))

WithCurrentTimeFrozen Creates a new ControlledSource with the current time and passes it to the given function for testing.

func WithTimeAt

func WithTimeAt(t time.Time, f func(ControlledSource))

WithTimeAt creates a new ControlledSource with the given time and passes it to the given function for testing.

Types

type ControlledSource

type ControlledSource interface {
	Source

	// Set sets the current time returned by this Source. Timers and context.Contexts
	// created by this Source are triggered/canceled if the new time exceeds their
	// deadline.
	Set(time.Time)

	// Set advances the current time returned by this Source.  Timers and
	// context.Contexts created by this Source are triggered/canceled if the new time
	// exceeds their deadline.
	Advance(time.Duration)

	// TriggerAllTimers set the current time to the latest deadline of any timers
	// Returns the number of timers that were triggered. If no timers are are live,
	// it returns 0 and the time is not advanced. Any contexts whose deadlines expire
	// based on the updated time are triggered as well.
	TriggerAllTimers() int

	// TriggerNextTimer sets the current time to the earliest deadline of all timers
	// created by this ControlledSource and triggers all timers with that
	// deadline. If no timers are live, it returns false and the time is not
	// advanced. Any contexts whose deadlines expire based on the updated time are
	// triggered as well.
	TriggerNextTimer() bool

	// TriggerNextContext sets the current time to the earliest deadline of all
	// contexts created by this ControlledSource and triggers all contexts with that
	// deadline. If no contexts are live, it returns false and the time is not
	// advanced. Any timers whose deadlines expire based on the updated time are
	// triggered as well.
	TriggerNextContext() bool
}

ControlledSource is a source of time.Time values that returns a fixed time unless modified with the Set or Advance methods. ControlledSource should be used for testing only.

func NewIncrementingControlledSource

func NewIncrementingControlledSource(at time.Time, delta time.Duration) ControlledSource

NewIncrementingControlledSource returns a new ControlledSource that increments the controlled time by some delta every time Now() is called.

type MockTimer

type MockTimer struct {
	// contains filtered or unexported fields
}

MockTimer is a mock of Timer interface

func NewMockTimer

func NewMockTimer(ctrl *gomock.Controller) *MockTimer

NewMockTimer creates a new mock instance

func (*MockTimer) C

func (m *MockTimer) C() <-chan time.Time

C mocks base method

func (*MockTimer) EXPECT

func (m *MockTimer) EXPECT() *MockTimerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockTimer) Reset

func (m *MockTimer) Reset(d time.Duration) bool

Reset mocks base method

func (*MockTimer) Stop

func (m *MockTimer) Stop() bool

Stop mocks base method

type MockTimerMockRecorder

type MockTimerMockRecorder struct {
	// contains filtered or unexported fields
}

MockTimerMockRecorder is the mock recorder for MockTimer

func (*MockTimerMockRecorder) C

C indicates an expected call of C

func (*MockTimerMockRecorder) Reset

func (mr *MockTimerMockRecorder) Reset(d interface{}) *gomock.Call

Reset indicates an expected call of Reset

func (*MockTimerMockRecorder) Stop

func (mr *MockTimerMockRecorder) Stop() *gomock.Call

Stop indicates an expected call of Stop

type Source

type Source interface {
	// Returns the current time, as with time.Time.Now().
	Now() time.Time

	// NewTimer creates a new Timer that will send the current
	// time on its channel after the given duration.
	NewTimer(time.Duration) Timer

	// AfterFunc creates a new Timer that will invoke the given
	// function in its own goroutine after the duration has
	// elapsed.
	AfterFunc(time.Duration, func()) Timer

	// NewContextWithDeadline creates a new context.Context and
	// associated context.CancelFunc with the given deadline.
	NewContextWithDeadline(
		parent context.Context,
		deadline time.Time,
	) (context.Context, context.CancelFunc)

	// NewContextWithTimeout creates a new context.Context and
	// associated context.CancelFunc with the given timeout.
	NewContextWithTimeout(
		parent context.Context,
		timeout time.Duration,
	) (context.Context, context.CancelFunc)
}

Source is a source of time.Time values and Timer instances.

func NewSource

func NewSource() Source

NewSource creates a new Source for normal use. The Source's Now method returns the current time.

type Timer

type Timer interface {
	C() <-chan time.Time
	Reset(d time.Duration) bool
	Stop() bool
}

Timer is a trivial wrapper around time.Timer which allows a timer to be mocked and/or replaced with an implementation that can be triggered deterministically.

func AfterFunc

func AfterFunc(d time.Duration, f func()) Timer

AfterFunc creates a new Timer, wrapping a time.Timer that will invoke the given function after the given duration.

func NewTimer

func NewTimer(d time.Duration) Timer

NewTimer creates a new Timer, wrapping a time.Timer that will expire after the given duration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL