-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
106 lines (85 loc) · 2.29 KB
/
utils.go
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
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"log"
"net"
"time"
)
// Networks is a slice of net.IPNet
type Networks struct {
nets []*net.IPNet
}
// NewNetworks creates a new Networks struct from a slice of subnets
func NewNetworks(subnets []string) Networks {
n := Networks{}
for _, subnet := range subnets {
_, net, err := net.ParseCIDR(subnet)
if err != nil {
log.Fatalf("[FATAL] Invalid subnet %s: %s", subnet, err)
}
n.nets = append(n.nets, net)
}
log.Printf("[INFO] Trusted subnets: %s", n.nets)
return n
}
// Contains returns true if the given IP is in one of the trusted subnets
// Runs in O(the number of trusted subnets)
func (n *Networks) Contains(ip net.IP) bool {
for _, net := range n.nets {
if net.Contains(ip) {
return true
}
}
return false
}
// Returns the next Wednesday in YYYYMMDD format
func nextWednesday() string {
const format = "20060102"
now := time.Now().In(tz)
daysUntilWednesday := time.Wednesday - now.Weekday()
if daysUntilWednesday == 0 {
return now.Format(format)
} else if daysUntilWednesday > 0 {
return now.AddDate(0, 0, int(daysUntilWednesday)).Format(format)
} else {
return now.AddDate(0, 0, int(daysUntilWednesday)+7).Format(format)
}
}
func addWeek(week string) string {
const format = "20060102"
t, err := time.ParseInLocation(format, week, tz)
if err != nil {
log.Fatalln("Failed to parse week:", err)
}
return t.AddDate(0, 0, 7).Format(format)
}
func subtractWeek(week string) string {
const format = "20060102"
t, err := time.ParseInLocation(format, week, tz)
if err != nil {
log.Fatalln("Failed to parse week:", err)
}
return t.AddDate(0, 0, -7).Format(format)
}
func weekForHumans(week string) (string, error) {
const format = "20060102"
t, err := time.ParseInLocation(format, week, tz)
if err != nil {
return "", err
}
return t.Format("January _2, 2006"), nil
}
func isPast(current, week string) bool {
const format = "20060102"
currentTime, err := time.ParseInLocation(format, current, tz)
if err != nil {
log.Fatalln("Failed to parse week:", err)
}
weekTime, err := time.ParseInLocation(format, week, tz)
if err != nil {
log.Fatalln("Failed to parse week:", err)
}
return currentTime.After(weekTime)
}
func duringMeeting() bool {
return time.Now().In(tz).Weekday() == time.Wednesday && time.Now().In(tz).Hour() >= 19
}