diff options
Diffstat (limited to 'todolist')
| -rw-r--r-- | todolist/date_filter.go | 32 | ||||
| -rw-r--r-- | todolist/date_filter_test.go | 5 | ||||
| -rw-r--r-- | todolist/parser.go | 24 | ||||
| -rw-r--r-- | todolist/parser_test.go | 9 | ||||
| -rw-r--r-- | todolist/util.go | 17 |
5 files changed, 49 insertions, 38 deletions
diff --git a/todolist/date_filter.go b/todolist/date_filter.go index 2bf51e2..7895000 100644 --- a/todolist/date_filter.go +++ b/todolist/date_filter.go @@ -3,8 +3,6 @@ package todolist import ( "regexp" "time" - - "github.com/jinzhu/now" ) type DateFilter struct { @@ -19,36 +17,36 @@ func NewDateFilter(todos []*Todo) *DateFilter { func (f *DateFilter) FilterDate(input string) []*Todo { agendaRegex, _ := regexp.Compile(`agenda .*$`) if agendaRegex.MatchString(input) { - return f.filterAgenda(now.BeginningOfDay()) + return f.filterAgenda(bod(time.Now())) } r, _ := regexp.Compile(`due .*$`) match := r.FindString(input) switch { case match == "due tod" || match == "due today": - return f.filterToday(now.BeginningOfDay()) + return f.filterToday(bod(time.Now())) case match == "due tom" || match == "due tomorrow": - return f.filterTomorrow(now.BeginningOfDay()) + return f.filterTomorrow(bod(time.Now())) case match == "due sun" || match == "due sunday": - return f.filterDay(now.BeginningOfDay(), time.Sunday) + return f.filterDay(bod(time.Now()), time.Sunday) case match == "due mon" || match == "due monday": - return f.filterDay(now.BeginningOfDay(), time.Monday) + return f.filterDay(bod(time.Now()), time.Monday) case match == "due tue" || match == "due tuesday": - return f.filterDay(now.BeginningOfDay(), time.Tuesday) + return f.filterDay(bod(time.Now()), time.Tuesday) case match == "due wed" || match == "due wednesday": - return f.filterDay(now.BeginningOfDay(), time.Wednesday) + return f.filterDay(bod(time.Now()), time.Wednesday) case match == "due thu" || match == "due thursday": - return f.filterDay(now.BeginningOfDay(), time.Thursday) + return f.filterDay(bod(time.Now()), time.Thursday) case match == "due fri" || match == "due friday": - return f.filterDay(now.BeginningOfDay(), time.Friday) + return f.filterDay(bod(time.Now()), time.Friday) case match == "due sat" || match == "due saturday": - return f.filterDay(now.BeginningOfDay(), time.Saturday) + return f.filterDay(bod(time.Now()), time.Saturday) case match == "due this week": - return f.filterThisWeek(now.BeginningOfDay()) + return f.filterThisWeek(bod(time.Now())) case match == "due next week": - return f.filterNextWeek(now.BeginningOfDay()) + return f.filterNextWeek(bod(time.Now())) case match == "overdue": - return f.filterOverdue(now.BeginningOfDay()) + return f.filterOverdue(bod(time.Now())) } return f.Todos } @@ -102,7 +100,7 @@ func (f *DateFilter) filterTomorrow(pivot time.Time) []*Todo { func (f *DateFilter) filterThisWeek(pivot time.Time) []*Todo { var ret []*Todo - begin := now.New(f.FindSunday(pivot)).BeginningOfDay() + begin := bod(f.FindSunday(pivot)) end := begin.AddDate(0, 0, 7) for _, todo := range f.Todos { @@ -144,7 +142,7 @@ func (f *DateFilter) filterOverdue(pivot time.Time) []*Todo { } func (f *DateFilter) FindSunday(pivot time.Time) time.Time { - switch now.New(pivot).Weekday() { + switch pivot.Weekday() { case time.Sunday: return pivot case time.Monday: diff --git a/todolist/date_filter_test.go b/todolist/date_filter_test.go index 8ee8db5..444b4ac 100644 --- a/todolist/date_filter_test.go +++ b/todolist/date_filter_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/jinzhu/now" "github.com/stretchr/testify/assert" ) @@ -63,7 +62,7 @@ func TestFilterOverdue(t *testing.T) { var todos []*Todo lastWeekTodo := &Todo{Id: 1, Subject: "one", Due: time.Now().AddDate(0, 0, -7).Format("2006-01-02")} - todayTodo := &Todo{Id: 2, Subject: "two", Due: now.BeginningOfDay().Format("2006-01-02")} + todayTodo := &Todo{Id: 2, Subject: "two", Due: bod(time.Now()).Format("2006-01-02")} tomorrowTodo := &Todo{Id: 3, Subject: "three", Due: time.Now().AddDate(0, 0, 1).Format("2006-01-02")} todos = append(todos, lastWeekTodo) @@ -71,7 +70,7 @@ func TestFilterOverdue(t *testing.T) { todos = append(todos, tomorrowTodo) filter := NewDateFilter(todos) - filtered := filter.filterOverdue(now.BeginningOfDay()) + filtered := filter.filterOverdue(bod(time.Now())) assert.Equal(1, len(filtered)) assert.Equal(1, filtered[0].Id) diff --git a/todolist/parser.go b/todolist/parser.go index 6cf1967..24a4016 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -7,8 +7,6 @@ import ( "strconv" "strings" "time" - - "github.com/jinzhu/now" ) type Parser struct{} @@ -79,9 +77,9 @@ func (p *Parser) Due(input string, day time.Time) string { case "none": return "" case "today", "tod": - return now.BeginningOfDay().Format("2006-01-02") + return bod(time.Now()).Format("2006-01-02") case "tomorrow", "tom": - return now.BeginningOfDay().AddDate(0, 0, 1).Format("2006-01-02") + return bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") case "monday", "mon": return p.monday(day) case "tuesday", "tue": @@ -97,8 +95,8 @@ func (p *Parser) Due(input string, day time.Time) string { case "sunday", "sun": return p.sunday(day) case "next week": - n := now.BeginningOfDay() - return now.New(n).Monday().AddDate(0, 0, 7).Format("2006-01-02") + n := bod(time.Now()) + return getNearestMonday(n).AddDate(0, 0, 7).Format("2006-01-02") } return p.parseArbitraryDate(res, time.Now()) } @@ -137,37 +135,37 @@ func (p *Parser) parseArbitraryDateWithYear(_date string, year int) time.Time { } func (p *Parser) monday(day time.Time) string { - mon := now.New(day).Monday() + mon := getNearestMonday(day) return p.thisOrNextWeek(mon, day) } func (p *Parser) tuesday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 1) + tue := getNearestMonday(day).AddDate(0, 0, 1) return p.thisOrNextWeek(tue, day) } func (p *Parser) wednesday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 2) + tue := getNearestMonday(day).AddDate(0, 0, 2) return p.thisOrNextWeek(tue, day) } func (p *Parser) thursday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 3) + tue := getNearestMonday(day).AddDate(0, 0, 3) return p.thisOrNextWeek(tue, day) } func (p *Parser) friday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 4) + tue := getNearestMonday(day).AddDate(0, 0, 4) return p.thisOrNextWeek(tue, day) } func (p *Parser) saturday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 5) + tue := getNearestMonday(day).AddDate(0, 0, 5) return p.thisOrNextWeek(tue, day) } func (p *Parser) sunday(day time.Time) string { - tue := now.New(day).Monday().AddDate(0, 0, 6) + tue := getNearestMonday(day).AddDate(0, 0, 6) return p.thisOrNextWeek(tue, day) } diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 791958c..cbb94e8 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -6,7 +6,6 @@ import ( "testing" "time" - "github.com/jinzhu/now" "github.com/stretchr/testify/assert" ) @@ -75,11 +74,11 @@ func TestParseContexts(t *testing.T) { func TestDueToday(t *testing.T) { parser := &Parser{} todo := parser.ParseNewTodo("do this thing with @bob and @mary due today") - if todo.Due != now.BeginningOfDay().Format("2006-01-02") { + if todo.Due != bod(time.Now()).Format("2006-01-02") { fmt.Println("Date is different", todo.Due, time.Now()) } todo = parser.ParseNewTodo("do this thing with @bob and @mary due tod") - if todo.Due != now.BeginningOfDay().Format("2006-01-02") { + if todo.Due != bod(time.Now()).Format("2006-01-02") { fmt.Println("Date is different", todo.Due, time.Now()) } } @@ -87,11 +86,11 @@ func TestDueToday(t *testing.T) { func TestDueTomorrow(t *testing.T) { parser := &Parser{} todo := parser.ParseNewTodo("do this thing with @bob and @mary due tomorrow") - if todo.Due != now.BeginningOfDay().AddDate(0, 0, 1).Format("2006-01-02") { + if todo.Due != bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") { fmt.Println("Date is different", todo.Due, time.Now()) } todo = parser.ParseNewTodo("do this thing with @bob and @mary due tom") - if todo.Due != now.BeginningOfDay().AddDate(0, 0, 1).Format("2006-01-02") { + if todo.Due != bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") { fmt.Println("Date is different", todo.Due, time.Now()) } } diff --git a/todolist/util.go b/todolist/util.go index bbed216..72d475f 100644 --- a/todolist/util.go +++ b/todolist/util.go @@ -1,5 +1,7 @@ package todolist +import "time" + func AddIfNotThere(arr []string, items []string) []string { for _, item := range items { there := false @@ -27,3 +29,18 @@ func AddTodoIfNotThere(arr []*Todo, item *Todo) []*Todo { } return arr } + +func bod(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) +} + +func getNearestMonday(t time.Time) time.Time { + for { + if t.Weekday() != time.Monday { + t = t.AddDate(0, 0, -1) + } else { + return t + } + } +} |
