Skip to content

Commit

Permalink
Merge pull request #9 from lowply/update-week-start-date
Browse files Browse the repository at this point in the history
Update week start date
  • Loading branch information
lowply authored Jan 6, 2020
2 parents bfeef74 + 99451c4 commit e100388
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
---
Expand Down
17 changes: 10 additions & 7 deletions src/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package main

import (
"fmt"
"strconv"
"time"

"github.com/jinzhu/now"
)

type date struct {
Year string
Month string
Day string
WeekStartDate string
WeekEndDate string
WeekNumber string
WeekNumberYear string
Dates [7]string
Expand All @@ -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
}
21 changes: 12 additions & 9 deletions src/date_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"testing"
"time"
)
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit e100388

Please sign in to comment.