fix: infinite loop caused by timezone weirdness in date range splitting (resolve #779)

This commit is contained in:
Ferdinand Mütsch
2025-04-30 14:39:48 +02:00
parent 06923b6313
commit f58653f1ba
3 changed files with 1069 additions and 1028 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -42,6 +42,11 @@ func SplitRangeByDays(from time.Time, to time.Time) [][]time.Time {
for t1 := from; t1.Before(to); {
t2 := datetime.BeginOfDay(t1).AddDate(0, 0, 1)
// https://github.com/muety/wakapi/issues/779
if t2 == t1 {
t1 = datetime.BeginOfDay(t1).Add(24 * time.Hour)
continue
}
if t2.After(to) {
t2 = to
}

View File

@@ -12,6 +12,7 @@ var (
tzUtc *time.Location
tzCet *time.Location
tzPst *time.Location
tzCch *time.Location
)
func init() {
@@ -19,6 +20,7 @@ func init() {
tzUtc, _ = time.LoadLocation("UTC")
tzCet, _ = time.LoadLocation("Europe/Berlin")
tzPst, _ = time.LoadLocation("America/Los_Angeles")
tzCch, _ = time.LoadLocation("America/Santiago")
}
func TestDate_SplitRangeByDays(t *testing.T) {
@@ -57,3 +59,11 @@ func TestDate_SplitRangeByDays(t *testing.T) {
assert.Len(t, result4, 0)
}
func TestDate_SplitRangeByDays_DSTBug(t *testing.T) {
// https://github.com/muety/wakapi/issues/779
df1 := time.Date(2024, time.April, 29, 0, 0, 0, 0, tzCch)
dt1 := time.Date(2025, time.April, 30, 2, 8, 25, 645879355, tzCch)
result1 := SplitRangeByDays(df1, dt1)
assert.Len(t, result1, 367)
}