Compare commits

...

3 Commits

Author SHA1 Message Date
a694f36f46 add tz params to timeext 2022-10-27 17:50:28 +02:00
e61682b24c Added termext.CleanString 2022-10-27 17:16:39 +02:00
5dc9e98f6b Add langext.ArrFirst / langext.ArrLast 2022-10-27 17:09:48 +02:00
4 changed files with 64 additions and 23 deletions

View File

@@ -3,6 +3,6 @@ BFB goext library
A collection of general & useful library methods A collection of general & useful library methods
Every subfolder is a seperate dependency and can be imported individually This should not have any heavy dependencies (gin, mongo, etc) and add missing basic language features...
Potentially needs `export GOPRIVATE="gogs.mikescher.com"` Potentially needs `export GOPRIVATE="gogs.mikescher.com"`

View File

@@ -142,6 +142,27 @@ func ArrAnyErr(arr interface{}, fn func(int) (bool, error)) (bool, error) {
return false, nil return false, nil
} }
func ArrFirst[T comparable](arr []T, comp func(v T) bool) (T, bool) {
for _, v := range arr {
if comp(v) {
return v, true
}
}
return *new(T), false
}
func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
found := false
result := *new(T)
for _, v := range arr {
if comp(v) {
found = true
result = v
}
}
return result, found
}
func AddToSet[T comparable](set []T, add T) []T { func AddToSet[T comparable](set []T, add T) []T {
for _, v := range set { for _, v := range set {
if v == add { if v == add {

View File

@@ -1,5 +1,7 @@
package termext package termext
import "strings"
const ( const (
colorReset = "\033[0m" colorReset = "\033[0m"
colorRed = "\033[31m" colorRed = "\033[31m"
@@ -43,3 +45,17 @@ func Gray(v string) string {
func White(v string) string { func White(v string) string {
return colorWhite + v + colorReset return colorWhite + v + colorReset
} }
func CleanString(v string) string {
v = strings.ReplaceAll(v, colorReset, "")
v = strings.ReplaceAll(v, colorRed, "")
v = strings.ReplaceAll(v, colorGreen, "")
v = strings.ReplaceAll(v, colorYellow, "")
v = strings.ReplaceAll(v, colorBlue, "")
v = strings.ReplaceAll(v, colorPurple, "")
v = strings.ReplaceAll(v, colorCyan, "")
v = strings.ReplaceAll(v, colorGray, "")
v = strings.ReplaceAll(v, colorWhite, "")
return v
}

View File

@@ -17,14 +17,14 @@ func init() {
} }
// TimeToDatePart returns a timestamp at the start of the day which contains t (= 00:00:00) // TimeToDatePart returns a timestamp at the start of the day which contains t (= 00:00:00)
func TimeToDatePart(t time.Time) time.Time { func TimeToDatePart(t time.Time, tz *time.Location) time.Time {
t = t.In(TimezoneBerlin) t = t.In(tz)
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
} }
// TimeToWeekStart returns a timestamp at the start of the week which contains t (= Monday 00:00:00) // TimeToWeekStart returns a timestamp at the start of the week which contains t (= Monday 00:00:00)
func TimeToWeekStart(t time.Time) time.Time { func TimeToWeekStart(t time.Time, tz *time.Location) time.Time {
t = TimeToDatePart(t) t = TimeToDatePart(t, tz)
delta := time.Duration(((int64(t.Weekday()) + 6) % 7) * 24 * int64(time.Hour)) delta := time.Duration(((int64(t.Weekday()) + 6) % 7) * 24 * int64(time.Hour))
t = t.Add(-1 * delta) t = t.Add(-1 * delta)
@@ -33,32 +33,32 @@ func TimeToWeekStart(t time.Time) time.Time {
} }
// TimeToMonthStart returns a timestamp at the start of the month which contains t (= yyyy-MM-00 00:00:00) // TimeToMonthStart returns a timestamp at the start of the month which contains t (= yyyy-MM-00 00:00:00)
func TimeToMonthStart(t time.Time) time.Time { func TimeToMonthStart(t time.Time, tz *time.Location) time.Time {
t = t.In(TimezoneBerlin) t = t.In(tz)
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()) return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
} }
// TimeToMonthEnd returns a timestamp at the end of the month which contains t (= yyyy-MM-31 23:59:59.999999999) // TimeToMonthEnd returns a timestamp at the end of the month which contains t (= yyyy-MM-31 23:59:59.999999999)
func TimeToMonthEnd(t time.Time) time.Time { func TimeToMonthEnd(t time.Time, tz *time.Location) time.Time {
return TimeToMonthStart(t).AddDate(0, 1, 0).Add(-1) return TimeToMonthStart(t, tz).AddDate(0, 1, 0).Add(-1)
} }
// TimeToYearStart returns a timestamp at the start of the year which contains t (= yyyy-01-01 00:00:00) // TimeToYearStart returns a timestamp at the start of the year which contains t (= yyyy-01-01 00:00:00)
func TimeToYearStart(t time.Time) time.Time { func TimeToYearStart(t time.Time, tz *time.Location) time.Time {
t = t.In(TimezoneBerlin) t = t.In(tz)
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location()) return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
} }
// TimeToYearEnd returns a timestamp at the end of the month which contains t (= yyyy-12-31 23:59:59.999999999) // TimeToYearEnd returns a timestamp at the end of the month which contains t (= yyyy-12-31 23:59:59.999999999)
func TimeToYearEnd(t time.Time) time.Time { func TimeToYearEnd(t time.Time, tz *time.Location) time.Time {
return TimeToYearStart(t).AddDate(1, 0, 0).Add(-1) return TimeToYearStart(t, tz).AddDate(1, 0, 0).Add(-1)
} }
// IsSameDayIncludingDateBoundaries returns true if t1 and t2 are part of the same day (TZ/Berlin), the boundaries of the day are // IsSameDayIncludingDateBoundaries returns true if t1 and t2 are part of the same day (TZ/Berlin), the boundaries of the day are
// inclusive, this means 2021-09-15T00:00:00 is still part of the day 2021-09-14 // inclusive, this means 2021-09-15T00:00:00 is still part of the day 2021-09-14
func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time) bool { func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time, tz *time.Location) bool {
dp1 := TimeToDatePart(t1) dp1 := TimeToDatePart(t1, tz)
dp2 := TimeToDatePart(t2) dp2 := TimeToDatePart(t2, tz)
if dp1.Equal(dp2) { if dp1.Equal(dp2) {
return true return true
@@ -72,9 +72,9 @@ func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time) bool {
} }
// IsDatePartEqual returns true if a and b have the same date part (`yyyy`, `MM` and `dd`) // IsDatePartEqual returns true if a and b have the same date part (`yyyy`, `MM` and `dd`)
func IsDatePartEqual(a time.Time, b time.Time) bool { func IsDatePartEqual(a time.Time, b time.Time, tz *time.Location) bool {
yy1, mm1, dd1 := a.In(TimezoneBerlin).Date() yy1, mm1, dd1 := a.In(tz).Date()
yy2, mm2, dd2 := b.In(TimezoneBerlin).Date() yy2, mm2, dd2 := b.In(tz).Date()
return yy1 == yy2 && mm1 == mm2 && dd1 == dd2 return yy1 == yy2 && mm1 == mm2 && dd1 == dd2
} }
@@ -82,7 +82,7 @@ func IsDatePartEqual(a time.Time, b time.Time) bool {
// WithTimePart returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from base // WithTimePart returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from base
// and the time (`HH`, `mm`, `ss`) from the parameter // and the time (`HH`, `mm`, `ss`) from the parameter
func WithTimePart(base time.Time, hour, minute, second int) time.Time { func WithTimePart(base time.Time, hour, minute, second int) time.Time {
datepart := TimeToDatePart(base) datepart := TimeToDatePart(base, base.Location())
delta := time.Duration(hour*int(time.Hour) + minute*int(time.Minute) + second*int(time.Second)) delta := time.Duration(hour*int(time.Hour) + minute*int(time.Minute) + second*int(time.Second))
@@ -92,7 +92,7 @@ func WithTimePart(base time.Time, hour, minute, second int) time.Time {
// CombineDateAndTime returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from the d parameter // CombineDateAndTime returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from the d parameter
// and the time (`HH`, `mm`, `ss`) from the t parameter // and the time (`HH`, `mm`, `ss`) from the t parameter
func CombineDateAndTime(d time.Time, t time.Time) time.Time { func CombineDateAndTime(d time.Time, t time.Time) time.Time {
datepart := TimeToDatePart(d) datepart := TimeToDatePart(d, d.Location())
delta := time.Duration(t.Hour()*int(time.Hour) + t.Minute()*int(time.Minute) + t.Second()*int(time.Second) + t.Nanosecond()*int(time.Nanosecond)) delta := time.Duration(t.Hour()*int(time.Hour) + t.Minute()*int(time.Minute) + t.Second()*int(time.Second) + t.Nanosecond()*int(time.Nanosecond))
@@ -100,8 +100,8 @@ func CombineDateAndTime(d time.Time, t time.Time) time.Time {
} }
// IsSunday returns true if t is a sunday (in TZ/Berlin) // IsSunday returns true if t is a sunday (in TZ/Berlin)
func IsSunday(t time.Time) bool { func IsSunday(t time.Time, tz *time.Location) bool {
if t.In(TimezoneBerlin).Weekday() == time.Sunday { if t.In(tz).Weekday() == time.Sunday {
return true return true
} }
return false return false
@@ -131,3 +131,7 @@ func UnixFloatSeconds(v float64) time.Time {
sec, dec := math.Modf(v) sec, dec := math.Modf(v)
return time.Unix(int64(sec), int64(dec*(1e9))) return time.Unix(int64(sec), int64(dec*(1e9)))
} }
func FloorTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}