diff --git a/README.md b/README.md index b0b5e22..7655885 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,10 @@ This action opens a new issue from an issue template. It parses the template's f ## Available template variables -- `.Year`: Year of the week -- `.WeekStartDate`: Date of Monday of the week -- `.WeekEndDate`: Date of Sunday of the week +- `.Year`: Year of the day when this action runs +- `.Month`: Month of the day when this action runs +- `.Day`: Day when this action runs +- `.WeekStartDate`: Date of Monday of the week (YYYY/MM/DD) - `.WeekNumber`: ISO week number - `.WeekNumberYear`: Year of the Thursday of the week. Matches with [ISO week number](https://en.wikipedia.org/wiki/ISO_week_date#First_week) - `.Dates`: Array of the dates of the week (Can be used as `{{ index .Dates 1 }}` in the template) @@ -21,7 +22,7 @@ This action opens a new issue from an issue template. It parses the template's f --- name: Weekly Report about: This is an example -title: 'Report on {{ .WeekStartDate }} (Week {{ .WeekNumber }}, {{ .WeekNumberYear }})' +title: 'Report for Week {{ .WeekNumber }}, {{ .WeekNumberYear }} (Week of {{ .WeekStartDate }})' labels: report assignees: lowply --- diff --git a/src/date.go b/src/date.go index 02a1ca1..fc12d7b 100644 --- a/src/date.go +++ b/src/date.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strconv" "time" "github.com/jinzhu/now" @@ -9,8 +10,9 @@ import ( type date struct { Year string + Month string + Day string WeekStartDate string - WeekEndDate string WeekNumber string WeekNumberYear string Dates [7]string @@ -24,18 +26,19 @@ func NewDate(t time.Time) *date { } n := nc.With(t) - d.Year = n.BeginningOfYear().Format("2006") - d.WeekEndDate = n.EndOfSunday().Format("01/02") - d.WeekStartDate = n.BeginningOfWeek().Format("01/02") + d.Year = strconv.Itoa(n.Year()) + d.Month = fmt.Sprintf("%02d", int(n.Month())) + d.Day = fmt.Sprintf("%02d", n.Day()) + d.WeekStartDate = n.BeginningOfWeek().Format("2006/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") + for j := range d.Dates { + d.Dates[j] = n.Monday().AddDate(0, 0, j).Format("01/02") + } return d } diff --git a/src/date_test.go b/src/date_test.go index 12eba42..a99cee6 100644 --- a/src/date_test.go +++ b/src/date_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "testing" "time" ) @@ -13,21 +14,23 @@ type testCase struct { func TestNewDate(t *testing.T) { testCases := []testCase{ // Monday when Jan 1st is Monday - {now: "2018-01-01T00:00:00Z", should: "2018 Week 01"}, + {now: "2018-01-01T00:00:00Z", should: "2018 Week 01, Week of 2018/01/01."}, // Monday when Jan 1st is Tuesday - {now: "2018-12-31T00:00:00Z", should: "2019 Week 01"}, + {now: "2018-12-31T00:00:00Z", should: "2019 Week 01, Week of 2018/12/31."}, // Monday when Jan 1st is Wednesday - {now: "2019-12-30T00:00:00Z", should: "2020 Week 01"}, + {now: "2019-12-30T00:00:00Z", should: "2020 Week 01, Week of 2019/12/30."}, // Monday when Jan 1st is Thursday - {now: "2025-12-29T00:00:00Z", should: "2026 Week 01"}, + {now: "2025-12-29T00:00:00Z", should: "2026 Week 01, Week of 2025/12/29."}, // Monday when Jan 1st is Friday - {now: "2020-12-28T00:00:00Z", should: "2020 Week 53"}, + {now: "2020-12-28T00:00:00Z", should: "2020 Week 53, Week of 2020/12/28."}, // Monday when Jan 1st is Saturday - {now: "2021-12-27T00:00:00Z", should: "2021 Week 52"}, + {now: "2021-12-27T00:00:00Z", should: "2021 Week 52, Week of 2021/12/27."}, // Monday when Jan 1st is Saturday and it's a leap year - {now: "2032-12-27T00:00:00Z", should: "2032 Week 53"}, + {now: "2032-12-27T00:00:00Z", should: "2032 Week 53, Week of 2032/12/27."}, // Monday when Jan 1st is Sunday - {now: "2022-12-26T00:00:00Z", should: "2022 Week 52"}, + {now: "2022-12-26T00:00:00Z", should: "2022 Week 52, Week of 2022/12/26."}, + // Wednesday when Jan 1st is Wednesday + {now: "2020-01-01T00:00:00Z", should: "2020 Week 01, Week of 2019/12/30."}, } for _, v := range testCases { @@ -37,7 +40,7 @@ func TestNewDate(t *testing.T) { t.Fatal(err) } d := NewDate(now) - current := d.WeekNumberYear + " Week " + d.WeekNumber + current := fmt.Sprintf("%v Week %v, Week of %v.", d.WeekNumberYear, d.WeekNumber, d.WeekStartDate) if current != v.should { t.Errorf("Actual: %v, Should: %v\n", current, v.should) }