-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from lowply/fix-year-bug
Fix week numbering bug
- Loading branch information
Showing
6 changed files
with
96 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
go test -v . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/jinzhu/now" | ||
) | ||
|
||
type date struct { | ||
Year string | ||
WeekStartDate string | ||
WeekEndDate string | ||
WeekNumber string | ||
WeekNumberYear string | ||
Dates [7]string | ||
} | ||
|
||
func NewDate(t time.Time) *date { | ||
d := &date{} | ||
|
||
nc := &now.Config{ | ||
WeekStartDay: time.Monday, | ||
} | ||
n := nc.With(t) | ||
|
||
d.Year = n.BeginningOfYear().Format("2006") | ||
d.WeekEndDate = n.EndOfSunday().Format("01/02") | ||
d.WeekStartDate = n.BeginningOfWeek().Format("01/02") | ||
_, isoweek := n.Monday().ISOWeek() | ||
d.WeekNumber = fmt.Sprintf("%02d", isoweek) | ||
for j := range d.Dates { | ||
d.Dates[j] = n.Monday().AddDate(0, 0, j).Format("01/02") | ||
} | ||
// Thursday of the week, should be used with the week number | ||
// e.g. "2020 Week 01". | ||
// See https://en.wikipedia.org/wiki/ISO_week_date#First_week | ||
// for the ISO 8601 first week definition | ||
d.WeekNumberYear = n.BeginningOfWeek().AddDate(0, 0, 3).Format("2006") | ||
return d | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
type testCase struct { | ||
now string | ||
should string | ||
} | ||
|
||
func TestNewDate(t *testing.T) { | ||
testCases := []testCase{ | ||
// Monday when Jan 1st is Monday | ||
{now: "2018-01-01T00:00:00Z", should: "2018 Week 01"}, | ||
// Monday when Jan 1st is Tuesday | ||
{now: "2018-12-31T00:00:00Z", should: "2019 Week 01"}, | ||
// Monday when Jan 1st is Wednesday | ||
{now: "2019-12-30T00:00:00Z", should: "2020 Week 01"}, | ||
// Monday when Jan 1st is Thursday | ||
{now: "2025-12-29T00:00:00Z", should: "2026 Week 01"}, | ||
// Monday when Jan 1st is Friday | ||
{now: "2020-12-28T00:00:00Z", should: "2020 Week 53"}, | ||
// Monday when Jan 1st is Saturday | ||
{now: "2021-12-27T00:00:00Z", should: "2021 Week 52"}, | ||
// Monday when Jan 1st is Saturday and it's a leap year | ||
{now: "2032-12-27T00:00:00Z", should: "2032 Week 53"}, | ||
// Monday when Jan 1st is Sunday | ||
{now: "2022-12-26T00:00:00Z", should: "2022 Week 52"}, | ||
} | ||
|
||
for _, v := range testCases { | ||
t.Logf("Testing %v...", v.now) | ||
now, err := time.Parse(time.RFC3339, v.now) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
d := NewDate(now) | ||
current := d.WeekNumberYear + " Week " + d.WeekNumber | ||
if current != v.should { | ||
t.Errorf("Actual: %v, Should: %v\n", current, v.should) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters