Documentation ¶
Overview ¶
Package time provides utility functions for go time.Time instances.
Index ¶
- Constants
- func Equal(t, o *time.Time) bool
- func Format(t *time.Time) string
- func FromUnixMicro(micros int64) time.Time
- func FromUnixMilli(millis int64) time.Time
- func Max(a time.Time, bs ...time.Time) time.Time
- func Min(a time.Time, bs ...time.Time) time.Time
- func Parse(s string) (*time.Time, error)
- func ToUnixMicro(t time.Time) int64
- func ToUnixMilli(t time.Time) int64
- func TruncUnixMicro(t time.Time) time.Time
- func TruncUnixMilli(t time.Time) time.Time
- func WithCurrentTimeFrozen(f func(ControlledSource))
- func WithTimeAt(t time.Time, f func(ControlledSource))
- type ControlledSource
- type MockTimer
- type MockTimerMockRecorder
- type Source
- type Timer
Constants ¶
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 ¶
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 ¶
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 ¶
FromUnixMicro returns a time.Time from the given microseconds from "January 1, 1970 UTC". The timezone of the returns Time is UTC.
func FromUnixMilli ¶
FromUnixMilli returns a time.Time from the given milliseconds from "January 1, 1970 UTC". The timezone of the returns Time is UTC.
func Parse ¶
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 ¶
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 ¶
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 ¶
TruncUnixMicro truncates the given time to microsecond resolution.
func TruncUnixMilli ¶
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) EXPECT ¶
func (m *MockTimer) EXPECT() *MockTimerMockRecorder
EXPECT returns an object that allows the caller to indicate expected use
type MockTimerMockRecorder ¶
type MockTimerMockRecorder struct {
// contains filtered or unexported fields
}
MockTimerMockRecorder is the mock recorder for MockTimer
func (*MockTimerMockRecorder) C ¶
func (mr *MockTimerMockRecorder) C() *gomock.Call
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.
type Timer ¶
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.