fix(package): [datetime] count calendar days across DST (#385)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-22 10:48:53 +08:00
1 parent 5c04ffae97
commit 752f1ed63d
2 files changed
+21 -5

No files matched your search

+1 -5
View File
@@ -343,11 +343,7 @@ func BetweenSeconds(t1 time.Time, t2 time.Time) int64 {
// DayOfYear returns which day of the year the parameter date `t` is.
// Play: https://go.dev/play/p/0hjqhTwFNlH
func DayOfYear(t time.Time) int {
y, m, d := t.Date()
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t.Location())
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
return int(nowDate.Sub(firstDay).Hours() / 24)
return t.YearDay() - 1
}
// IsWeekend checks if passed time is weekend or not.
+20
View File
@@ -0,0 +1,20 @@
package datetime
import (
"testing"
"time"
_ "time/tzdata"
)
func TestDayOfYearDaylightSaving(t *testing.T) {
loc, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatal(err)
}
for _, month := range []time.Month{time.January, time.July, time.December} {
date := time.Date(2024, month, 1, 12, 0, 0, 0, loc)
if got, want := DayOfYear(date), date.YearDay()-1; got != want {
t.Errorf("%v: got %d, want %d", date, got, want)
}
}
}