timeseries

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 5 Imported by: 6

README

timeseries

Custom Golang timeseries implementation

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeRangeInvalid is returned when the time range is invalid.
	ErrTimeRangeInvalid = fmt.Errorf("time range invalid")
	// ErrTimeRangeStartAfterEnd is returned when the start time is after the end time.
	ErrTimeRangeStartAfterEnd = fmt.Errorf("%w: start after end", ErrTimeRangeInvalid)
)
View Source
var (
	// ErrTimeStampAlreadyExists is returned when trying to set a timestamp that already exists.
	ErrTimeStampAlreadyExists = errors.New("timestamp-already-exists")
)

Functions

func TimeRangesToString

func TimeRangesToString(tr []TimeRange) string

TimeRangesToString returns a string representation of the time ranges.

Types

type MergeOptions

type MergeOptions struct {
	// ErrorOnCollision returns an error if there is a collision between the two TimeSeries.
	ErrorOnCollision bool
}

MergeOptions are options for the Merge function.

type TimeRange

type TimeRange struct {
	Start, End time.Time
}

TimeRange is a structure representing the slot of time, from start to end.

func MergeTimeRanges

func MergeTimeRanges(tr1, tr2 []TimeRange) ([]TimeRange, error)

MergeTimeRanges merges two time ranges together.

func OrderTimeRanges

func OrderTimeRanges(tr []TimeRange) []TimeRange

OrderTimeRanges returns a list of TimeRange ordered by start time.

func TimeRangesFromMissingTimes

func TimeRangesFromMissingTimes(interval time.Duration, times []time.Time) []TimeRange

TimeRangesFromMissingTimes return missing time ranges from a list of times.

func (TimeRange) Validate

func (tr TimeRange) Validate() error

Validate checks if the time range is valid.

type TimeSerie

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

TimeSerie is a time serie data structure that can be used to store data over time.

func New

func New[T any]() *TimeSerie[T]

New creates a new TimeSerie.

func (TimeSerie[T]) AreMissing

func (ts TimeSerie[T]) AreMissing(start, end time.Time, interval time.Duration, limit uint) bool

AreMissing checks if there is missing candlesticks between two times.

func (*TimeSerie[T]) Delete

func (ts *TimeSerie[T]) Delete(t ...time.Time)

Delete deletes a value at a specific time.

func (TimeSerie[T]) Extract

func (ts TimeSerie[T]) Extract(start, end time.Time, limit int) *TimeSerie[T]

Extract extracts a TimeSerie between two times.

func (TimeSerie[T]) First

func (ts TimeSerie[T]) First() (time.Time, T, bool)

First returns the first element of the TimeSerie.

func (TimeSerie[T]) FirstN

func (ts TimeSerie[T]) FirstN(limit uint) *TimeSerie[T]

FirstN returns the first N elements of the TimeSerie.

func (*TimeSerie[T]) Get

func (ts *TimeSerie[T]) Get(t time.Time) (T, bool)

Get gets a value at a specific time. The second return value is false if the value for this specific time does not exist.

func (TimeSerie[T]) GetMissingRanges

func (ts TimeSerie[T]) GetMissingRanges(start, end time.Time, interval time.Duration, limit uint) []TimeRange

GetMissingRanges returns an array of missing time ranges in the timeserie.

func (TimeSerie[T]) GetMissingTimes

func (ts TimeSerie[T]) GetMissingTimes(start, end time.Time, interval time.Duration, limit uint) []time.Time

GetMissingTimes returns an array of missing time in the timeserie.

func (TimeSerie[T]) Last

func (ts TimeSerie[T]) Last() (time.Time, T, bool)

Last returns the last element of the TimeSerie.

func (*TimeSerie[T]) Len

func (ts *TimeSerie[T]) Len() int

Len returns the length of the TimeSerie.

func (*TimeSerie[T]) Loop

func (ts *TimeSerie[T]) Loop(callback func(time.Time, T) (bool, error)) error

Loop loops over every elements of the TimeSerie in time order and calls the callback function.

func (*TimeSerie[T]) MarshalJSON

func (ts *TimeSerie[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals the TimeSerie into a JSON object.

func (*TimeSerie[T]) Merge

func (ts *TimeSerie[T]) Merge(ts2 TimeSerie[T], options *MergeOptions) error

Merge merges two TimeSeries together.

func (*TimeSerie[T]) Set

func (ts *TimeSerie[T]) Set(t time.Time, d T) *TimeSerie[T]

Set sets a value at a specific time.

func (TimeSerie[T]) String added in v1.1.0

func (ts TimeSerie[T]) String() string

String returns a string representation of the TimeSerie.

func (TimeSerie[T]) ToArray added in v1.1.0

func (ts TimeSerie[T]) ToArray() []T

ToArray converts the TimeSerie to an array of values.

func (TimeSerie[T]) ToList

func (ts TimeSerie[T]) ToList() []T

ToList returns an ordered list of the TimeSerie.

func (*TimeSerie[T]) UnmarshalJSON

func (ts *TimeSerie[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the TimeSerie from a JSON object.

Jump to

Keyboard shortcuts

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