Skip to content

Commit

Permalink
Add IsLeapYear method
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Jan 25, 2024
1 parent 9b0a1ec commit 9ed8c4a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions calendar/gregorian.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,16 @@ func (g Gregorian) String() string {
func (g Gregorian) IsZero() bool {
return g.Time.IsZero()
}

// IsLeapYear reports whether is a leap year.
// 是否是闰年
func (g Gregorian) IsLeapYear() bool {
if g.IsZero() {
return false
}
year := g.Year()
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
return false
}
52 changes: 52 additions & 0 deletions calendar/gregorian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,55 @@ func TestGregorian_Clock(t *testing.T) {
})
}
}

func TestGregorian_IsZero(t *testing.T) {
type args struct {
g Gregorian
}
tests := []struct {
args args
want bool
}{
{
args: args{NewGregorian(time.Time{})},
want: true,
},
{
args: args{NewGregorian(time.Date(2020, 8, 5, 13, 14, 15, 0, time.Local))},
want: false,
},
}
for index, tt := range tests {
t.Run(strconv.Itoa(index), func(t *testing.T) {
assert.Equalf(t, tt.want, tt.args.g.IsZero(), "args(%v)", tt.args.g)
})
}
}

func TestGregorian_IsLeapYear(t *testing.T) {
type args struct {
g Gregorian
}
tests := []struct {
args args
want bool
}{
{
args: args{NewGregorian(time.Time{})},
want: false,
},
{
args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))},
want: true,
},
{
args: args{NewGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))},
want: false,
},
}
for index, tt := range tests {
t.Run(strconv.Itoa(index), func(t *testing.T) {
assert.Equalf(t, tt.want, tt.args.g.IsLeapYear(), "args(%v)", tt.args.g)
})
}
}

0 comments on commit 9ed8c4a

Please # to comment.