Documentation ¶
Overview ¶
Package timerange provides simple types to handle a time range.
Index ¶
- func In(t time.Time, r TimeRange) bool
- type SplitIterator
- type TimeRange
- func (r TimeRange) After(t time.Time) bool
- func (r TimeRange) Before(t time.Time) bool
- func (r TimeRange) Contains(t time.Time) bool
- func (r TimeRange) Duration() time.Duration
- func (r TimeRange) End() time.Time
- func (r TimeRange) Equal(x TimeRange) bool
- func (r TimeRange) Extend(d time.Duration) TimeRange
- func (r TimeRange) ExtendDate(years, months, days int) TimeRange
- func (r TimeRange) IsZero() bool
- func (r TimeRange) Shift(d time.Duration) TimeRange
- func (r TimeRange) ShiftDate(years, months, days int) TimeRange
- func (r TimeRange) Split(span time.Duration) []time.Time
- func (r TimeRange) SplitIterator(span time.Duration) SplitIterator
- func (r TimeRange) Start() time.Time
- func (r TimeRange) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func In ¶
In returns true if the time is within the range. This is a synonym of TimeRange.Contains().
Example ¶
desiredTime := time.Date(2006, 1, 2, 15, 6, 0, 0, time.UTC) availableRange := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) if timerange.In(desiredTime, availableRange) { fmt.Printf("The reservation at %s is available.", desiredTime) }
Output: The reservation at 2006-01-02 15:06:00 +0000 UTC is available.
Types ¶
type SplitIterator ¶ added in v1.1.0
type SplitIterator interface { // HasNext returns true if the next time is within the range. HasNext() bool // Next returns the next time. // It the next time is not in the range, this returns a zero value. Next() time.Time }
SplitIterator is an iterator to retrieve time points within a range.
type TimeRange ¶
type TimeRange struct {
// contains filtered or unexported fields
}
TimeRange represents an immutable range of time with timezone. The range includes start time and end time, i.e., [start, end]. Start time must be earlier than end time.
func From ¶
From returns a TimeRange with start time and duration. The duration must be positive.
Example ¶
start := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) r := timerange.From(start, 15*time.Minute) fmt.Print(r)
Output: [2006-01-02T15:04:05Z, 2006-01-02T15:19:05Z]
func Intersect ¶
Intersect returns the intersection of given ranges. If the intersection is empty, this returns a zero value.
func New ¶
New returns a TimeRange with start time and end time. It must be start <= end. If start > end, this returns a zero value.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) fmt.Print(r)
Output: [2006-01-02T15:04:05Z, 2006-01-02T15:07:05Z]
func Until ¶
Until returns a TimeRange with end time and duration. The duration must be positive.
Example ¶
end := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) r := timerange.Until(end, 15*time.Minute) fmt.Print(r)
Output: [2006-01-02T14:49:05Z, 2006-01-02T15:04:05Z]
func (TimeRange) Contains ¶
Contains returns true if the time is within this range.
Example ¶
desiredTime := time.Date(2006, 1, 2, 15, 6, 0, 0, time.UTC) availableRange := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) if availableRange.Contains(desiredTime) { fmt.Printf("The reservation at %s is available.", desiredTime) }
Output: The reservation at 2006-01-02 15:06:00 +0000 UTC is available.
func (TimeRange) Extend ¶
Extend returns an extended TimeRange for the duration. If the duration is positive, this returns the longer range. If the duration is negative, this returns the shorter range.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) fmt.Print(r.Extend(15 * time.Minute))
Output: [2006-01-02T15:04:05Z, 2006-01-02T15:22:05Z]
func (TimeRange) ExtendDate ¶ added in v1.2.0
ExtendDate returns an extended TimeRange for the duration in days. If the duration is positive, this returns the longer range. If the duration is negative, this returns the shorter range.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) fmt.Print(r.ExtendDate(0, 0, 1))
Output: [2006-01-02T15:04:05Z, 2006-01-03T15:07:05Z]
func (TimeRange) Shift ¶
Shift returns a TimeRange moved by the duration. If the duration is positive, this returns the later range. If the duration is negative, this returns the earlier range.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) fmt.Print(r.Shift(15 * time.Minute))
Output: [2006-01-02T15:19:05Z, 2006-01-02T15:22:05Z]
func (TimeRange) ShiftDate ¶ added in v1.2.0
ShiftDate returns a TimeRange moved by the duration in days. If the duration is positive, this returns the later range. If the duration is negative, this returns the earlier range.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) fmt.Print(r.ShiftDate(0, 0, -1))
Output: [2006-01-01T15:04:05Z, 2006-01-01T15:07:05Z]
func (TimeRange) Split ¶ added in v1.1.0
Split returns an array of time points within this range. If the span is longer than this range, this returns only start time.
If the result array is too long, consider using SplitIterator() instead.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) for _, t := range r.Split(1 * time.Minute) { fmt.Println(t) }
Output: 2006-01-02 15:04:05 +0000 UTC 2006-01-02 15:05:05 +0000 UTC 2006-01-02 15:06:05 +0000 UTC 2006-01-02 15:07:05 +0000 UTC
func (TimeRange) SplitIterator ¶ added in v1.1.0
func (r TimeRange) SplitIterator(span time.Duration) SplitIterator
SplitIterator returns an iterator for time points within this range. If the span is longer than this range, this returns only start time.
Example ¶
r := timerange.New( time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Date(2006, 1, 2, 15, 7, 5, 0, time.UTC), ) iter := r.SplitIterator(1 * time.Minute) for iter.HasNext() { fmt.Println(iter.Next()) }
Output: 2006-01-02 15:04:05 +0000 UTC 2006-01-02 15:05:05 +0000 UTC 2006-01-02 15:06:05 +0000 UTC 2006-01-02 15:07:05 +0000 UTC