From d2df602c25afe3193e2b4d434042b47de64a00e6 Mon Sep 17 00:00:00 2001 From: Ingo Richter Date: Sat, 8 Apr 2017 18:24:16 -0700 Subject: Add `CompletedDate` to todo item. `CompletedDate` will save a timestamp in ISO8601 format (Internet date/ time format to preserve timezone information) Added a new `Complete` and `Uncomplete` method for todo_item. Currently a todo has a boolean flag and the timestamp to indicate that the item is completed. The boolean flag could be kept for backward compatibility (reading the json into memory), but should be omitted for files being written. --- todolist/todo_item.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'todolist/todo_item.go') diff --git a/todolist/todo_item.go b/todolist/todo_item.go index 273df8d..c208f9f 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 = bod(time.Now()).Format(ISO8601_TIMESTAMP_FORMAT) +} + +func (t *Todo) Uncomplete() { + t.Completed = false + t.CompletedDate = "" +} \ No newline at end of file -- cgit v1.3 From 8badae37312c70bc9ae68c7c6c0792033fb0dbd1 Mon Sep 17 00:00:00 2001 From: Ingo Richter Date: Sat, 8 Apr 2017 18:58:45 -0700 Subject: fix build error with older go version --- todolist/todo_item.go | 2 +- todolist/util.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'todolist/todo_item.go') diff --git a/todolist/todo_item.go b/todolist/todo_item.go index c208f9f..859e946 100644 --- a/todolist/todo_item.go +++ b/todolist/todo_item.go @@ -37,7 +37,7 @@ func (t Todo) CalculateDueTime() time.Time { func (t *Todo) Complete() { t.Completed = true - t.CompletedDate = bod(time.Now()).Format(ISO8601_TIMESTAMP_FORMAT) + t.CompletedDate = timestamp(time.Now()).Format(ISO8601_TIMESTAMP_FORMAT) } func (t *Todo) Uncomplete() { diff --git a/todolist/util.go b/todolist/util.go index b87ed03..34f9dd1 100644 --- a/todolist/util.go +++ b/todolist/util.go @@ -32,6 +32,12 @@ 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()) -- cgit v1.3