mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [datetime] count calendar days across DST (#385)
This commit is contained in:
1 parent
5c04ffae97
commit
752f1ed63d
2 files changed
+21
-5
No files matched your search
@@ -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.
|
// DayOfYear returns which day of the year the parameter date `t` is.
|
||||||
// Play: https://go.dev/play/p/0hjqhTwFNlH
|
// Play: https://go.dev/play/p/0hjqhTwFNlH
|
||||||
func DayOfYear(t time.Time) int {
|
func DayOfYear(t time.Time) int {
|
||||||
y, m, d := t.Date()
|
return t.YearDay() - 1
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsWeekend checks if passed time is weekend or not.
|
// IsWeekend checks if passed time is weekend or not.
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user