period

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoesOverlap

func DoesOverlap(a, b Period) bool

DoesOverlap returns true if a and b overlap, otherwise false.

a and b are NOT considered to be overlapping if their bounds merely
touch - for that see Touching.

func Duration

func Duration(period Period) time.Duration

Duration returns the duration that a period covers. If period is nil,

0 is returned.

func Max

func Max(a, b Period, timeFunc TimeFunction) time.Time

Max returns the maximum time of timeFunc(a) vs. timeFunc(b)

func Min

func Min(a, b Period, timeFunc TimeFunction) time.Time

Min returns the minimum time of timeFunc(a) vs. timeFunc(b)

func Overlap

func Overlap(a, b Period) time.Duration

Overlap returns the duration of the section for which a and b overlap.

If a and b do NOT overlap, or either is nil, then 0 is returned.

func Sort

func Sort(many Periods)

Sort orders the elements naturally.

func Touching

func Touching(a, b Period) bool

Touching returns true if a and b overlap OR if their bounds touch,

otherwise false.

Types

type Period

type Period interface {
	Valid() bool
	Start() time.Time
	End() time.Time
	TransformToNew(time.Time, time.Time) Period
}

Period is a conventional time period, which is a starting point

in time followed by an ending period of time, plus all the points in-between.

func Scale

func Scale(period Period, origin time.Time, factor float64) Period

Scale returns a new period where period has been scaled by factor from origin. e.g. period: (0:00:02.000,0:00:03.000), origin: 0:00:01.000, factor: 2.0 => (0:00:03.000,0:00:05.000)

func Shift

func Shift(period Period, amount time.Duration) Period

Shift returns a new period with the start and end adjusted to be +amount.

type Periods

type Periods []Period

Periods is a slice of Period. It can itself be considered a Period (and we

implement Period for Periods)... see below.

func CoverGaps

func CoverGaps(many Periods) Periods

CoverGaps will close any gaps between close elements by stretching those

elements to cover the gap. The degree to which the elements are stretched is
determined by their original Duration.

func FixOverlaps

func FixOverlaps(many Periods) Periods

FixOverlaps will adjust any set of overlapping elements in many such that

their bounds touch, and they share the span of their overlapping set in
proportion to their original Durations.

func MergeTouching

func MergeTouching(many Periods, mergeFunc func(a, b Period) Period) Periods

MergeTouching will merge any touching periods using mergeFunc.

mergeFunc should return a period which has Start = a.Start() and end
= b.End(), otherwise the result is not guaranteed to have non-touching
elements.

func (Periods) End

func (p Periods) End() time.Time

End is the maximum end of all elements in Periods.

func (Periods) Start

func (p Periods) Start() time.Time

Start is the minimum start of all elements in Periods.

func (Periods) TransformToNew

func (p Periods) TransformToNew(start, end time.Time) Period

TransformToNew scales and shifts the elements of Periods, such that

the minimum start of all elements is now "start", and the maximum end
of all elements is now "end". The elements are also transformed into new
variants, and the relative relationship between elements is unchanged -
e.g. if elements 2 and 7 were overlapping, they will continue to overlap by
the same percentage after the transformation.

func (Periods) Valid

func (p Periods) Valid() bool

Valid is true for Periods when there is at least one element, and all elements

are themselves Valid.

type TimeFunction

type TimeFunction func(Period) time.Time

TimeFunction must return a time value for a given Period.

Generally, this will be Period.Start or Period.End.

Jump to

Keyboard shortcuts

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