timestamp

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package timestamp implements several component related to time.

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 (
	// ErrSchedulerClosed indicates the scheduler is closed.
	ErrSchedulerClosed = errors.New("the scheduler is closed")

	// ErrTaskDuplicated indicates registered task already exists.
	ErrTaskDuplicated = errors.New("the task is duplicated")
)
View Source
var (

	// DefaultTimeRange for the input time range.
	DefaultTimeRange = &modelv1.TimeRange{
		Begin: defaultBeginPbTime,
		End:   maxMilliPbTime,
	}
)

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

SetClock 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
}

MockClock represents a mock clock that only moves forward programmatically.

func NewMockClock

func NewMockClock() MockClock

NewMockClock returns an instance of a mock clock.

type Scheduler added in v0.3.0

type Scheduler struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Scheduler maintains a registry of tasks and their duty cycle. It also provides a Trigger method to fire a task that is scheduled by a MockClock.

func NewScheduler added in v0.3.0

func NewScheduler(parent *logger.Logger, clock Clock) *Scheduler

NewScheduler returns an instance of Scheduler.

func (*Scheduler) Close added in v0.3.0

func (s *Scheduler) Close()

Close the Scheduler and shut down all registered tasks.

func (*Scheduler) Closed added in v0.3.0

func (s *Scheduler) Closed() bool

Closed returns whether the Scheduler is closed.

func (*Scheduler) Register added in v0.3.0

func (s *Scheduler) Register(name string, options cron.ParseOption, expr string, action SchedulerAction) error

Register adds the given task's SchedulerAction to the Scheduler, and associate the given schedule expression.

func (*Scheduler) Trigger added in v0.3.0

func (s *Scheduler) Trigger(name string) bool

Trigger fire a task that is scheduled by a MockTime. A real clock-based task will ignore this trigger, and return false. If the task's name is unknown, it returns false.

type SchedulerAction added in v0.3.0

type SchedulerAction func(now time.Time, logger *logger.Logger) bool

SchedulerAction is an executable when a trigger is fired now is the trigger time, logger has a context indicating the task's identity.

type TimeRange

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

TimeRange is a range of periods into which data can be written or retrieved.

func NewInclusiveTimeRange

func NewInclusiveTimeRange(start, end time.Time) TimeRange

NewInclusiveTimeRange returns TimeRange includes start and end time.

func NewInclusiveTimeRangeDuration

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

NewInclusiveTimeRangeDuration returns TimeRange includes start and end time. It is created from a start time and the Duration.

func NewSectionTimeRange added in v0.2.0

func NewSectionTimeRange(start, end time.Time) TimeRange

NewSectionTimeRange returns TimeRange includes start time. This function is for creating blocks and segments of tsdb.

func NewTimeRange

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

NewTimeRange returns TimeRange.

func NewTimeRangeDuration

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

NewTimeRangeDuration returns TimeRange from a start time and the Duration.

func (TimeRange) Contains

func (t TimeRange) Contains(unixNano uint64) bool

Contains returns whether the unixNano is in the TimeRange.

func (TimeRange) Duration

func (t TimeRange) Duration() time.Duration

Duration converts TimeRange to time.Duration.

func (TimeRange) Overlapping

func (t TimeRange) Overlapping(other TimeRange) bool

Overlapping returns whether TimeRanges intersect each other.

func (TimeRange) String

func (t TimeRange) String() string

String shows the string representation.

Jump to

Keyboard shortcuts

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