timestamp

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Licensed to Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. Apache Software Foundation (ASF) licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// MinNanoTime is the minimum time that can be represented.
	//
	// 1677-09-21 00:12:43.145224192 +0000 UTC
	MinNanoTime = int64(math.MinInt64)

	// MaxNanoTime is the maximum time that can be represented.
	//
	// 2262-04-11 23:47:16.854775807 +0000 UTC
	MaxNanoTime = int64(math.MaxInt64)
)

Variables

View Source
var (
	MaxMilliTime       = time.UnixMilli(maxNanoTime.UnixMilli())
	MaxMilliPbTime     = timestamppb.New(MaxMilliTime)
	DefaultBeginPbTime = timestamppb.New(time.Unix(0, 0))
	DefaultTimeRange   = &modelv1.TimeRange{
		Begin: DefaultBeginPbTime,
		End:   MaxMilliPbTime,
	}

	ErrTimeOutOfRange     = errors.Errorf("time is out of range %d - %d", MinNanoTime, MaxNanoTime)
	ErrTimeNotMillisecond = errors.Errorf("time is not millisecond precision")
	ErrTimeEmpty          = errors.Errorf("time is empty")
)

Functions

func Check

func Check(t time.Time) error

Check checks that a time is valid

func CheckPb

func CheckPb(t *timestamppb.Timestamp) error

CheckPb checks that a protobuf timestamp is valid

func CheckTimeRange

func CheckTimeRange(timeRange *modelv1.TimeRange) error

CheckTimeRange checks that a protobuf time range is valid

func MToN

func MToN(ms time.Time) time.Time

MToN convert time unix millisends to nanoseconds

func NowMilli

func NowMilli() time.Time

NowMilli returns a time based on a unix millisecond

func ParseDuration

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

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d".

func SetClock

func SetClock(ctx context.Context, clock Clock) context.Context

SetContext returns a sub context with the passed Clock

Types

type Clock

type Clock interface {
	clock.Clock
}

Clock represents an interface contains all functions in the standard library time.

func GetClock

func GetClock(ctx context.Context) (Clock, context.Context)

GetClock returns a Clock from the context. If the context doesn't contains a Clock, a real-time one will be created and returned with a child context which contains the new one.

func NewClock

func NewClock() Clock

NewClock returns an instance of a real-time clock.

type MockClock

type MockClock interface {
	clock.Clock
	// Add moves the current time of the mock clock forward by the specified duration.
	Add(d time.Duration)
	// Set sets the current time of the mock clock to a specific one.
	Set(t time.Time)
	// TriggerTimer sends the current time to timer.C
	TriggerTimer() bool
}

func NewMockClock

func NewMockClock() MockClock

NewMockClock returns an instance of a mock clock.

type TimeRange

type TimeRange struct {
	Start        time.Time
	End          time.Time
	IncludeStart bool
	IncludeEnd   bool
}

func NewInclusiveTimeRange

func NewInclusiveTimeRange(start, end time.Time) TimeRange

func NewInclusiveTimeRangeDuration

func NewInclusiveTimeRangeDuration(start time.Time, duration time.Duration) TimeRange

func NewSectionTimeRange added in v0.2.0

func NewSectionTimeRange(start, end time.Time) TimeRange

func NewTimeRange

func NewTimeRange(start, end time.Time, includeStart, includeEnd bool) TimeRange

func NewTimeRangeDuration

func NewTimeRangeDuration(start time.Time, duration time.Duration, includeStart, includeEnd bool) TimeRange

func (TimeRange) Contains

func (t TimeRange) Contains(unixNano uint64) bool

func (TimeRange) Duration

func (t TimeRange) Duration() time.Duration

func (TimeRange) Overlapping

func (t TimeRange) Overlapping(other TimeRange) bool

func (TimeRange) String

func (t TimeRange) String() string

Jump to

Keyboard shortcuts

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