All checks were successful
GoLang Tests / Run Go Tests (push) Successful in 49s
42 lines
815 B
Go
42 lines
815 B
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
|
|
}
|
|
|
|
// Converts duration to string
|
|
func FormatDuration(d time.Duration) 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:
|
|
return ""
|
|
}
|
|
}
|