time

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: Apache-2.0 Imports: 8 Imported by: 101

Documentation

Overview

Package time implement facilities for working with time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ceil

func Ceil(t time.Time, d time.Duration) time.Time

Ceil returns the result of rounding t up to a multiple of d since the zero time.

func DurationFromUnit

func DurationFromUnit(u Unit) (time.Duration, error)

DurationFromUnit creates a time duration from a time unit.

func FromNanoseconds

func FromNanoseconds(nsecs int64) time.Time

FromNanoseconds converts nanoseconds to a time.

func FromNormalizedDuration

func FromNormalizedDuration(nd int64, u time.Duration) time.Duration

FromNormalizedDuration returns the duration given the normalized time duration and a time unit.

func FromUnixMillis

func FromUnixMillis(ms int64) time.Time

FromUnixMillis converts milliseconds since Unix epoch to a time

func MaxTime

func MaxTime(t1, t2 time.Time) time.Time

MaxTime returns the later one of t1 and t2.

func MinTime

func MinTime(t1, t2 time.Time) time.Time

MinTime returns the earlier one of t1 and t2.

func ParseExtendedDuration

func ParseExtendedDuration(s string) (time.Duration, error)

ParseExtendedDuration parses a duration, with the ability to specify time units in days, weeks, months, and years.

func Since added in v1.4.0

func Since(t UnixNano) time.Duration

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

func ToExtendedString

func ToExtendedString(d time.Duration) string

ToExtendedString converts a duration to an extended string.

func ToNanoseconds

func ToNanoseconds(t time.Time) int64

ToNanoseconds converts a time to nanoseconds.

func ToNormalizedDuration

func ToNormalizedDuration(d time.Duration, u time.Duration) int64

ToNormalizedDuration returns the normalized units of duration given a time unit.

func ToNormalizedTime

func ToNormalizedTime(t time.Time, u time.Duration) int64

ToNormalizedTime returns the normalized units of time given a time unit.

func ToUnixMillis

func ToUnixMillis(t time.Time) int64

ToUnixMillis converts a time to milliseconds since Unix epoch

func UnitCount added in v0.15.0

func UnitCount() int

UnitCount returns the total number of unit types.

Types

type Matcher

type Matcher interface {
	gomock.Matcher
}

Matcher is a gomock.Matcher that matches time.Time

func NewMatcher

func NewMatcher(t time.Time) Matcher

NewMatcher returns a new Matcher

type Range

type Range struct {
	Start UnixNano
	End   UnixNano
}

Range represents [start, end)

func (Range) After

func (r Range) After(other Range) bool

After determines whether r is after other.

func (Range) Before

func (r Range) Before(other Range) bool

Before determines whether r is before other.

func (Range) Contains

func (r Range) Contains(other Range) bool

Contains determines whether r contains other.

func (Range) Duration

func (r Range) Duration() time.Duration

Duration returns the duration of the range.

func (Range) Equal

func (r Range) Equal(other Range) bool

Equal returns whether two time ranges are equal.

func (Range) Intersect

func (r Range) Intersect(other Range) (Range, bool)

Intersect calculates the intersection of the receiver range against the provided argument range iff there is an overlap between the two. It also returns a bool indicating if there was a valid intersection.

func (Range) IsEmpty

func (r Range) IsEmpty() bool

IsEmpty returns whether the time range is empty.

func (Range) IterateBackward added in v0.11.0

func (r Range) IterateBackward(stepSize time.Duration, f func(t UnixNano) (shouldContinue bool))

IterateBackward iterates through a time range by step size in the backwards direction.

func (Range) IterateForward added in v0.11.0

func (r Range) IterateForward(stepSize time.Duration, f func(t UnixNano) (shouldContinue bool))

IterateForward iterates through a time range by step size in the forwards direction.

func (Range) Merge

func (r Range) Merge(other Range) Range

Merge merges the two ranges if they overlap. Otherwise, the gap between them is included.

func (Range) Overlaps

func (r Range) Overlaps(other Range) bool

Overlaps determines whether r overlaps with other.

func (Range) Since

func (r Range) Since(t UnixNano) Range

Since returns the time range since a given point in time.

func (Range) String

func (r Range) String() string

String returns the string representation of the range.

func (Range) Subtract

func (r Range) Subtract(other Range) []Range

Subtract removes the intersection between r and other from r, possibly splitting r into two smaller ranges.

type RangeIter

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

RangeIter iterates over a collection of time ranges.

func (*RangeIter) Next

func (it *RangeIter) Next() bool

Next moves to the next item.

func (*RangeIter) Value

func (it *RangeIter) Value() Range

Value returns the current time range.

type Ranges

type Ranges interface {
	AddRange(Range)
	AddRanges(Ranges)
	RemoveRange(Range)
	RemoveRanges(Ranges)
	Overlaps(Range) bool
	Iter() *RangeIter
	Clone() Ranges
	Len() int
	IsEmpty() bool
	String() string
}

Ranges is a collection of time ranges.

func NewRanges

func NewRanges(in ...Range) Ranges

NewRanges constructs a new Ranges object comprising the provided ranges.

type Unit

type Unit uint16

Unit represents a time unit.

const (
	// None is a place holder for time units, it doesn't represent an actual time unit. The ordering
	// here is used for comparisons betweens units and should not be changed.
	None Unit = iota
	Second
	Millisecond
	Microsecond
	Nanosecond
	Minute
	Hour
	Day
	Year
)

Different time units that are supported.

func MaxUnitForDuration

func MaxUnitForDuration(d time.Duration) (int64, Unit)

MaxUnitForDuration determines the maximum unit for which the input duration is a multiple of.

func UnitFromDuration

func UnitFromDuration(d time.Duration) (Unit, error)

UnitFromDuration creates a time unit from a time duration.

func (Unit) Count

func (tu Unit) Count(d time.Duration) (int, error)

Count returns the number of units contained within the duration.

func (Unit) IsValid

func (tu Unit) IsValid() bool

IsValid returns whether the given time unit is valid / supported.

func (Unit) MustCount

func (tu Unit) MustCount(d time.Duration) int

MustCount is like Count but panics if d is negative or if tu is not a valid Unit.

func (Unit) String

func (tu Unit) String() string

String returns the string representation for the time unit

func (Unit) Validate added in v0.15.0

func (tu Unit) Validate() error

Validate will validate the time unit.

func (Unit) Value

func (tu Unit) Value() (time.Duration, error)

Value is the time duration of the time unit.

type UnixNano

type UnixNano int64

UnixNano is used to indicate that an int64 stores a unix timestamp at nanosecond resolution.

func FromNormalizedTime

func FromNormalizedTime(nt int64, u time.Duration) UnixNano

FromNormalizedTime returns the time given the normalized time units and the time unit.

func FromSeconds added in v1.2.0

func FromSeconds(seconds int64) UnixNano

FromSeconds returns the UnixNano representation of the local Time corresponding to the seconds since January 1, 1970 UTC.

func FromSecondsAndNanos added in v1.2.0

func FromSecondsAndNanos(seconds int64, nanos int64) UnixNano

FromSecondsAndNanos returns the UnixNano representation of the local Time corresponding to the seconds and nanoseconds since January 1, 1970 UTC.

func MaxUnixNano added in v1.2.0

func MaxUnixNano(t1, t2 UnixNano) UnixNano

MaxUnixNano returns the later one of t1 and t2.

func MinUnixNano added in v1.2.0

func MinUnixNano(t1, t2 UnixNano) UnixNano

MinUnixNano returns the earlier one of t1 and t2.

func Now added in v1.2.0

func Now() UnixNano

Now returns the current local time, in nanoseconds elapsed since January 1, 1970 UTC. NB: This is independent of location.

func ToUnixNano

func ToUnixNano(t time.Time) UnixNano

ToUnixNano returns a UnixNano from a time.Time.

func (UnixNano) Add added in v1.2.0

func (u UnixNano) Add(d time.Duration) UnixNano

Add returns the time u+d.

func (UnixNano) After

func (u UnixNano) After(t UnixNano) bool

After reports whether the time instant u is after t.

func (UnixNano) Before

func (u UnixNano) Before(t UnixNano) bool

Before reports whether the time instant u is before t.

func (UnixNano) Equal

func (u UnixNano) Equal(t UnixNano) bool

Equal reports whether the time instant u is equal to t.

func (UnixNano) Format added in v1.2.0

func (u UnixNano) Format(blockTimeFormat string) string

Format returns the string representation for the time with the given format.

func (UnixNano) FromNormalizedTime added in v1.2.0

func (u UnixNano) FromNormalizedTime(d time.Duration) UnixNano

FromNormalizedTime returns the time given the normalized time units and the time unit.

func (UnixNano) IsZero added in v1.2.0

func (u UnixNano) IsZero() bool

IsZero reports whether the time instant u is 0.

func (UnixNano) Seconds added in v1.2.0

func (u UnixNano) Seconds() int64

Seconds returns the seconds for time u, as an int64.

func (UnixNano) String added in v1.2.0

func (u UnixNano) String() string

String returns the time formatted using the format string

"2006-01-02 15:04:05.999999999 -0700 MST"

func (UnixNano) Sub added in v1.2.0

func (u UnixNano) Sub(o UnixNano) time.Duration

Sub returns the duration u-o. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned.

func (UnixNano) ToNormalizedTime added in v1.2.0

func (u UnixNano) ToNormalizedTime(d time.Duration) int64

ToNormalizedTime returns the normalized units of time given a time unit.

func (UnixNano) ToTime

func (u UnixNano) ToTime() time.Time

ToTime returns a time.ToTime from a UnixNano.

func (UnixNano) Truncate added in v1.2.0

func (u UnixNano) Truncate(d time.Duration) UnixNano

Truncate returns the result of rounding u down to a multiple of d. If d <= 1, Truncate returns u unchanged.

Jump to

Keyboard shortcuts

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