All checks were successful
Tests / Run Go Tests (push) Successful in 1m24s
improved ci/cd pipeline + interactive install script Reviewed-on: #58 Co-authored-by: Tom Tröger <t.troeger.02@gmail.com> Co-committed-by: Tom Tröger <t.troeger.02@gmail.com>
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package helper
|
|
|
|
import (
|
|
"fmt"
|
|
"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 IsWeekend(ts time.Time) bool {
|
|
return ts.Weekday() == time.Saturday || ts.Weekday() == time.Sunday
|
|
}
|
|
|
|
func GetKW(t time.Time) int {
|
|
_, kw := t.ISOWeek()
|
|
return kw
|
|
}
|
|
|
|
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
|
|
}
|