CHANGE: added work and pause time

This commit is contained in:
2025-02-23 09:44:18 +01:00
parent 6a264a20c0
commit f73cf0884c
5 changed files with 43 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
package models
import (
"fmt"
"time"
)
@@ -12,26 +13,51 @@ type WorkDay struct {
// 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)
var workTime, pauseTime time.Duration
var lastBooking Booking
for _, booking := range d.Bookings{
if booking.CheckInOut % 2 == 1 {
if !lastBooking.Timestamp.IsZero() {
pauseTime += booking.Timestamp.Sub(lastBooking.Timestamp)
}
}else {
workTime += booking.Timestamp.Sub(lastBooking.Timestamp)
}
lastBooking = booking
}
// checks if booking is today and has no gehen yet, so the time since last kommen booking is added to workTime
if(d.Day.Day() == time.Now().Day() && len(d.Bookings) % 2 == 1){
workTime += time.Since(lastBooking.Timestamp.Local())
}
// 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)
if workTime > 6 * time.Hour && pauseTime < 45 * time.Minute {
if workTime < 9 * time.Hour && pauseTime < 30 * time.Minute {
diff := 30 * time.Minute - pauseTime
workTime -= diff
pauseTime += diff
}else if pauseTime < 45 * time.Minute {
diff := 45 * time.Minute - pauseTime
workTime -= diff
pauseTime += diff
}
}
return d.workTime
return workTime
}
func formatDuration(d time.Duration) string{
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
if d.Hours() > 0 {
return fmt.Sprintf("%dh %dmin", hours, minutes)
}
return fmt.Sprintf("%dmin", minutes)
}
// 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"
str := formatDuration(d.GetWorkTime().Abs().Round(time.Minute))
if(len(str) > 2){
return str
}