time_utils

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OneDay  = time.Hour * 24
	OneWeek = OneDay * 7
)

Variables

View Source
var InvalidTimeSegmentErr = errors.New("invalid start/end times. End cannot be before start")

Functions

func AddMonths

func AddMonths(t time.Time, delta int) time.Time

func AlignTimeWithStepEnd

func AlignTimeWithStepEnd(endTime time.Time, step TimeStep) time.Time

func AlignTimeWithStepStart

func AlignTimeWithStepStart(startTime time.Time, step TimeStep) time.Time

func AssertAdjacentTimeSegmentsAreNonIntersecting

func AssertAdjacentTimeSegmentsAreNonIntersecting(segments []TimeSegment) error

func AssertAdjacentTimeSegmentsAreNonOverlapping

func AssertAdjacentTimeSegmentsAreNonOverlapping(segments []TimeSegment) error

func AssertTimeSegmentsAreContinuous

func AssertTimeSegmentsAreContinuous(segments []TimeSegment) error

func Compare

func Compare(a time.Time, b time.Time) bool

func Copy

func Copy(t *time.Time) *time.Time

func ExtractBoundariesFromTimeSegments

func ExtractBoundariesFromTimeSegments(timeSegments []TimeSegment, extraBoundaries ...time.Time) ([]time.Time, error)

func FirstDayOfTheMonth

func FirstDayOfTheMonth(t time.Time) time.Time

func IsZero

func IsZero(a *time.Time) bool

func LastDayOfTheMonth

func LastDayOfTheMonth(t time.Time) time.Time

func MustParseTime

func MustParseTime(format string, s string) time.Time

func PtrCompare

func PtrCompare(a *time.Time, b *time.Time) bool

func PtrTo

func PtrTo(t time.Time) *time.Time

func StartOfTheDay

func StartOfTheDay(t time.Time) time.Time

func StartOfTheHour

func StartOfTheHour(t time.Time) time.Time

func TimeBetweenExclusive

func TimeBetweenExclusive(now time.Time, startTime time.Time, endTime time.Time) bool

func TimeBetweenInclusive

func TimeBetweenInclusive(now time.Time, startTime time.Time, endTime time.Time) bool

func TimeIsAlignedWithTimeStep

func TimeIsAlignedWithTimeStep(time time.Time, step TimeStep) bool

func Weekday

func Weekday(t time.Time, weekday time.Weekday) time.Time

func Zero

func Zero() *time.Time

Types

type StepAlignedTimeSegment

type StepAlignedTimeSegment struct {
	TimeSegment
	TimeStep
}

func AlignTimesWithTimeStep

func AlignTimesWithTimeStep(startTime time.Time, endTime time.Time, step TimeStep) StepAlignedTimeSegment

type TimeSegment

type TimeSegment struct {
	Start time.Time `json:"start"`
	End   time.Time `json:"end"`
}

func MustParseTimeSegment

func MustParseTimeSegment(format string, s string, e string) TimeSegment

func NewTimeSegment

func NewTimeSegment(start time.Time, end time.Time) (TimeSegment, error)

func (TimeSegment) Align

func (t TimeSegment) Align(timeStep TimeStep) StepAlignedTimeSegment

func (TimeSegment) AssertIsComposedOf

func (t TimeSegment) AssertIsComposedOf(subSegments []TimeSegment) error

AssertIsComposedOf returns an error if this TimeSegment is not "composed of" the given subSegments

A TimeSegment is "composed of" subSegments if:

  • The subSegments are continuous
  • The subSegments are non-overlapping
  • The subSegments completely cover the TimeSegment

func (TimeSegment) Clip

func (t TimeSegment) Clip(boundary TimeSegment) TimeSegment

func (TimeSegment) Contains

func (t TimeSegment) Contains(toTest time.Time, includeEdges bool) bool

func (TimeSegment) ContainsTimeSegment

func (t TimeSegment) ContainsTimeSegment(other TimeSegment, includeEdges bool) bool

func (TimeSegment) CountTimeStepBoundaries

func (t TimeSegment) CountTimeStepBoundaries(timeStep TimeStep) int

func (TimeSegment) Decompose

func (t TimeSegment) Decompose(boundaries []time.Time, decomposeInto []TimeStep, greedy bool) ([]StepAlignedTimeSegment, error)

Decompose attempts to break this TimeSegment into smaller TimeSegments along the given time boundaries. The output TimeSegments will be aligned with one of the TimeSteps given by decomposeInto. If one or more of the boundary times are not aligned with at least one of the given TimeSteps, the decomposition will fail

func (TimeSegment) Duration

func (t TimeSegment) Duration() time.Duration

func (TimeSegment) ExecuteOncePerBoundary

func (t TimeSegment) ExecuteOncePerBoundary(f func(t time.Time) error, timeStep TimeStep) error

func (TimeSegment) ExecuteOncePerDuration

func (t TimeSegment) ExecuteOncePerDuration(function func(t TimeSegment) error, d time.Duration) error

func (TimeSegment) ExecuteOncePerTimeStep

func (t TimeSegment) ExecuteOncePerTimeStep(f func(t TimeSegment) error, timeStep TimeStep) error

func (TimeSegment) Format

func (t TimeSegment) Format(layout string) string

func (TimeSegment) Intersect

func (t TimeSegment) Intersect(other TimeSegment) (TimeSegment, bool)

func (TimeSegment) Intersecting

func (t TimeSegment) Intersecting(other TimeSegment, includeBoundaries bool) bool

func (TimeSegment) IsAlignedWithTimeStep

func (t TimeSegment) IsAlignedWithTimeStep(timeStep TimeStep) bool

func (TimeSegment) IsContainedBy

func (t TimeSegment) IsContainedBy(parent TimeSegment, includeEdges bool) bool

func (TimeSegment) ListTimeStepBoundaries

func (t TimeSegment) ListTimeStepBoundaries(timeStep TimeStep) []time.Time

func (TimeSegment) Overlaps

func (t TimeSegment) Overlaps(other TimeSegment, includeEdges bool) bool

func (TimeSegment) SplitByDuration

func (t TimeSegment) SplitByDuration(d time.Duration) ([]TimeSegment, error)

func (TimeSegment) SplitByTimeStep

func (t TimeSegment) SplitByTimeStep(timeStep TimeStep) ([]StepAlignedTimeSegment, error)

SplitByTimeStep returns a slice of segments aligned with time step boundaries. NOTE: The time covered by the returned TimeSegments will extend beyond the original TimeSegment if it wasn't already boundary aligned

func (TimeSegment) UTC

func (t TimeSegment) UTC() TimeSegment

type TimeSegmentPair

type TimeSegmentPair struct {
	First  TimeSegment
	Second TimeSegment
}

func FindAdjacentIntersectingTimeSegments

func FindAdjacentIntersectingTimeSegments(segments []TimeSegment, includeBoundaries bool) []TimeSegmentPair

func FindDiscontinuities

func FindDiscontinuities(segments []TimeSegment) []TimeSegmentPair

type TimeStep

type TimeStep string
const (
	TimeStepHour  TimeStep = "hour"
	TimeStepDay   TimeStep = "day"
	TimeStepWeek  TimeStep = "week"
	TimeStepMonth TimeStep = "month"
)

func (TimeStep) Advance

func (s TimeStep) Advance(t time.Time, steps int) time.Time

Jump to

Keyboard shortcuts

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