Skip to content

Commit

Permalink
(feat) Add function to get total number of days in a BS year (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mesaugat authored and srishanbhattarai committed Apr 4, 2019
1 parent 9d93062 commit 483c356
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dateconv/dateconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package dateconv

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -72,6 +73,22 @@ func BsDaysInMonthsByYear(yy int, mm time.Month) (int, bool) {
return months[query], true
}

// TotalDaysInBSYear returns total number of days in a particular BS year.
func TotalDaysInBSYear(bsYear int) (int, error) {
days, ok := bsDaysInMonthsByYear[bsYear]

if !ok {
return -1, fmt.Errorf("Year should be in between %d and %d", bsLBound, bsUBound)
}

sum := 0
for _, value := range days {
sum += value
}

return sum, nil
}

// toTime creates a new time.Time with the basic yy/mm/dd parameters.
func toTime(yy, mm, dd int) time.Time {
return time.Date(yy, time.Month(mm), dd, 0, 0, 0, 0, time.UTC)
Expand Down
28 changes: 28 additions & 0 deletions dateconv/dateconv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dateconv

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -175,3 +176,30 @@ func TestBsDaysInMonthsByYear(t *testing.T) {
})
}
}

func TestTotalDaysInBSYear(t *testing.T) {
tests := []struct {
name string
year int
expected int
}{
{"returns total number of days in 2001 BS", 2001, 365},
{"returns total number of days in 2077 BS", 2077, 366},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
total, err := TotalDaysInBSYear(test.year)

assert.Equal(t, test.expected, total, err)
})
}

t.Run("errors if BS year is out of range", func(t *testing.T) {
_, err := TotalDaysInBSYear(3050)

if assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("Year should be in between %d and %d", bsLBound, bsUBound), err)
}
})
}

0 comments on commit 483c356

Please sign in to comment.