Files
arbeitszeitmessung/Backend/helper/time.go
Tom Tröger 46218f9bca fix: weekbased calculation pdf report
with this change the time calculations for pdf reports should be better
line with the reports send as "week_report"
2026-02-15 18:16:57 +01:00

141 lines
2.9 KiB
Go

package helper
// time helpers
import (
"fmt"
"slices"
"time"
)
func GetMonday(ts time.Time) time.Time {
if ts.Weekday() != time.Monday {
if ts.Weekday() == time.Sunday {
return ts.AddDate(0, 0, -6)
} else {
return ts.AddDate(0, 0, -int(ts.Weekday()-1))
}
}
return ts
}
func GetMondays(allDays []time.Time, onlyInRange bool) []time.Time {
var mondays []time.Time
var start, end time.Time
for _, day := range allDays {
mondays = append(mondays, GetMonday(day))
if start.IsZero() || day.Before(start) {
start = day
}
if end.IsZero() || day.After(end) {
end = day
}
}
mondays = slices.Compact(mondays)
if onlyInRange {
return DaysInRange(mondays, start, end)
}
return mondays
}
func DaysInRange(days []time.Time, startDate, endDate time.Time) []time.Time {
filtered := []time.Time{}
startDate = startDate.Add(-time.Minute)
endDate = endDate.Add(time.Minute)
for _, day := range days {
if day.After(startDate) && day.Before(endDate) {
filtered = append(filtered, day)
}
}
return filtered
}
func IsMonday(day time.Time) bool {
return day.Weekday() == time.Monday
}
// GenerateDateRange returns a slice of all dates between start and end (inclusive).
func GenerateDateRange(start, end time.Time) []time.Time {
var dates []time.Time
// Ensure start is before or equal to end
if start.After(end) {
return dates
}
// Normalize times to midnight
current := start.Truncate(time.Hour * 24)
end = end.Truncate(time.Hour * 24)
for !current.After(end) {
dates = append(dates, current)
current = current.AddDate(0, 0, 1) // Add one day
}
return dates
}
func GetFirstOfMonth(ts time.Time) time.Time {
if ts.Day() > 1 {
return ts.AddDate(0, 0, -(ts.Day() - 1))
}
return ts
}
func IsWeekend(ts time.Time) bool {
return ts.Weekday() == time.Saturday || ts.Weekday() == time.Sunday
}
func FormatDuration(d time.Duration) string {
return FormatDurationFill(d, false)
}
// Converts duration to string
func FormatDurationFill(d time.Duration, fill bool) string {
hours := int(d.Abs().Hours())
minutes := int(d.Abs().Minutes()) % 60
sign := ""
if d < 0 {
sign = "-"
}
switch {
case hours > 0 && minutes == 0:
return fmt.Sprintf("%s%dh", sign, hours)
case hours > 0:
return fmt.Sprintf("%s%dh %dmin", sign, hours, minutes)
case minutes > 0:
return fmt.Sprintf("%s%dmin", sign, minutes)
default:
if fill {
return "0min"
}
return ""
}
}
func IsSameDate(a, b time.Time) bool {
return a.Truncate(24 * time.Hour).Equal(b.Truncate(24 * time.Hour))
}
func GetWorkingDays(startDate, endDate time.Time) int {
if endDate.Before(startDate) {
return 0
}
var count int = 0
for d := startDate.Truncate(24 * time.Hour); !d.After(endDate); d = d.Add(24 * time.Hour) {
if !IsWeekend(d) {
count++
}
}
return count
}
func FormatGermanDayOfWeek(t time.Time) string {
return days[t.Weekday()][:2]
}
var days = [...]string{
"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}