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"
This commit is contained in:
@@ -4,6 +4,7 @@ package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,6 +19,64 @@ func GetMonday(ts time.Time) time.Time {
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user