timekit

package
v0.297.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package timekit is a collection of time related helpers

Deprecated: not deprecated, but experimental, API is subject to changes.

Index

Examples

Constants

View Source
const ErrParseDuration errorkit.Error = "ErrParseDuration"

Variables

This section is empty.

Functions

func DayDiff

func DayDiff(ref, target time.Time) int

DayDiff returns the difference in days between two times, truncating each to midnight before comparison.

func EnableTimeEnum

func EnableTimeEnum() func()

func MonthDiff

func MonthDiff(ref, target time.Month) int

MonthDiff calculates the number of months between ref and target. It assumes a cyclic nature, meaning that if target is before ref in the year, it wraps around after December.

Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/timekit"
)

func main() {
	_ = timekit.MonthDiff(time.January, time.January)   // 0
	_ = timekit.MonthDiff(time.January, time.February)  // 1
	_ = timekit.MonthDiff(time.January, time.March)     // 2
	_ = timekit.MonthDiff(time.January, time.April)     // 3
	_ = timekit.MonthDiff(time.January, time.May)       // 4
	_ = timekit.MonthDiff(time.January, time.June)      // 5
	_ = timekit.MonthDiff(time.January, time.July)      // 6
	_ = timekit.MonthDiff(time.January, time.August)    // 7
	_ = timekit.MonthDiff(time.January, time.September) // 8
	_ = timekit.MonthDiff(time.January, time.October)   // 9
	_ = timekit.MonthDiff(time.January, time.November)  // 10
	_ = timekit.MonthDiff(time.January, time.December)  // 11
	_ = timekit.MonthDiff(time.December, time.January)  // 1
	_ = timekit.MonthDiff(time.October, time.January)   // 3
}

func Months

func Months() []time.Month

func ShiftMonth

func ShiftMonth(m time.Month, n int) time.Month

func ShiftWeekday

func ShiftWeekday(wd time.Weekday, d int) time.Weekday

func ToDate

func ToDate(ref time.Time) (year int, month time.Month, day int, tz *time.Location)

func WeekdayDiff

func WeekdayDiff(ref, target time.Weekday) int

WeekdayDiff calculates the number of days between ref and target. It assumes a cyclic nature, meaning that if target is before ref in the week, it wraps around after Sunday.

Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/timekit"
)

func main() {
	_ = timekit.WeekdayDiff(time.Monday, time.Monday)    // 0
	_ = timekit.WeekdayDiff(time.Monday, time.Tuesday)   // 1
	_ = timekit.WeekdayDiff(time.Monday, time.Wednesday) // 2
	_ = timekit.WeekdayDiff(time.Monday, time.Thursday)  // 3
	_ = timekit.WeekdayDiff(time.Monday, time.Friday)    // 4
	_ = timekit.WeekdayDiff(time.Monday, time.Saturday)  // 5
	_ = timekit.WeekdayDiff(time.Monday, time.Sunday)    // 6
}

func Weekdays

func Weekdays() []time.Weekday

Types

type DayTime

type DayTime struct {
	Hour   int `range:"0..24"`
	Minute int `range:"0..60"`
}

func (DayTime) Compare

func (d DayTime) Compare(oth DayTime) int

func (DayTime) IsZero

func (d DayTime) IsZero() bool

func (DayTime) ToTime

func (d DayTime) ToTime(year int, month time.Month, day int, tz *time.Location) time.Time

func (DayTime) ToTimeRelTo

func (d DayTime) ToTimeRelTo(ref time.Time) time.Time

ToTimeRelTo will return the time.Time equalement of a DayTime, which is relative to the provided reference time's date.

type Duration

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

Duration allows to describe distances between two time point, that normally would not be possible with time.Duration.

Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/timekit"
)

func main() {
	var d timekit.Duration
	d = d.Between(time.Now(), time.Now().AddDate(1000, 0, 0)) // 1000 years worth of duration relative to time now.
	_ = d.String()
}

func (Duration) Add

func (d Duration) Add(o Duration) Duration

func (Duration) AddDuration

func (d Duration) AddDuration(duration time.Duration) Duration

func (Duration) AddTo

func (d Duration) AddTo(t time.Time) time.Time

func (Duration) Between

func (Duration) Between(from, till time.Time) Duration

func (Duration) ByDuration

func (Duration) ByDuration(duration time.Duration) Duration

func (Duration) Compare

func (d Duration) Compare(o Duration) int

func (Duration) IsZero

func (d Duration) IsZero() bool

func (Duration) Iter

func (d Duration) Iter() iter.Seq[time.Duration]

func (Duration) Parse

func (Duration) Parse(raw string) (Duration, error)

func (Duration) String

func (d Duration) String() string

type Interval

type Interval interface {
	UntilNext(since time.Time) time.Duration
}

type Monthly

type Monthly time.Month

func (Monthly) Near

func (m Monthly) Near(ref time.Time) (_ time.Time, _ bool)

Near will return the nearest time that Has the given Month.

type Range

type Range struct {
	From time.Time
	Till time.Time
}

func (Range) Contain

func (tr Range) Contain(ref time.Time) bool

func (Range) Duration

func (tr Range) Duration() time.Duration

func (Range) IsZero

func (tr Range) IsZero() bool

func (Range) Overlaps

func (tr Range) Overlaps(other Range) bool

func (Range) Validate

func (tr Range) Validate() error

type Schedule

type Schedule struct {
	// DayTime is the time during a day, where the scheduling begins.
	DayTime DayTime
	// Duration is length of the scheduling.
	Duration time.Duration
	// Month defines for which month the AvailabilityPolicy is meant for.
	Months []time.Month
	// Day defines for which day the AvailabilityPolicy is meant for.
	Days []int
	// Weekday defines if the policy is meant for a given day, or for any day of the week.
	// if nil, then it is interpreted as unbound.
	Weekdays []time.Weekday
	// Location of the AvailabilityPolicy.
	// When no Location set, then Product specific configuration is expected.
	Location *time.Location
}

Schedule allows to define an abstract time, that continously reoccurs in time based on the specifications set in its field.

func (Schedule) Check

func (sch Schedule) Check(ref time.Time) bool

func (Schedule) IsZero

func (sch Schedule) IsZero() bool

func (Schedule) Near

func (sch Schedule) Near(ref time.Time) (nearOccurrence Range, ok bool)

func (Schedule) Next

func (sch Schedule) Next(ref time.Time) (nextOccurrence Range, ok bool)

func (Schedule) Validate

func (sch Schedule) Validate() error

Jump to

Keyboard shortcuts

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