Files
arbeitszeitmessung/Backend/models/workDay.go

52 lines
1.3 KiB
Go

package models
import (
"time"
)
type WorkDay struct {
Day time.Time
Bookings []Booking
workTime time.Duration
}
// Gets the duration someone worked that day
func (d WorkDay) GetWorkTime() time.Duration{
d.workTime = 0
for i := 0; i < len(d.Bookings) - (1 + len(d.Bookings)%2); i += 2{
start := d.Bookings[i].Timestamp
end := d.Bookings[i+1].Timestamp
d.workTime += end.Sub(start)
}
// checks if booking is today and has no gehen yet, so the time since last kommen booking is added to workTime
if(d.Day.Equal(time.Now().Truncate(24 * time.Hour)) && len(d.Bookings) % 2 == 1){
d.workTime += time.Since(d.Bookings[len(d.Bookings)-1].Timestamp)
}
return d.workTime
}
// Converts duration to string and replaces 0s with in
//
// -> output xhxmin
func (d WorkDay) GetWorkTimeString() string {
str := d.GetWorkTime().Abs().Round(time.Minute).String()
str = str[:len(str)-2] + "in"
if(len(str) > 2){
return str
}
return ""
}
// returns bool wheter the workday was ended with an automatic logout
func (d WorkDay) RequiresAction() bool {
return d.Bookings[len(d.Bookings)-1].CheckInOut == 255
}
// returns a integer percentage of how much day has been worked of
func (d WorkDay) GetWorkDayProgress() uint8 {
defaultWorkTime, _ := time.ParseDuration("7h42m")
progress := (d.GetWorkTime().Seconds()/defaultWorkTime.Seconds())*100
return uint8(progress)
}