-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift.go
More file actions
74 lines (66 loc) · 1.26 KB
/
Copy pathshift.go
File metadata and controls
74 lines (66 loc) · 1.26 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
package main
import (
"errors"
"gopkg.in/mgo.v2/bson"
"time"
)
type Shift struct {
Id bson.ObjectId `bson:"_id,omitempty"`
On int64 `bson:"on,omitempty"`
Off int64 `bson:"off"`
Active bool `bson:"active"`
}
func NewShift() *Shift {
return &Shift{
On: time.Now().Unix(),
Off: time.Now().Unix(),
Active: true,
}
}
func FullShift(on, off int64) (*Shift, error) {
if off < on {
return nil, errors.New("Off time is before on time.")
}
return &Shift{
On: on,
Off: off,
Active: false,
}, nil
}
func (s *Shift) DayOverlap(day_start int64) int64 {
day_end := day_start + 86400 // Seconds in a day
// Handle cases where shift does not overlap with day
if s.On >= day_end {
return 0
}
if s.Off < day_start {
return 0
}
if s.Active {
return min(day_end, time.Now().Unix()) - max(day_start, s.On)
} else {
return min(day_end, s.Off) - max(day_start, s.On)
}
}
func (s *Shift) OnDay(day_start int64) bool {
day_end := day_start + 86400 // Seconds in a day
if s.Off > day_start && s.Off <= day_end {
return true
} else {
return false
}
}
func max(a, b int64) int64 {
if a > b {
return a
} else {
return b
}
}
func min(a, b int64) int64 {
if a < b {
return a
} else {
return b
}
}