Documentation
¶
Overview ¶
Package timespan provides the types represent span of time.
Example ¶
package main
import (
"fmt"
"time"
"github.com/takumakei/go-timespan"
_ "time/tzdata"
)
func main() {
// the span represents 2 hours from 23:00Z to 01:00Z(next day)
span := timespan.MustParse("23:00Z/2h")
epoch := time.Unix(0, 0)
fmt.Println(span.Contains(epoch.Add(time.Hour - 1))) // true: 00:59:59.999...
fmt.Println(span.Contains(epoch.Add(time.Hour))) // false: 01:00:00.000...
fmt.Println(span.Contains(epoch.Add(23*time.Hour - 1))) // false: 22:59:59.999...
fmt.Println(span.Contains(epoch.Add(23 * time.Hour))) // true: 23:00:00.000...
}
Output: true false false true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Always means an entire span. Always = constant(true) // Never means an empty span. Never = constant(false) )
var ( // ErrParseTimeSlot is an error. ErrParseTimeSlot = errors.New("can't parse as TimeSlot") // ErrParseTime is an error. ErrParseTime = fmt.Errorf("%w (time)", ErrParseTimeSlot) // ErrParseDuration is an error. ErrParseDuration = fmt.Errorf("%w (duration)", ErrParseTimeSlot) )
Functions ¶
This section is empty.
Types ¶
type All ¶
type All []Span
All is a composite span.
type Any ¶
type Any []Span
Any is a composite span.
type Span ¶
type Span interface {
// Contains returns true if t is inside a span of this.
Contains(t time.Time) bool
}
Span represents a time span.
func MustParse ¶
MustParse calls Parse(s), returns a span. In case of err != nil, it calls panic(err).
func Parse ¶
Parse parses s and returns a span.
Example ¶
package main
import (
"fmt"
"time"
"github.com/takumakei/go-timespan"
)
func main() {
span, err := timespan.Parse("0:00+0900/3s 13:24+0900/1h2m3s 0:00Z/3m")
if err != nil {
panic(err)
}
fmt.Println(span)
t0, err := time.Parse(time.RFC3339, "2020-12-05T00:00:00+09:00")
if err != nil {
panic(err)
}
fmt.Println(span.Contains(t0))
t1, err := time.Parse(time.RFC3339, "2020-12-05T13:24:00+09:00")
if err != nil {
panic(err)
}
fmt.Println(span.Contains(t1))
t2, err := time.Parse(time.RFC3339, "2020-12-05T09:00:00+09:00")
if err != nil {
panic(err)
}
fmt.Println(span.Contains(t2))
}
Output: (00:00+0900/3s or 13:24+0900/1h2m3s or 00:00Z/3m0s) true true true
type TimeSlot ¶
type TimeSlot struct {
// contains filtered or unexported fields
}
TimeSlot is a Span represents some hours out of 24 hours.
Begin time is inclusive. End time is exclusive. In case of d > 0, t is of begin time, end time is t.Add(d). In case of d < 0, t is of end time, begin time is t.Add(d).
func NewTimeSlot ¶
NewTimeSlot returns a TimeSlot consisted by reference time and duration.
func ParseTimeSlot ¶
ParseTimeSlot parses s and returns a TimeSlot