aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-05-15 08:57:19 -0400
committerGrant Ammons <gammons@gmail.com>2016-05-15 08:57:19 -0400
commitf2b9dab056d46a0485e28a33da93e8dc7a1e8780 (patch)
treed9781ce9bf7d276287ce930abddfcf8d68e112c1
parent8fcd99c683048e546686ead649339b94ed011321 (diff)
Use the user's current timezone when parsing dates
This should remove ambiguity when comparing dates that are close or equal to each other, e.g. Sundays.
-rw-r--r--todolist/date_filter.go19
-rw-r--r--todolist/date_filter_test.go4
2 files changed, 12 insertions, 11 deletions
diff --git a/todolist/date_filter.go b/todolist/date_filter.go
index a502d62..c3d81fd 100644
--- a/todolist/date_filter.go
+++ b/todolist/date_filter.go
@@ -8,11 +8,12 @@ import (
)
type DateFilter struct {
- Todos []*Todo
+ Todos []*Todo
+ Location *time.Location
}
func NewDateFilter(todos []*Todo) *DateFilter {
- return &DateFilter{Todos: todos}
+ return &DateFilter{Todos: todos, Location: time.Now().Location()}
}
func (f *DateFilter) FilterDate(input string) []*Todo {
@@ -56,7 +57,7 @@ func (f *DateFilter) filterAgenda(pivot time.Time) []*Todo {
var ret []*Todo
for _, todo := range f.Todos {
- dueTime, _ := time.Parse("2006-01-02", todo.Due)
+ dueTime, _ := time.ParseInLocation("2006-01-02", todo.Due, f.Location)
if dueTime.Before(pivot) || todo.Due == pivot.Format("2006-01-02") {
ret = append(ret, todo)
}
@@ -78,7 +79,7 @@ func (f *DateFilter) filterDay(pivot time.Time, day time.Weekday) []*Todo {
var ret []*Todo
filtered := f.filterThisWeek(pivot)
for _, todo := range filtered {
- dueTime, _ := time.Parse("2006-01-02", todo.Due)
+ dueTime, _ := time.ParseInLocation("2006-01-02", todo.Due, f.Location)
if dueTime.Weekday() == day {
ret = append(ret, todo)
}
@@ -101,12 +102,12 @@ func (f *DateFilter) filterTomorrow(pivot time.Time) []*Todo {
func (f *DateFilter) filterThisWeek(pivot time.Time) []*Todo {
var ret []*Todo
- begin := f.findSunday(pivot)
+ begin := now.New(f.findSunday(pivot)).BeginningOfDay()
end := begin.AddDate(0, 0, 7)
for _, todo := range f.Todos {
- dueTime, _ := time.Parse("2006-01-02", todo.Due)
- if begin.Before(dueTime) && end.After(dueTime) {
+ dueTime, _ := time.ParseInLocation("2006-01-02", todo.Due, f.Location)
+ if (begin.Before(dueTime) || begin.Equal(dueTime)) && end.After(dueTime) {
ret = append(ret, todo)
}
}
@@ -120,7 +121,7 @@ func (f *DateFilter) filterNextWeek(pivot time.Time) []*Todo {
end := begin.AddDate(0, 0, 7)
for _, todo := range f.Todos {
- dueTime, _ := time.Parse("2006-01-02", todo.Due)
+ dueTime, _ := time.ParseInLocation("2006-01-02", todo.Due, f.Location)
if begin.Before(dueTime) && end.After(dueTime) {
ret = append(ret, todo)
}
@@ -134,7 +135,7 @@ func (f *DateFilter) filterOverdue(pivot time.Time) []*Todo {
pivotDate := pivot.Format("2006-01-02")
for _, todo := range f.Todos {
- dueTime, _ := time.Parse("2006-01-02", todo.Due)
+ dueTime, _ := time.ParseInLocation("2006-01-02", todo.Due, f.Location)
if dueTime.Before(pivot) && pivotDate != todo.Due {
ret = append(ret, todo)
}
diff --git a/todolist/date_filter_test.go b/todolist/date_filter_test.go
index e8855d5..c8b888b 100644
--- a/todolist/date_filter_test.go
+++ b/todolist/date_filter_test.go
@@ -81,8 +81,8 @@ func TestFilterDay(t *testing.T) {
assert := assert.New(t)
var todos []*Todo
- mondayTodo := &Todo{Id: 1, Subject: "one", Due: now.Monday().Format("2006-01-02")}
- tuesdayTodo := &Todo{Id: 2, Subject: "two", Due: now.Monday().AddDate(0, 0, 1).Format("2006-01-02")}
+ mondayTodo := &Todo{Id: 1, Subject: "one", Due: now.Sunday().AddDate(0, 0, 1).Format("2006-01-02")}
+ tuesdayTodo := &Todo{Id: 2, Subject: "two", Due: now.Sunday().AddDate(0, 0, 2).Format("2006-01-02")}
todos = append(todos, mondayTodo)
todos = append(todos, tuesdayTodo)