diff options
| author | Grant Ammons <gammons@gmail.com> | 2017-04-12 08:54:19 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-04-12 08:54:19 -0400 |
| commit | e118736cf7df17dc10822fab36af7407c7e110a6 (patch) | |
| tree | 651dfc58c6c144bd1349dfe6a74c9aed477cf13f /todolist | |
| parent | 146f19e1dc8143096bca6bfd0adf2e1f8d9be7b7 (diff) | |
| parent | 8badae37312c70bc9ae68c7c6c0792033fb0dbd1 (diff) | |
Merge pull request #53 from ingorichter/ingo/completion-date
Add `CompletedDate` to todo item
Diffstat (limited to 'todolist')
| -rw-r--r-- | todolist/todo_item.go | 30 | ||||
| -rw-r--r-- | todolist/todo_list.go | 4 | ||||
| -rw-r--r-- | todolist/todo_test.go | 2 | ||||
| -rw-r--r-- | todolist/todos.json | 2 | ||||
| -rw-r--r-- | todolist/util.go | 8 |
5 files changed, 34 insertions, 12 deletions
diff --git a/todolist/todo_item.go b/todolist/todo_item.go index 273df8d..859e946 100644 --- a/todolist/todo_item.go +++ b/todolist/todo_item.go @@ -2,15 +2,19 @@ package todolist import "time" +// Timestamp format to include date, time with timezone support. Easy to parse +const ISO8601_TIMESTAMP_FORMAT = "2006-01-02T15:04:05Z07:00" + type Todo struct { - Id int `json:"id"` - Subject string `json:"subject"` - Projects []string `json:"projects"` - Contexts []string `json:"contexts"` - Due string `json:"due"` - Completed bool `json:"completed"` - Archived bool `json:"archived"` - IsPriority bool `json:"isPriority"` + Id int `json:"id"` + Subject string `json:"subject"` + Projects []string `json:"projects"` + Contexts []string `json:"contexts"` + Due string `json:"due"` + Completed bool `json:"completed"` + CompletedDate string `json:"completedDate"` + Archived bool `json:"archived"` + IsPriority bool `json:"isPriority"` } func NewTodo() *Todo { @@ -30,3 +34,13 @@ func (t Todo) CalculateDueTime() time.Time { return parsedTime } } + +func (t *Todo) Complete() { + t.Completed = true + t.CompletedDate = timestamp(time.Now()).Format(ISO8601_TIMESTAMP_FORMAT) +} + +func (t *Todo) Uncomplete() { + t.Completed = false + t.CompletedDate = "" +}
\ No newline at end of file diff --git a/todolist/todo_list.go b/todolist/todo_list.go index 19fe0b7..6bab407 100644 --- a/todolist/todo_list.go +++ b/todolist/todo_list.go @@ -28,14 +28,14 @@ func (t *TodoList) Delete(id int) { func (t *TodoList) Complete(id int) { todo := t.FindById(id) - todo.Completed = true + todo.Complete() t.Delete(id) t.Data = append(t.Data, todo) } func (t *TodoList) Uncomplete(id int) { todo := t.FindById(id) - todo.Completed = false + todo.Uncomplete() t.Delete(id) t.Data = append(t.Data, todo) } diff --git a/todolist/todo_test.go b/todolist/todo_test.go index aab2a74..c0069e6 100644 --- a/todolist/todo_test.go +++ b/todolist/todo_test.go @@ -5,7 +5,7 @@ import "testing" func TestNewTodo(t *testing.T) { todo := NewTodo() - if todo.Completed || todo.Archived { + if todo.Completed || todo.Archived || todo.CompletedDate != "" { t.Error("Completed should be false for new todos") } } diff --git a/todolist/todos.json b/todolist/todos.json index bc147d1..7f511ed 100644 --- a/todolist/todos.json +++ b/todolist/todos.json @@ -1 +1 @@ -[{"id":1,"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":false,"archived":true,"isPriority":false},{"id":2,"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":"","completed":true,"archived":false,"isPriority":false}]
\ No newline at end of file +[{"id":1,"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":false,"completedDate":"","archived":true,"isPriority":false},{"id":2,"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":"","completed":true,"completedDate":"","archived":false,"isPriority":false}]
\ No newline at end of file diff --git a/todolist/util.go b/todolist/util.go index 72d475f..34f9dd1 100644 --- a/todolist/util.go +++ b/todolist/util.go @@ -32,9 +32,17 @@ func AddTodoIfNotThere(arr []*Todo, item *Todo) []*Todo { 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 timestamp(t time.Time) time.Time { + year, month, day := t.Date() + hour, min, sec := t.Clock() + + return time.Date(year, month, day, hour, min, sec, 0, t.Location()) +} + func getNearestMonday(t time.Time) time.Time { for { if t.Weekday() != time.Monday { |
