cockroach: github.com/cockroachdb/cockroach/pkg/util/duration Index | Files

package duration

import "github.com/cockroachdb/cockroach/pkg/util/duration"

Index

Package Files

duration.go

func Add Uses

func Add(ctx Context, t time.Time, d Duration) time.Time

Add returns the time t+d, using a configurable mode.

func AddMicros Uses

func AddMicros(t time.Time, d int64) time.Time

AddMicros adds the microsecond delta to the provided time value. The reason this function is necessary even though time.Add(duration) exists is that time.Duration can only hold values up to ~290 years, because it stores duration at the nanosecond resolution. This function makes it possible to add more than 290 years to a time.Time, at the tradeoff of working on a microsecond resolution.

func DiffMicros Uses

func DiffMicros(t1, t2 time.Time) int64

DiffMicros computes the microsecond difference between two time values. The reason this function is necessary even though time.Sub(time) exists is that time.Duration can only hold values up to ~290 years, because it stores duration at the nanosecond resolution. This function should be used if a difference of more than 290 years is possible between time values, and a microsecond resolution is acceptable.

func Truncate Uses

func Truncate(d time.Duration, r time.Duration) time.Duration

Truncate returns a new duration obtained from the first argument by discarding the portions at finer resolution than that given by the second argument. Example: Truncate(time.Second+1, time.Second) == time.Second.

type AdditionMode Uses

type AdditionMode bool

AdditionMode controls date normalization behaviors in Add().

const (
    // AdditionModeCompatible applies a date-normalization strategy
    // which produces results which are more compatible with
    // PostgreSQL in which adding a month "rounds down" to the last
    // day of a month instead of producing a value in the following
    // month.
    //
    // See PostgreSQL 10.5: src/backend/utils/adt/timestamp.c timestamp_pl_interval().
    AdditionModeCompatible AdditionMode = false
    // AdditionModeLegacy delegates to time.Time.AddDate() for
    // performing Time+Duration math.
    AdditionModeLegacy AdditionMode = true
)

func (AdditionMode) GetAdditionMode Uses

func (m AdditionMode) GetAdditionMode() AdditionMode

GetAdditionMode allows an AdditionMode to be used as its own context.

func (AdditionMode) String Uses

func (m AdditionMode) String() string

type Context Uses

type Context interface {
    GetAdditionMode() AdditionMode
}

Context is used to prevent a package-dependency cycle via tree.EvalContext.

type Duration Uses

type Duration struct {
    Months int64
    Days   int64
    // contains filtered or unexported fields
}

A Duration represents a length of time.

A duration of "1 month" cannot be represented as a fixed number of nanoseconds because the length of months vary. The same is true for days because of leap seconds. Given a begin or end time to anchor a duration, the nanosecond count can be calculated, but it's useful to represent durations such as "1 year 3 months" without an anchor. Duration allows this.

For the purposes of Compare and Encode, 1 month is considered equivalent to 30 days and 1 day is equivalent to 24 * 60 * 60 * 1E9 nanoseconds.

Although the Nanos field is a number of nanoseconds, all operations round to the nearest microsecond. Any setting of this field should avoid setting with precision below microseconds. The only exceptions are the encode/decode operations.

TODO(dan): Until the overflow and underflow handling is fixed, this is only useful for durations of < 292 years.

func Decode Uses

func Decode(sortNanos int64, months int64, days int64) (Duration, error)

Decode reverses the three integers returned from Encode and produces an equal Duration to the original.

func DecodeDuration Uses

func DecodeDuration(months, days, nanos int64) Duration

DecodeDuration returns a Duration without rounding nanos.

func FromBigInt Uses

func FromBigInt(src *big.Int) (Duration, bool)

FromBigInt converts a big.Int number of nanoseconds to a duration. Inverse conversion of AsBigInt. Boolean false if the result overflows.

func FromFloat64 Uses

func FromFloat64(x float64) Duration

FromFloat64 converts a float64 number of seconds to a duration. Inverse conversion of AsFloat64.

func FromInt64 Uses

func FromInt64(x int64) Duration

FromInt64 converts an int64 number of seconds to a duration. Inverse conversion of AsInt64.

func MakeDuration Uses

func MakeDuration(nanos, days, months int64) Duration

MakeDuration returns a Duration rounded to the nearest microsecond.

func (Duration) Add Uses

func (d Duration) Add(x Duration) Duration

Add returns a Duration representing a time length of d+x.

func (Duration) AsBigInt Uses

func (d Duration) AsBigInt(dst *big.Int)

AsBigInt converts a duration to a big.Int with the number of nanoseconds.

func (Duration) AsFloat64 Uses

func (d Duration) AsFloat64() float64

AsFloat64 converts a duration to a float64 number of seconds.

func (Duration) AsInt64 Uses

func (d Duration) AsInt64() (int64, bool)

AsInt64 converts a duration to an int64 number of seconds. The conversion may overflow, in which case the boolean return value is false.

func (Duration) Compare Uses

func (d Duration) Compare(x Duration) int

Compare returns an integer representing the relative length of two Durations. The result will be 0 if d==x, -1 if d < x, and +1 if d > x.

func (Duration) Div Uses

func (d Duration) Div(x int64) Duration

Div returns a Duration representing a time length of d/x.

func (Duration) DivFloat Uses

func (d Duration) DivFloat(x float64) Duration

DivFloat returns a Duration representing a time length of d/x.

func (Duration) Encode Uses

func (d Duration) Encode() (sortNanos int64, months int64, days int64, err error)

Encode returns three integers such that the original Duration is recoverable (using Decode) and the first int will approximately sort a collection of encoded Durations.

func (Duration) EncodeBigInt Uses

func (d Duration) EncodeBigInt() (sortNanos *big.Int, months int64, days int64)

EncodeBigInt is the same as Encode, except that it always returns successfully and is slower.

func (Duration) Format Uses

func (d Duration) Format(buf *bytes.Buffer)

Format emits a string representation of a Duration to a Buffer truncated to microseconds.

func (Duration) Mul Uses

func (d Duration) Mul(x int64) Duration

Mul returns a Duration representing a time length of d*x.

func (Duration) MulFloat Uses

func (d Duration) MulFloat(x float64) Duration

MulFloat returns a Duration representing a time length of d*x.

func (Duration) Nanos Uses

func (d Duration) Nanos() int64

Nanos returns the nanos of d.

func (*Duration) SetNanos Uses

func (d *Duration) SetNanos(nanos int64)

SetNanos rounds and sets nanos.

func (Duration) String Uses

func (d Duration) String() string

String returns a string representation of a Duration.

func (Duration) StringNanos Uses

func (d Duration) StringNanos() string

StringNanos returns a string representation of a Duration including its hidden nanoseconds value. To be used only by the encoding/decoding packages for pretty printing of on-disk values.

func (Duration) Sub Uses

func (d Duration) Sub(x Duration) Duration

Sub returns a Duration representing a time length of d-x.

Package duration imports 10 packages (graph) and is imported by 42 packages. Updated 2019-06-30. Refresh now. Tools for package owners.