-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek.go
111 lines (96 loc) · 1.96 KB
/
week.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
107
108
109
110
111
package godate
import "fmt"
// A Weekday representing the 7 days of the week.
// In addition to the textual name, each day-of-week has an int value.
// The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
type Weekday int
const (
Monday Weekday = 1 + iota
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
var longWeekdayNames = []string{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}
var shortWeekdayNames = []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}
// String returns the English name of the day ("Sunday", "Monday", ...).
func (w Weekday) String() string {
if Monday <= w && w <= Sunday {
return longWeekdayNames[w]
}
return fmt.Sprintf("!Weekday(\"%d\")", w)
}
// IntValue returns the day-of-week to represent, from 1 (Monday) to 7 (Sunday)
func (w Weekday) IntValue() int {
return int(w)
}
func (d Date) Workdays() (workdays []Date) {
w := d.Weekday()
switch w {
case Saturday, Sunday:
for i, cnt := int(w)-1, 5; i > 0 && cnt > 0; i, cnt = i-1, cnt-1 {
day, _ := d.SubDay(int(i))
workdays = append(workdays, day)
}
//case Sunday:
// for i := Monday; i <= Friday; i++ {
// day, _ := d.AddDay(int(i))
// workdays = append(workdays, day)
// }
default:
workdays = make([]Date, 5)
wi := int(w) - 1
workdays[wi] = d
before, after := int(w-Monday), int(Friday-w)
for before > 0 {
day, _ := d.SubDay(before)
workdays[wi-before] = day
before--
}
for after > 0 {
day, _ := d.AddDay(after)
workdays[wi+after] = day
after--
}
}
return
}
func (d Date) NextWorkday() Date {
dayOfWeek := d.Weekday()
var dayToAdd int
switch dayOfWeek {
case Friday:
dayToAdd = 3
case Saturday:
dayToAdd = 2
default:
dayToAdd = 1
}
next, _ := d.AddDay(dayToAdd)
return next
}
func (d Date) IsWorkday() bool {
return !d.IsWeekend()
}
func (d Date) IsWeekend() bool {
weekday := d.Weekday()
return Saturday == weekday || Sunday == weekday
}