-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_span.go
More file actions
95 lines (80 loc) · 2.46 KB
/
time_span.go
File metadata and controls
95 lines (80 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package time
import (
"fmt"
"strings"
"time"
)
// LocalTimeSpan represents a span between two LocalTime instances
type LocalTimeSpan struct {
from LocalTime
to LocalTime
}
// NewTimeSpan creates new LocalTimeSpan
func NewTimeSpan(from LocalTime, to LocalTime) LocalTimeSpan {
if to != Midnight && from.After(to) {
panic(fmt.Sprintf("Start can't be after end of LocalTimeSpan."+
" Got: start: %v end: %v", from, to))
}
return LocalTimeSpan{from, to}
}
// OverlapsDateTimeSpan checks if LocalTimeSpan overlaps DateTimeSpan
func (ts LocalTimeSpan) OverlapsDateTimeSpan(dateTimeSpan DateTimeSpan) bool {
fromDay := dateTimeSpan.from.Date()
container := ts.DateTimeSpanWithinOneDay(fromDay)
return container.Overlaps(dateTimeSpan)
}
// Overlaps checks if LocalTimeSpan overlaps other LocalTimeSpan
func (ts LocalTimeSpan) Overlaps(other LocalTimeSpan) bool {
if ts.to == Midnight && ts.from.Before(other.to) {
return true
}
if !ts.from.Before(other.to) || !ts.to.After(other.from) {
return false
}
return true
}
// Contains check if LocalTimeSpan contains DateTimeSpan
func (ts LocalTimeSpan) Contains(dateTimeSpan DateTimeSpan) bool {
fromDay := dateTimeSpan.from.Date()
container := ts.DateTimeSpanWithinOneDay(fromDay)
return container.Contains(dateTimeSpan)
}
// From returns start of LocalTimeSpan
func (ts LocalTimeSpan) From() LocalTime {
return ts.from
}
// To returns end of LocalTimeSpan
func (ts LocalTimeSpan) To() LocalTime {
return ts.to
}
// DateTimeSpanWithinOneDay creates DateTimeSpan with start and end on the
// given day and with time corresponding to LocalTimeSpan start and end.
func (ts LocalTimeSpan) DateTimeSpanWithinOneDay(date LocalDate) DateTimeSpan {
start := date.WithTime(ts.from)
var end LocalDateTime
if ts.to == Midnight {
end = date.Next().Start()
} else {
end = date.WithTime(ts.to)
}
return NewDateTimeSpan(start, end)
}
// MustParseTimeSpan parses string in form of "15:04-15:04" into LocalTimeSpan
func MustParseTimeSpan(value string) LocalTimeSpan {
times := strings.Split(value, "-")
from := MustParseLocalTime(times[0])
to := MustParseLocalTime(times[1])
return NewTimeSpan(from, to)
}
func (ts LocalTimeSpan) Duration() Duration {
from := time.Date(0, 0, 0, ts.from.hour, ts.from.minute, 0, 0, time.UTC)
day := 0
if ts.to == Midnight {
day++
}
to := time.Date(0, 0, day, ts.to.hour, ts.to.minute, 0, 0, time.UTC)
return Duration(to.Sub(from))
}
func (ts LocalTimeSpan) Valid() bool {
return ts.Duration() > 0
}