timeutil

package
v0.31.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: Unlicense Imports: 7 Imported by: 14

Documentation

Overview

Package timeutil contains types and utilities for working with time and duration values.

Index

Constants

View Source
const Day time.Duration = 24 * time.Hour

Day is the duration of one day.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock added in v0.31.1

type Clock interface {
	// Now returns the current time in accordance with the clock.
	Now() (now time.Time)
}

Clock is an interface for time-related operations.

type ClockAfter added in v0.31.1

type ClockAfter interface {
	Clock

	// After returns a channel on which the current time is sent after d has
	// passed.
	After(d time.Duration) (c <-chan time.Time)
}

ClockAfter is an extension of the Clock interface for clocks that can create timers.

type ConstSchedule added in v0.31.1

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

ConstSchedule is a Schedule for tasks that run with a constant interval between the runs.

func NewConstSchedule added in v0.31.1

func NewConstSchedule(ivl time.Duration) (s *ConstSchedule)

NewConstSchedule returns a new schedule that runs with a constant interval. ivl must be positive.

func (*ConstSchedule) UntilNext added in v0.31.1

func (s *ConstSchedule) UntilNext(_ time.Time) (d time.Duration)

UntilNext implements the Schedule interface for *ConstSchedule. It always returns the original interval used during its construction.

type CronSchedule added in v0.31.1

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

CronSchedule is an adapter for the cron module.

func NewCronSchedule added in v0.31.1

func NewCronSchedule(cs cron.Schedule) (s *CronSchedule)

NewCronSchedule returns a new cron schedule adapted to the Schedule interface. cs must not be nil.

func (*CronSchedule) UntilNext added in v0.31.1

func (s *CronSchedule) UntilNext(now time.Time) (d time.Duration)

UntilNext implements the Schedule interface for *CronSchedule.

type Duration

type Duration time.Duration

Duration is a helper type for time.Duration providing functionality for encoding.

func (Duration) MarshalText

func (d Duration) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface for Duration.

func (Duration) String

func (d Duration) String() (str string)

String implements the fmt.Stringer interface for Duration. It wraps time.Duration.String method and additionally cuts off non-leading zero values of minutes and seconds. Some values which are differ between the implementations:

Duration:   "1m", time.Duration:   "1m0s"
Duration:   "1h", time.Duration: "1h0m0s"
Duration: "1h1m", time.Duration: "1h1m0s"

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(b []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface for *Duration.

TODO(e.burkov): Fix parsing larger units like days.

type RandomizedSchedule added in v0.31.1

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

RandomizedSchedule adds a random duration to the result of the Schedule that it wraps.

func NewRandomizedSchedule added in v0.31.1

func NewRandomizedSchedule(
	sched Schedule,
	r *rand.Rand,
	minAdd time.Duration,
	maxAdd time.Duration,
) (s *RandomizedSchedule)

NewRandomizedSchedule returns a new schedule that adds a random value between minAdd and maxAdd to the result of sched. sched and r must not be nil. max must be greater than min.

func (*RandomizedSchedule) UntilNext added in v0.31.1

func (s *RandomizedSchedule) UntilNext(now time.Time) (d time.Duration)

UntilNext implements the Schedule interface for *RandomizedSchedule.

type Schedule added in v0.31.1

type Schedule interface {
	// UntilNext returns the duration left until the next time when a task
	// should be performed.  UntilNext can return different values with the same
	// now value.  d must not be negative.
	UntilNext(now time.Time) (d time.Duration)
}

Schedule is an interface for entities that can decide when a task should be performed based on the current time.

type SystemClock added in v0.31.1

type SystemClock struct{}

SystemClock is a Clock that uses the functions from package time.

func (SystemClock) After added in v0.31.1

func (SystemClock) After(d time.Duration) (c <-chan time.Time)

After implements the ClockAfter interface for SystemClock.

func (SystemClock) Now added in v0.31.1

func (SystemClock) Now() (now time.Time)

Now implements the ClockAfter interface for SystemClock.

Jump to

Keyboard shortcuts

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