-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
215 lines (200 loc) · 5.09 KB
/
calendar.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package tt
import (
"errors"
"fmt"
"strings"
"time"
"github.com/fatih/color"
)
var monthNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
type Year struct {
Year int
Months [12]Month
}
func (y Year) String() string {
b := strings.Builder{}
for m, month := range y.Months {
if len(month.Days) == 0 {
continue
}
b.WriteString(fmt.Sprintf("%04d %s\n", y.Year, monthNames[m]))
b.WriteString(month.String())
b.WriteString("\n\n")
}
return b.String()
}
type Month struct {
// Days contains the days of the month but starts at 0
Days []Day
}
func (m Month) String() string {
b := strings.Builder{}
b.WriteString("Mon Tue Wed Thu Fri Sat Sun\n")
// we need to insert space to account for months that do not start on mondays
dayOfWeek := correctedWeekday(m.Days[0].Time.Weekday())
b.WriteString(strings.Repeat(" ", dayOfWeek))
for i, day := range m.Days {
b.WriteString(day.String())
if day.Time.Weekday() == time.Sunday && i < len(m.Days)-1 {
// after a sunday we need a new line
b.WriteRune('\n')
} else {
b.WriteString(" ")
}
}
return b.String()
}
type Day struct {
Time time.Time
Timers Timers
Vacation *VacationDay
}
func (d Day) String() string {
tracked := d.Timers.Duration()
planned, err := PlannedTime(d.Time)
if err != nil {
panic(err.Error())
}
// TODO: how do we handle edge cases?
// 1. working on vacation
// 2. working on non-work-days
if !IsWorkDay(d.Time) && tracked == 0 {
// could also be a vacation day but we don't care if we shouldn't work and didn't work
return fmt.Sprintf("%02d ", d.Time.Day())
} else if (d.Vacation == nil || d.Vacation.Half) && (planned != 0 || tracked != 0) {
var coloring func(string, ...interface{}) string
if planned < tracked {
coloring = color.GreenString
} else if planned > tracked {
coloring = color.RedString
} else {
coloring = color.BlueString
}
return fmt.Sprintf("%02d %s", d.Time.Day(), coloring("%s", FormatDurationCustom(tracked, time.Minute)))
} else {
return fmt.Sprintf("%02d vac. ", d.Time.Day())
}
}
// correctedWeekday adjusts from Sunday as first day of the week to monday as
// first day of the week.
func correctedWeekday(weekday time.Weekday) int {
if weekday == time.Sunday {
return int(time.Saturday)
} else {
return int(weekday) - 1
}
}
// IsWorkDay checks if the day should have been worked on. It does not take into
// account vacation days, but only the configured weekdays.
func IsWorkDay(d time.Time) bool {
days := GetConfig().Timeclock.DaysPerWeek
switch d.Weekday() {
case time.Monday:
return days.Monday
case time.Tuesday:
return days.Tuesday
case time.Wednesday:
return days.Wednesday
case time.Thursday:
return days.Thursday
case time.Friday:
return days.Friday
case time.Saturday:
return days.Saturday
case time.Sunday:
return days.Sunday
default:
panic(fmt.Sprintf("unknown day of week %d", d.Weekday()))
}
}
// PlannedTime returns the duration that was planned for the given date.
func PlannedTime(date time.Time) (time.Duration, error) {
if !IsWorkDay(date) {
return 0, nil
}
workTime := time.Duration(GetConfig().Timeclock.HoursPerDay) * time.Hour
var vac VacationDay
err := GetDB().GetVacationDay(VacationFilter(date), &vac)
if errors.Is(err, ErrNotFound) {
return workTime, nil
} else if err != nil {
return 0, err
}
if vac.Half {
return workTime / 2, nil
}
return 0, nil
}
func BuildCalendar() ([]Year, error) {
db := GetDB()
var timers Timers
err := db.GetTimers(EmptyFilter, OrderBy{}, &timers)
if err != nil {
return nil, err
}
groupedTimers := timers.GroupByDay()
var start time.Time
var stop time.Time
for _, timers := range groupedTimers {
for _, timer := range timers {
if start.IsZero() || timer.Start.Unix() < start.Unix() {
start = timer.Start
}
if stop.IsZero() || timer.Start.Unix() > stop.Unix() {
stop = timer.Start
}
}
}
var years []Year
for y := start.Year(); y <= stop.Year(); y++ {
var months [12]Month
for m := 0; !(y == stop.Year() && time.Month(m+1) > stop.Month()) && m < 12; m++ {
if y == start.Year() && time.Month(m+1) < start.Month() {
continue
}
var days []Day
for d := 0; isValidDate(y, m, d); d++ {
key := fmt.Sprintf("%04d-%02d-%02d", y, m+1, d+1)
t := time.Date(y, time.Month(m+1), d+1, 0, 0, 0, 0, time.UTC)
var vac VacationDay
var pVac *VacationDay
err = db.GetVacationDay(VacationFilter(t), &vac)
if err == nil {
pVac = &vac
} else if err != nil && !errors.Is(err, ErrNotFound) {
return nil, err
}
days = append(days, Day{
Time: t,
Timers: groupedTimers[key],
Vacation: pVac,
})
}
months[m] = Month{
Days: days,
}
}
years = append(years, Year{
Year: y,
Months: months,
})
}
return years, nil
}
func isValidDate(year, month, day int) bool {
d := time.Date(year, time.Month(month+1), day+1, 0, 0, 0, 0, time.UTC)
return d.Year() == year && month+1 == int(d.Month()) && day+1 == d.Day()
}