-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.go
49 lines (38 loc) · 954 Bytes
/
date.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
package main
import "time"
type Date struct {
time.Time
}
func DateOf(t time.Time) Date {
// Grab local date:
//_, zoneOffset := t.Zone()
l := t.Location()
y, m, d := t.Date()
// Build new date:
return Date{time.Date(y, m, d, 6, 0, 0, 0, l)}
}
func (date Date) NextDate() Date {
return DateOf(date.Time.Add(25 * time.Hour))
}
func (date Date) BusinessDaysUntil(until Date) int {
// Count weekdays, skipping weekends:
days := 0
d := date
_, startOffset := date.Zone()
_, untilOffset := until.Zone()
untilTime := until.In(date.Location()).Add(time.Duration(untilOffset-startOffset) * time.Second)
//fmt.Printf("from %s to %s\n", date.Time, untilTime)
for d.Time.Before(untilTime) {
//fmt.Printf(" %d %s\n", days, d)
days++
d = d.NextDate()
if d.Time.Weekday() == time.Saturday {
d = d.NextDate()
}
if d.Time.Weekday() == time.Sunday {
d = d.NextDate()
}
}
//fmt.Printf(" %d %s\n", days, d)
return days
}