timeutil

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// OneSecond is the number of millisecond for a second
	OneSecond int64 = 1000
	// OneMinute is the number of millisecond for a minute
	OneMinute = 60 * OneSecond
	// OneHour is the number of millisecond for an hour
	OneHour = 60 * OneMinute
	// OneDay is the number of millisecond for a day
	OneDay = 24 * OneHour
	// OneWeek is the number of millisecond for a week
	OneWeek = 7 * OneDay
	// OneMonth is the number of millisecond for a month
	OneMonth = 30 * OneDay
	// OneYear is the number of millisecond for a year
	OneYear = 365 * OneDay

	DataTimeFormat1 = "20060102 15:04:05"
	DataTimeFormat2 = "2006-01-02 15:04:05"
	DataTimeFormat3 = "2006/01/02 15:04:05"
	DataTimeFormat4 = "20060102150405"
)

Variables

View Source
var ErrUnknownInterval = errors.New("unknown interval")

Functions

func CalIntervalRatio

func CalIntervalRatio(queryInterval, storageInterval int64) int

CalIntervalRatio calculates the interval ratio for query, if query interval < storage interval return 1.

func CalPointCount

func CalPointCount(startTime, endTime, interval int64) int

CalPointCount calculates point counts between start time and end time by interval

func CalcTimestamp

func CalcTimestamp(startTime int64, slot int, interval Interval) int64

CalcTimestamp returns timestamp based on start time, slot and interval.

func FormatTimestamp

func FormatTimestamp(timestamp int64, layout string) string

FormatTimestamp returns timestamp format based on layout

func Now

func Now() int64

Now returns t as a Unix time, the number of millisecond elapsed since January 1, 1970 UTC. The result does not depend on the location associated with t.

func NowNano

func NowNano() int64

NowNano returns t as a unix time, the number of nanoseconds elapsed

func ParseTimestamp

func ParseTimestamp(timestampStr string, layout ...string) (int64, error)

ParseTimestamp parses timestamp str value based on layout using local zone

func Truncate

func Truncate(timestamp, interval int64) int64

Truncate truncates timestamp based on interval

Types

type Interval

type Interval int64

Interval is the interval value in millisecond

func CalcQueryInterval

func CalcQueryInterval(queryTimeRange TimeRange, queryInterval Interval) Interval

CalcQueryInterval returns query interval based on query time range and interval.

func (Interval) CalcSlotRange

func (i Interval) CalcSlotRange(familyTime int64, timeRange TimeRange) SlotRange

CalcSlotRange returns slot range by family time and time range.

func (Interval) Calculator

func (i Interval) Calculator() IntervalCalculator

Calculator returns the calculator for current interval.

func (Interval) Int64

func (i Interval) Int64() int64

func (*Interval) MarshalJSON

func (i *Interval) MarshalJSON() (data []byte, err error)

MarshalJSON converts an interval to a string for decoding json

func (Interval) MarshalText

func (i Interval) MarshalText() (text []byte, err error)

MarshalText converts an interval to a string for decoding toml

func (Interval) String

func (i Interval) String() string

String returns the string representation of the interval.

func (Interval) Type

func (i Interval) Type() IntervalType

func (*Interval) UnmarshalJSON

func (i *Interval) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON parses a JSON value into an interval value.

func (*Interval) UnmarshalText

func (i *Interval) UnmarshalText(text []byte) error

UnmarshalText parses a TOML value into an interval value. See https://github.com/BurntSushi/toml

func (*Interval) ValueOf

func (i *Interval) ValueOf(intervalStr string) error

ValueOf parses the interval str, return number of interval(millisecond),

type IntervalCalculator

type IntervalCalculator interface {
	// GetSegment returns segment name by given timestamp
	GetSegment(timestamp int64) string
	// ParseSegmentTime parses segment base time based on given segment name
	ParseSegmentTime(segmentName string) (int64, error)
	// CalcSegmentTime calculates segment base time based on given segment name
	CalcSegmentTime(timestamp int64) int64
	// CalcFamily calculates family base time based on given timestamp
	CalcFamily(timestamp, segmentTime int64) int
	// CalcFamilyTime calculates family base time based on given timestamp
	CalcFamilyTime(timestamp int64) int64
	// CalcFamilyStartTime calculates family start time based on segment time and family time
	CalcFamilyStartTime(segmentTime int64, familyTime int) int64
	// CalcFamilyEndTime calculates family end time based on family start time
	CalcFamilyEndTime(familyStartTime int64) int64
	// CalcSlot calculates field store slot index based on given timestamp and base time
	CalcSlot(timestamp, baseTime, interval int64) int
	// CalcTimeWindows calculates the number of time window between start and end time
	CalcTimeWindows(start, end int64) int
}

IntervalCalculator calculates the timestamp for each interval type

type IntervalType

type IntervalType string

IntervalType defines interval type

const (
	Day     IntervalType = "day"
	Month   IntervalType = "month"
	Year    IntervalType = "year"
	Unknown IntervalType = "unknown"
)

Interval types.

func (IntervalType) String

func (t IntervalType) String() string

String implements stringer

type SlotRange

type SlotRange struct {
	Start, End uint16
}

SlotRange represents time range with start/end timestamp using low value.

func NewSlotRange

func NewSlotRange(start, end uint16) SlotRange

NewSlotRange creates a new slot range with start/end

func (*SlotRange) Contains

func (sr *SlotRange) Contains(slot uint16) bool

Contains tests if timestamp in current time range

func (*SlotRange) GetRange

func (sr *SlotRange) GetRange() (start, end uint16)

GetRange returns the time slot range

func (*SlotRange) Overlap

func (sr *SlotRange) Overlap(o SlotRange) bool

Overlap tests if overlap with current time range

func (*SlotRange) SetSlot

func (sr *SlotRange) SetSlot(slot uint16)

SetSlot sets the time slot range

func (*SlotRange) Union

func (sr *SlotRange) Union(o SlotRange) SlotRange

Union returns the union of two slot range

type TimeRange

type TimeRange struct {
	Start int64 `json:"start"`
	End   int64 `json:"end"`
}

TimeRange represents time range with start/end timestamp.

func (*TimeRange) Contains

func (r *TimeRange) Contains(timestamp int64) bool

Contains tests if timestamp in current time range

func (*TimeRange) Intersect

func (r *TimeRange) Intersect(o TimeRange) TimeRange

Intersect returns the intersection of two time range

func (*TimeRange) IsEmpty

func (r *TimeRange) IsEmpty() bool

IsEmpty tests if empty, start>=end => empty

func (*TimeRange) Overlap

func (r *TimeRange) Overlap(o TimeRange) bool

Overlap tests if overlap with current time range

Jump to

Keyboard shortcuts

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