spaniel

package module
v2.0.6 Latest Latest
Warning

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

Go to latest
Published: May 31, 2022 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Fork

this fork removes the whole endpoint logic and simplifies it.

Spaniel

Time span handling for Go

godoc license build

Spaniel contains functionality for timespan handling, specifically for merging overlapping timespans and finding the intersections between multiple timespans. It lets you specify the type of interval you want to use (open, closed), and provide handlers for when you want to add more functionality when merging/intersecting.

Install

This package is "go-gettable", just do:

go get github.com/senseyeio/spaniel

Basics

Spaniel operates on lists of timespans, where a timespan is represented as the interval between a start and end time. It has a built-in minimal timespan representation for convenience, or you can use your own type, so long as it implements the timespan.Span interface.

To import spaniel and create a new list of timespans:

package main

import (
	"fmt"
	timespan "github.com/senseyeio/spaniel"
	"time"
)

func main() {
	// Times at half-hourly intervals
	var t1 = time.Date(2018, 1, 30, 0, 0, 0, 0, time.UTC)
	var t2 = time.Date(2018, 1, 30, 0, 30, 0, 0, time.UTC)
	var t3 = time.Date(2018, 1, 30, 1, 0, 0, 0, time.UTC)
	var t4 = time.Date(2018, 1, 30, 1, 30, 0, 0, time.UTC)

	input := timespan.Spans{
		timespan.New(t1, t3),
		timespan.New(t2, t4),
	}
	fmt.Println(input)
}

You can then use the Union function to merge the timestamps:

union := input.Union()
fmt.Println(union[0].Start(), "->", union[0].End()) // 2018-01-30 00:00:00 +0000 UTC -> 2018-01-30 01:30:00 +0000 UTC

Or the Intersection function to find the overlaps:

intersection := input.Intersection()
fmt.Println(intersection[0].Start(), "->", intersection[0].End()) // 2018-01-30 00:30:00 +0000 UTC -> 2018-01-30 01:00:00 +0000 UTC

Types

timespan.New sets the span to be [) by default - i.e. including the left-most point, excluding the right-most. In other words, [1,2,3) and [3,4,5) do not overlap, but are contiguous. Instants are [] by default (they contain a single time).

If you would like to override these types, you can use NewWithTypes:

openSpan := timespan.NewWithTypes(t1, t3, timespan.Open, timespan.Open)

You can see a more involved example of types in examples/types/types.go

Handlers

If you need to use a more complex object, you can call UnionWithHandler and IntersectionWithHandler. There is an example of this in examples/handlers/handlers.go.

More Examples

All of the above examples are available in the examples folder.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInstant

func IsInstant(a Span) bool

IsInstant returns true if the interval is deemed instantaneous

func Within

func Within(a, b Span) bool

Within returns if b is completely in a Same instants of start or end are considered within.

Types

type ByEnd

type ByEnd Spans

ByEnd sorts a list of spans by their end point

func (ByEnd) Len

func (s ByEnd) Len() int

func (ByEnd) Less

func (s ByEnd) Less(i, j int) bool

func (ByEnd) Swap

func (s ByEnd) Swap(i, j int)

type ByStart

type ByStart Spans

ByStart sorts a list of spans by their start point

func (ByStart) Len

func (s ByStart) Len() int

func (ByStart) Less

func (s ByStart) Less(i, j int) bool

func (ByStart) Swap

func (s ByStart) Swap(i, j int)

type IntersectionHandlerFunc

type IntersectionHandlerFunc func(intersectingEvent1, intersectingEvent2, intersectionSpan Span) Span

IntersectionHandlerFunc is used by IntersectionWithHandler to allow for custom functionality when two spans intersect. It is passed the two spans that intersect, and span representing the intersection.

type Span

type Span interface {
	Start() time.Time
	End() time.Time
	String() string
}

Span represents a basic span, with a start and end time.

type Spans

type Spans []Span

Spans represents a list of spans, on which other functions operate.

func Without

func Without(a, b Span) Spans

Without returns a list of spans representing the spans of baseSpan not covered by intersector. It can return one Span element in Spans, if the intersector can overlap at the beginning or end of baseSpan (baseSpan.Start/End is moved to intersector.End/Start) or It returns two Span elements in Spans, if the intersector is within the baseSpan. this creates a Span baseSpan.Start->interseector.Start and intersector.End->baseSpan.End

func WithoutWithHandler

func WithoutWithHandler(a, b Span, handlerFunc WithoutHandlerFunc) Spans

func (Spans) Duration

func (s Spans) Duration() time.Duration

Duration sums up the duration of all given Spans

func (Spans) Intersection

func (s Spans) Intersection() Spans

Intersection returns a list of Spans representing the overlaps between the contained spans. For example, given a list [A,B] where A and B overlap, a list [C] would be returned, with the span C covering the intersection of A and B.

func (Spans) IntersectionBetween

func (s Spans) IntersectionBetween(b Spans) Spans

IntersectionBetween returns the slice of spans representing the overlaps between the contained spans and a given set of spans.

func (Spans) IntersectionBetweenWithHandler

func (s Spans) IntersectionBetweenWithHandler(candidates Spans, intersectHandlerFunc IntersectionHandlerFunc) Spans

IntersectionBetweenWithHandler returns a list of pointers to Spans representing the overlaps between the contained spans and a given set of spans. It calls intersectHandlerFunc for each pair of spans that are intersected.

func (Spans) IntersectionWithHandler

func (s Spans) IntersectionWithHandler(intersectHandlerFunc IntersectionHandlerFunc) Spans

IntersectionWithHandler returns a list of Spans representing the overlaps between the contained spans. For example, given a list [A,B] where A and B overlap, a list [C] would be returned, with the span C covering the intersection of the A and B. The provided handler function is notified of the two spans that have been found to overlap, and the span representing the overlap.

func (Spans) String

func (s Spans) String() string

func (Spans) Without

func (s Spans) Without(b Span) Spans

Without removes the given Span from the Spans

func (Spans) WithoutWithHandler

func (s Spans) WithoutWithHandler(b Span, handlerFunc WithoutHandlerFunc) Spans

type TimeSpan

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

TimeSpan represents a simple span of time, with no additional properties. It should be constructed with NewEmpty.

func New

func New(start time.Time, end time.Time) *TimeSpan

New creates a span with a start and end time, with the types set to [] for instants and [) for spans.

func (TimeSpan) Duration

func (ts TimeSpan) Duration() time.Duration

func (TimeSpan) End

func (ts TimeSpan) End() time.Time

End returns the end time of a span

func (*TimeSpan) MarshalBinary added in v2.0.3

func (ts *TimeSpan) MarshalBinary() ([]byte, error)

func (TimeSpan) MarshalJSON

func (ts TimeSpan) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshal

func (TimeSpan) Start

func (ts TimeSpan) Start() time.Time

Start returns the start time of a span

func (TimeSpan) String

func (ts TimeSpan) String() string

func (*TimeSpan) UnmarshalBinary added in v2.0.3

func (ts *TimeSpan) UnmarshalBinary(data []byte) error

func (*TimeSpan) UnmarshalJSON

func (ts *TimeSpan) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements json.Unmarshal

type UnionHandlerFunc

type UnionHandlerFunc func(mergeInto, mergeFrom, mergeSpan Span) Span

UnionHandlerFunc is used by UnionWithHandler to allow for custom functionality when two spans are merged. It is passed the two spans to be merged, and span which will result from the union.

type WithoutHandlerFunc

type WithoutHandlerFunc func(a, b Span, diff Spans) Spans

Jump to

Keyboard shortcuts

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