timespan

package module
v0.0.0-...-e45bec3 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: Apache-2.0 Imports: 5 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Time span

The Go package timespan provides the types represent the span of time.

example
// 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

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

View Source
var (
	// Always means an entire span.
	Always = constant(true)

	// Never means an empty span.
	Never = constant(false)
)
View Source
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.

func (All) Contains

func (s All) Contains(t time.Time) bool

Contains returns true in case of all of spans in s returns true.

func (All) String

func (s All) String() string

String returns a string representation of s.

type Any

type Any []Span

Any is a composite span.

func (Any) Contains

func (s Any) Contains(t time.Time) bool

Contains returns true in case of any one of spans in s returns true.

func (Any) String

func (s Any) String() string

String returns a string representation of s.

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 And

func And(ss ...Span) Span

And makes an All span of ss.

func MustParse

func MustParse(s string) Span

MustParse calls Parse(s), returns a span. In case of err != nil, it calls panic(err).

func Or

func Or(ss ...Span) Span

Or makes an Any span of ss.

func Parse

func Parse(s string) (Span, error)

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

func NewTimeSlot(t time.Time, d time.Duration) *TimeSlot

NewTimeSlot returns a TimeSlot consisted by reference time and duration.

func ParseTimeSlot

func ParseTimeSlot(s string) (*TimeSlot, error)

ParseTimeSlot parses s and returns a TimeSlot

func (*TimeSlot) Contains

func (s *TimeSlot) Contains(t time.Time) bool

Contains returns true if t is inside a span of s.

func (*TimeSlot) String

func (s *TimeSlot) String() string

String returns a string representation of s.

Jump to

Keyboard shortcuts

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