Skip to content

Commit

Permalink
fix NewLocalDate
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Nov 20, 2024
1 parent 751cb18 commit 60af11e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go/common/xtime/localdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ type LocalDate int64

func NewLocalDate(t time.Time) LocalDate {
year1 := 1970
year2 := t.Year() - 1 // exclude the current year for leap year calculations
year2 := t.Year()
mul := 1

if year1 > year2 {
year1, year2 = year2, year1
mul = -1
}

totalYears := year2 - year1 + 1
totalYears := year2 - year1
year2 -= 1 // exclude the current year for leap year calculations
leapYears1 := year1/4 - year1/100 + year1/400
leapYears2 := year2/4 - year2/100 + year2/400
leapYears := leapYears2 - leapYears1
Expand Down
4 changes: 4 additions & 0 deletions go/common/xtime/localdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func TestNewLocalDate(t *testing.T) {
assert.Equal(t, LocalDate(0), NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.FixedZone("", -60*60*14))))
assert.Equal(t, LocalDate(0), NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)))
assert.Equal(t, LocalDate(0), NewLocalDate(time.Date(1970, time.January, 1, 23, 59, 59, 0, time.FixedZone("", 60*60*14))))

assert.Equal(t, LocalDate(-365), NewLocalDate(time.Date(1969, time.January, 1, 0, 0, 0, 0, time.UTC)))
assert.Equal(t, LocalDate(365), NewLocalDate(time.Date(1971, time.January, 1, 0, 0, 0, 0, time.UTC)))
assert.Equal(t, LocalDate(165), NewLocalDate(time.Date(1970, time.June, 15, 0, 0, 0, 0, time.UTC)))
}

func TestParseLocalDate(t *testing.T) {
Expand Down

0 comments on commit 60af11e

Please sign in to comment.