21
0
Fork 0

v0.0.424 timeext.SubtractYears
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m29s Details

This commit is contained in:
Mike Schwörer 2024-03-30 03:01:55 +01:00
parent 6c4af4006b
commit 9491b72b8d
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.423"
const GoextVersion = "0.0.424"
const GoextVersionTimestamp = "2024-03-24T15:25:52+0100"
const GoextVersionTimestamp = "2024-03-30T03:01:55+0100"

View File

@ -146,3 +146,37 @@ func UnixFloatSeconds(v float64) time.Time {
func FloorTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
func SubtractYears(t time.Time, yearCount float64, tz *time.Location) time.Time {
t = t.In(tz)
if yearCount < 0 {
return AddYears(t, -yearCount, tz)
}
intCount, floatCount := math.Modf(yearCount)
t.AddDate(-int(intCount), 0, 0)
t0 := TimeToYearStart(t, tz)
t1 := TimeToYearEnd(t, tz)
return t.Add(time.Duration(float64(t1.Sub(t0)) * floatCount * -1))
}
func AddYears(t time.Time, yearCount float64, tz *time.Location) time.Time {
t = t.In(tz)
if yearCount < 0 {
return SubtractYears(t, -yearCount, tz)
}
intCount, floatCount := math.Modf(yearCount)
t.AddDate(int(intCount), 0, 0)
t0 := TimeToYearStart(t, tz)
t1 := TimeToYearEnd(t, tz)
return t.Add(time.Duration(float64(t1.Sub(t0)) * floatCount))
}