Documentation
¶
Index ¶
- func All[T any](r golang.Range[T], values ...T) bool
- func Any[T any](r golang.Range[T], values ...T) bool
- func Between[T cmp.Ordered](lower, upper T) golang.Range[T]
- func Bounds[R golang.Range[T], T any](r R) (lower, upper T)
- func Duration(t golang.Range[*time.Time]) time.Duration
- func In[T any](r golang.Range[T], v T) bool
- func Intersect[T any](a, b golang.Range[T]) (golang.Range[T], bool)
- func Interval(r golang.Range[*time.Time], options ...TimeOption) golang.Interval[*time.Time]
- func Merge[T any](a, b golang.Range[T]) (golang.Range[T], bool)
- func New[T any](lower, upper T, comparer func(a, b T) int) golang.Range[T]
- func Pick[T any](r golang.Range[T], values ...T) (found []T)
- func Since(start *time.Time) golang.Range[*time.Time]
- func Time(start, end *time.Time, options ...TimeOption) golang.Interval[*time.Time]
- func Unpick[T any](r golang.Range[T], values ...T) (found []T)
- type Incremented
- type TimeOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Between ¶
Between create a new range of ordered value
创建一个有序值的区间
Example ¶
package main
import (
"fmt"
"github.com/keepitlight/golang/ranges"
)
func main() {
r := ranges.Between(90, 100)
l, u := ranges.Bounds(r)
fmt.Println(l, u)
fmt.Println(ranges.In(r, 50), ranges.In(r, 95))
fmt.Println(ranges.Pick(r, 1, 2, 3, 4, 5, 99, 101))
fmt.Println(ranges.Unpick(r, 1, 2, 3, 4, 5, 99, 101))
}
Output: 90 100 false true [99] [1 2 3 4 5 101]
func Bounds ¶
Bounds get bounds of range
获取范围边界值
Example ¶
package main
import (
"fmt"
"time"
"github.com/keepitlight/golang"
"github.com/keepitlight/golang/ranges"
)
func main() {
n := time.Now()
s := golang.AddTime(n, -1, 0, 0, 0)
r := ranges.Time(&s, &n)
l, u := ranges.Bounds(r)
fmt.Println(u.Sub(*l))
}
Output: 1h0m0s
func Duration ¶
Duration return duration of time range
返回时间范围的持续时长
Example ¶
package main
import (
"fmt"
"time"
"github.com/keepitlight/golang/ranges"
)
func main() {
s, e := time.Now(), time.Now().Add(time.Hour)
// 实际运行时有运行时时间差
t := ranges.Time(&s, &e)
fmt.Println(ranges.Duration(t).Round(time.Second))
// 舍掉时间计算的时间差
func Intersect ¶
Intersect returns the intersection of two ranges, a and b, or nil if one of any is `nil` or non-overlapping.
返回 a 和 b 的交集,如果任一一个是 `nil` 或者不相交,则返回 nil。
func Merge ¶
Merge combines two ranges, a and b, returning nil if they're both `nil` or non-overlapping, and the non-nil range if one of them is nil.
合并两个范围 a 和 b,如果它们都是 `nil` 或者不相交,则返回 nil,否则返回非空的。
Example ¶
package main
import (
"fmt"
"time"
"github.com/keepitlight/golang"
"github.com/keepitlight/golang/ranges"
)
func main() {
s, _ := time.Parse(golang.DateTimeZone, "2023-07-07 08:10:00 UTC")
e1 := s.Add(time.Hour)
e2 := s.Add(-time.Hour)
i := s.Add(10 * time.Minute)
t1 := ranges.Time(&s, &e1, ranges.WithInitialValue(&i), ranges.WithInterval(time.Minute))
t2 := ranges.Time(&s, &e2, ranges.WithInitialValue(&i), ranges.WithInterval(time.Minute))
m, ok := ranges.Merge[*time.Time](t1, t2)
l, u := m.Bounds()
fmt.Println(l, u, ok)
}
Output: 2023-07-07 07:10:00 +0000 UTC 2023-07-07 09:10:00 +0000 UTC true
func New ¶
New create a new range, comparer is a function to compare two value a and b, if a < b return -1, a == b return 0, a > b return 1, nil if comparer is nil.
使用比较函数创建一个范围,函数 comparer 比较两个值 a 和 b,如果 a < b 返回 -1,a == b 返回 0,a > b 返回 1。 如果 compare 为 nil,则返回 nil。
func Since ¶
Since like time.Since to create a new time range from start to now, nil if start is nil or start is after now.
从 start 到现在创建一个时间范围,如果 start 为 nil 或 start 在现在之后,则返回 nil
Example ¶
package main
import (
"fmt"
"time"
"github.com/keepitlight/golang/ranges"
)
func main() {
s := time.Now().Add(-time.Hour)
// 实际运行时有运行时时间差
t := ranges.Since(&s)
fmt.Println(ranges.Duration(t).Round(time.Second))
// 舍掉时间计算的时间差
Types ¶
type Incremented ¶
type Incremented struct {
}
type TimeOption ¶
type TimeOption func(*timeInterval)
func WithInitialValue ¶
func WithInitialValue(v *time.Time) TimeOption
WithInitialValue set initial value of time range, default is start
设置时间范围的初始值,默认为 start
func WithInterval ¶
func WithInterval(duration time.Duration) TimeOption
WithInterval set interval of time range, default is 1 hour
设置时间范围的间隔时长,默认为 1 小时