init
This commit is contained in:
94
pkg/utils/utils.go
Normal file
94
pkg/utils/utils.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RoundToTwoDecimals rounds a float64 to two decimal places
|
||||
func RoundToTwoDecimals(value float64) float64 {
|
||||
return math.Round(value*100) / 100
|
||||
}
|
||||
|
||||
// StartOfDay returns the start of the day for a given time
|
||||
func StartOfDay(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// EndOfDay returns the end of the day for a given time
|
||||
func EndOfDay(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, t.Location())
|
||||
}
|
||||
|
||||
// StartOfMonth returns the first day of the month for a given time
|
||||
func StartOfMonth(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// EndOfMonth returns the last day of the month for a given time
|
||||
func EndOfMonth(t time.Time) time.Time {
|
||||
return StartOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||
}
|
||||
|
||||
// StartOfYear returns the first day of the year for a given time
|
||||
func StartOfYear(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// EndOfYear returns the last day of the year for a given time
|
||||
func EndOfYear(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), 12, 31, 23, 59, 59, 999999999, t.Location())
|
||||
}
|
||||
|
||||
// StartOfWeek returns the start of the week (Monday) for a given time
|
||||
func StartOfWeek(t time.Time) time.Time {
|
||||
weekday := int(t.Weekday())
|
||||
if weekday == 0 {
|
||||
weekday = 7 // Sunday is 7
|
||||
}
|
||||
return StartOfDay(t.AddDate(0, 0, -(weekday - 1)))
|
||||
}
|
||||
|
||||
// EndOfWeek returns the end of the week (Sunday) for a given time
|
||||
func EndOfWeek(t time.Time) time.Time {
|
||||
return EndOfDay(StartOfWeek(t).AddDate(0, 0, 6))
|
||||
}
|
||||
|
||||
// ParseDate parses a date string in YYYY-MM-DD format
|
||||
func ParseDate(dateStr string) (time.Time, error) {
|
||||
return time.Parse("2006-01-02", dateStr)
|
||||
}
|
||||
|
||||
// FormatDate formats a time to YYYY-MM-DD string
|
||||
func FormatDate(t time.Time) string {
|
||||
return t.Format("2006-01-02")
|
||||
}
|
||||
|
||||
// Min returns the minimum of two integers
|
||||
func Min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Max returns the maximum of two integers
|
||||
func Max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Abs returns the absolute value of a float64
|
||||
func Abs(value float64) float64 {
|
||||
return math.Abs(value)
|
||||
}
|
||||
|
||||
// CalculatePercentage calculates the percentage of part relative to total
|
||||
func CalculatePercentage(part, total float64) float64 {
|
||||
if total == 0 {
|
||||
return 0
|
||||
}
|
||||
return RoundToTwoDecimals((part / total) * 100)
|
||||
}
|
||||
Reference in New Issue
Block a user