timeutil

package
v1.0.0-beta.222 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const MAX_SAFE_ITERATIONS = 10000

Variables

This section is empty.

Functions

func AsTimed

func AsTimed[T any](fn func(T) time.Time) func(T) Timed[T]

AsTimed returns a function that converts a value of type T to a Timed value.

func Compare

func Compare(a, b time.Time) int

Types

type Boundary

type Boundary string
const (
	// Exclusive means the specified boundary time is excluded from the interval.
	// For example, for the range (2025-01-01T00:00:00Z, 2025-01-02T00:00:00Z),
	// the instant 2025-01-01T00:00:00Z is not considered part of the interval.
	Exclusive Boundary = "exclusive"

	// Inclusive means the specified boundary time is included in the interval.
	// For example, for the range [2025-01-01T00:00:00Z, 2025-01-02T00:00:00Z],
	// the instant 2025-01-01T00:00:00Z is considered part of the interval.
	Inclusive Boundary = "inclusive"
)

func (Boundary) Validate

func (b Boundary) Validate() error

type ClosedPeriod

type ClosedPeriod struct {
	From time.Time `json:"from"`
	To   time.Time `json:"to"`
}

func (ClosedPeriod) Contains

func (p ClosedPeriod) Contains(t time.Time) bool

Inclusive at start, exclusive at end

func (ClosedPeriod) ContainsExclusive

func (p ClosedPeriod) ContainsExclusive(t time.Time) bool

Exclusive at both start and end

func (ClosedPeriod) ContainsInclusive

func (p ClosedPeriod) ContainsInclusive(t time.Time) bool

Inclusive at both start and end

func (ClosedPeriod) Duration

func (p ClosedPeriod) Duration() time.Duration

func (ClosedPeriod) Intersection

func (p ClosedPeriod) Intersection(other ClosedPeriod) *ClosedPeriod

func (ClosedPeriod) Open

func (p ClosedPeriod) Open() OpenPeriod

func (ClosedPeriod) Overlaps

func (p ClosedPeriod) Overlaps(other ClosedPeriod) bool

Returns true if the two periods overlap at any point Returns false if the periods are exactly sequential, e.g.: [1, 2] and [2, 3]

func (ClosedPeriod) OverlapsInclusive

func (p ClosedPeriod) OverlapsInclusive(other ClosedPeriod) bool

Returns true if the two periods overlap at any point Returns true if the periods are exactly sequential, e.g.: [1, 2] and [2, 3]

func (ClosedPeriod) Validate

func (p ClosedPeriod) Validate() error

type OpenPeriod

type OpenPeriod struct {
	From *time.Time `json:"from,omitempty"`
	To   *time.Time `json:"to,omitempty"`
}

func (OpenPeriod) Closed

func (p OpenPeriod) Closed() (ClosedPeriod, error)

func (OpenPeriod) Contains

func (p OpenPeriod) Contains(t time.Time) bool

Inclusive at start, exclusive at end

func (OpenPeriod) ContainsExclusive

func (p OpenPeriod) ContainsExclusive(t time.Time) bool

Exclusive at both start and end

func (OpenPeriod) ContainsInclusive

func (p OpenPeriod) ContainsInclusive(t time.Time) bool

Inclusive at both start and end

func (OpenPeriod) Difference

func (p OpenPeriod) Difference(other OpenPeriod) []OpenPeriod

Difference returns P - Other (times in P not in Other)

func (OpenPeriod) Equals

func (p OpenPeriod) Equals(other OpenPeriod) bool

func (OpenPeriod) Intersection

func (p OpenPeriod) Intersection(other OpenPeriod) *OpenPeriod

func (OpenPeriod) IsSupersetOf

func (p OpenPeriod) IsSupersetOf(other OpenPeriod) bool

IsSupersetOf returns true if p contains other (both ends inclusive)

func (OpenPeriod) Union

func (p OpenPeriod) Union(other OpenPeriod) OpenPeriod

type Period

type Period interface {
	// Inclusive at both start and end
	ContainsInclusive(t time.Time) bool
	// Exclusive at both start and end
	ContainsExclusive(t time.Time) bool
	// Inclusive at start, exclusive at end
	Contains(t time.Time) bool
}

type Recurrence

type Recurrence struct {
	Interval RecurrenceInterval `json:"interval"`
	// Anchor can be an arbitrary anchor time for the recurrence.
	// It does not have to be the last or the next time.
	Anchor time.Time `json:"anchor"`
}

func NewRecurrence

func NewRecurrence(p RecurrenceInterval, anchor time.Time) (Recurrence, error)

func NewRecurrenceFromISODuration

func NewRecurrenceFromISODuration(p datetime.ISODuration, anchor time.Time) (Recurrence, error)

func (Recurrence) GetPeriodAt

func (r Recurrence) GetPeriodAt(t time.Time) (ClosedPeriod, error)

Returns a period where p.Contains(t) is true

func (Recurrence) IterateFromNextAfter

func (r Recurrence) IterateFromNextAfter(t time.Time, boundaryBehavior Boundary) (RecurrenceIterator, error)

IterateFromNextAfter returns the next time after (or equal to) t that the recurrence should occur.

If boundaryBehavior is Inclusive, if t matches a recurrence value, we return t as is. If boundaryBehavior is Exclusive, if t matches a recurrence value, we return the next recurrence value.

func (Recurrence) IterateFromPrevBefore

func (r Recurrence) IterateFromPrevBefore(t time.Time, boundaryBehavior Boundary) (RecurrenceIterator, error)

IterateFromPrevBefore returns the previous time before (or equal to) t that the recurrence should occur.

If boundaryBehavior is Inclusive, if t matches a recurrence value, we return t as is. If boundaryBehavior is Exclusive, if t matches a recurrence value, we return the previous recurrence value.

func (Recurrence) Iterator

func (r Recurrence) Iterator() RecurrenceIterator

Iterator returns an iterator that starts at the anchor.

func (Recurrence) NextAfter

func (r Recurrence) NextAfter(t time.Time, boundaryBehavior Boundary) (time.Time, error)

NextAfter is a convenience function that returns the next time after t that the recurrence should occur. It is equivalent to calling IterateFromNextAfter and returning the At field of the iterator.

If boundaryBehavior is Inclusive, if t matches a recurrence value, we return t as is. If boundaryBehavior is Exclusive, if t matches a recurrence value, we return the next recurrence value.

func (Recurrence) PrevBefore

func (r Recurrence) PrevBefore(t time.Time, boundaryBehavior Boundary) (time.Time, error)

PrevBefore is a convenience function that returns the previous time before t that the recurrence should occur. It is equivalent to calling IterateFromPrevBefore and returning the At field of the iterator.

If boundaryBehavior is Inclusive, if t matches a recurrence value, we return t as is. If boundaryBehavior is Exclusive, if t matches a recurrence value, we return the previous recurrence value.

func (Recurrence) Validate

func (r Recurrence) Validate() error

type RecurrenceIterator

type RecurrenceIterator struct {
	At time.Time
	// contains filtered or unexported fields
}

func (RecurrenceIterator) Next

func (RecurrenceIterator) Prev

type SimpleTimeline

type SimpleTimeline = Timeline[time.Time]

func NewSimpleTimeline

func NewSimpleTimeline(times []time.Time) SimpleTimeline

type Timed

type Timed[T any] struct {
	// contains filtered or unexported fields
}

func (Timed[T]) Equal

func (t Timed[T]) Equal(other Timed[T]) bool

func (Timed[T]) GetTime

func (t Timed[T]) GetTime() time.Time

func (Timed[T]) GetValue

func (t Timed[T]) GetValue() T

type Timeline

type Timeline[T any] struct {
	// contains filtered or unexported fields
}

func NewTimeline

func NewTimeline[T any](times []Timed[T]) Timeline[T]

func (Timeline[T]) After

func (t Timeline[T]) After(at time.Time) Timeline[T]

func (Timeline[T]) Before

func (t Timeline[T]) Before(at time.Time) Timeline[T]

func (Timeline[T]) GetAt

func (t Timeline[T]) GetAt(idx int) Timed[T]

func (Timeline[T]) GetBoundingPeriod

func (t Timeline[T]) GetBoundingPeriod() ClosedPeriod

func (Timeline[T]) GetClosedPeriods

func (t Timeline[T]) GetClosedPeriods() []ClosedPeriod

GetClosedPeriods returns the closed periods between the times in the timeline.

func (Timeline[T]) GetOpenPeriods

func (t Timeline[T]) GetOpenPeriods() []OpenPeriod

GetOpenPeriods returns the open periods between the times in the timeline. For non-empty timelines, the first and last periods will be open at the start and end of the timeline respectively while the rest will effectively be closed.

func (Timeline[T]) GetTimes

func (t Timeline[T]) GetTimes() []time.Time

Jump to

Keyboard shortcuts

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