Skip to content

Instantly share code, notes, and snippets.

@kortschak
Created January 14, 2012 07:58
Show Gist options
  • Save kortschak/1610668 to your computer and use it in GitHub Desktop.
Save kortschak/1610668 to your computer and use it in GitHub Desktop.
Patch for DevTodo2 weekly.2011-12-01
From f5cc658536f716b5686a8cd158ad556b77a023c9 Mon Sep 17 00:00:00 2001
From: kortschak <dan.kortschak@adelaide.edu.au>
Date: Sat, 14 Jan 2012 18:00:49 +1030
Subject: [PATCH] time changes - weekly.2011-12-01
---
consoleview.go | 9 ++++-----
jsonio.go | 18 +++++++++---------
legacyio.go | 8 ++++----
main.go | 4 +++-
todo.go | 24 ++++++++++++------------
todo_test.go | 2 +-
view.go | 29 ++++++++++++++---------------
7 files changed, 47 insertions(+), 47 deletions(-)
diff --git a/consoleview.go b/consoleview.go
index 3543a50..2f821b3 100644
--- a/consoleview.go
+++ b/consoleview.go
@@ -75,12 +75,11 @@ func getTerminalWidth() int {
return int(ws.ws_col)
}
-
func taskState(task Task) int {
if task.Len() != 0 {
return '+'
}
- if task.CompletionTime() != nil {
+ if !task.CompletionTime().IsZero() {
return '-'
}
return ' '
@@ -118,7 +117,7 @@ func formatTask(width, depth int, task Task, options *ViewOptions) {
trimmed := false
if options.Summarise {
if len(text) > width {
- text = strings.TrimSpace(text[:width - 1])
+ text = strings.TrimSpace(text[:width-1])
trimmed = true
}
}
@@ -131,7 +130,7 @@ func formatTask(width, depth int, task Task, options *ViewOptions) {
}
func consoleDisplayTask(width, depth int, task Task, options *ViewOptions) {
- if depth >= 0 && (!options.ShowAll && task.CompletionTime() != nil) {
+ if depth >= 0 && (!options.ShowAll && !task.CompletionTime().IsZero()) {
return
}
if depth >= 0 {
@@ -145,7 +144,7 @@ func consoleDisplayTask(width, depth int, task Task, options *ViewOptions) {
}
}
-type ConsoleView struct {}
+type ConsoleView struct{}
func NewConsoleView() *ConsoleView {
return &ConsoleView{}
diff --git a/jsonio.go b/jsonio.go
index aca690c..9604715 100644
--- a/jsonio.go
+++ b/jsonio.go
@@ -76,18 +76,18 @@ func toMarshalableTask(n TaskNode) []*marshalableTask {
children := make([]*marshalableTask, n.Len())
for i := 0; i < n.Len(); i++ {
t := n.At(i)
- var created, completed int64 = 0, 0
- if t.CreationTime() != nil {
- created = t.CreationTime().Seconds()
+ var created, completed time.Time
+ if !t.CreationTime().IsZero() {
+ created = t.CreationTime()
}
- if t.CompletionTime() != nil {
- completed = t.CompletionTime().Seconds()
+ if !t.CompletionTime().IsZero() {
+ completed = t.CompletionTime()
}
children[i] = &marshalableTask{
Text: t.Text(),
Priority: t.Priority().String(),
- Creation: created,
- Completion: completed,
+ Creation: created.Unix(),
+ Completion: completed.Unix(),
Tasks: toMarshalableTask(t),
}
}
@@ -104,9 +104,9 @@ func fromMarshalableTaskList(l *marshalableTaskList) TaskList {
func fromMarshalableTask(node TaskNode, t []*marshalableTask) {
for _, j := range t {
task := node.Create(j.Text, PriorityFromString(j.Priority))
- task.SetCreationTime(time.SecondsToUTC(j.Creation))
+ task.SetCreationTime(time.Unix(j.Creation, 0).UTC())
if j.Completion != 0 {
- task.SetCompletionTime(time.SecondsToUTC(j.Completion))
+ task.SetCompletionTime(time.Unix(j.Completion, 0).UTC())
}
fromMarshalableTask(task, j.Tasks)
}
diff --git a/legacyio.go b/legacyio.go
index 264dbc3..dc182a9 100644
--- a/legacyio.go
+++ b/legacyio.go
@@ -55,11 +55,11 @@ func parseXmlNote(parent TaskNode, from []xmlNote) {
task := parent.Create(text, priority)
- created, _ := strconv.Atoi64(note.Time)
- completed, _ := strconv.Atoi64(note.Done)
- task.SetCreationTime(time.SecondsToUTC(created))
+ created, _ := strconv.ParseInt(note.Time, 10, 64)
+ completed, _ := strconv.ParseInt(note.Done, 10, 64)
+ task.SetCreationTime(time.Unix(created, 0).UTC())
if completed != 0 {
- task.SetCompletionTime(time.SecondsToUTC(completed))
+ task.SetCompletionTime(time.Unix(completed, 0).UTC())
}
parseXmlNote(task, note.Note)
}
diff --git a/main.go b/main.go
index 44ea017..7691385 100644
--- a/main.go
+++ b/main.go
@@ -22,6 +22,7 @@ import (
"os"
"strconv"
"strings"
+ "time"
)
// Actions
@@ -34,6 +35,7 @@ var reparentFlag = goopt.Flag([]string{"-R", "--reparent"}, nil, "reparent task
var titleFlag = goopt.Flag([]string{"--title"}, nil, "set the task list title", "")
var versionFlag = goopt.Flag([]string{"--version"}, nil, "show version", "")
var infoFlag = goopt.Flag([]string{"-i", "--info"}, nil, "show information on a task", "")
+
// Options
var priorityFlag = goopt.String([]string{"-p", "--priority"}, "medium", "priority of newly created tasks (veryhigh,high,medium,low,verylow)")
var graftFlag = goopt.String([]string{"-g", "--graft"}, "root", "task to graft new tasks to")
@@ -79,7 +81,7 @@ func doMarkDone(tasks TaskList, references []Task) {
func doMarkNotDone(tasks TaskList, references []Task) {
for _, task := range references {
- task.SetCompletionTime(nil)
+ task.SetCompletionTime(time.Time{})
}
saveTaskList(tasks)
}
diff --git a/todo.go b/todo.go
index 073881d..a4d897d 100644
--- a/todo.go
+++ b/todo.go
@@ -72,12 +72,12 @@ type Task interface {
Priority() Priority
SetPriority(priority Priority)
- SetCreationTime(time *time.Time)
- CreationTime() *time.Time
+ SetCreationTime(time time.Time)
+ CreationTime() time.Time
SetCompleted()
- SetCompletionTime(time *time.Time)
- CompletionTime() *time.Time
+ SetCompletionTime(time time.Time)
+ CompletionTime() time.Time
}
type TaskList interface {
@@ -233,7 +233,7 @@ type taskImpl struct {
*taskNodeImpl
text string
priority Priority
- created, completed *time.Time
+ created, completed time.Time
}
func newTask(id int, text string, priority Priority) Task {
@@ -241,8 +241,8 @@ func newTask(id int, text string, priority Priority) Task {
taskNodeImpl: newTaskNode(id),
text: text,
priority: priority,
- created: time.UTC(),
- completed: nil,
+ created: time.Now().UTC(),
+ completed: time.Time{},
}
}
@@ -250,23 +250,23 @@ func (self *taskImpl) Id() int {
return self.id
}
-func (self *taskImpl) SetCreationTime(time *time.Time) {
+func (self *taskImpl) SetCreationTime(time time.Time) {
self.created = time
}
-func (self *taskImpl) CreationTime() *time.Time {
+func (self *taskImpl) CreationTime() time.Time {
return self.created
}
func (self *taskImpl) SetCompleted() {
- self.SetCompletionTime(time.UTC())
+ self.SetCompletionTime(time.Now().UTC())
}
-func (self *taskImpl) SetCompletionTime(time *time.Time) {
+func (self *taskImpl) SetCompletionTime(time time.Time) {
self.completed = time
}
-func (self *taskImpl) CompletionTime() *time.Time {
+func (self *taskImpl) CompletionTime() time.Time {
return self.completed
}
diff --git a/todo_test.go b/todo_test.go
index d4c8052..d4979fc 100644
--- a/todo_test.go
+++ b/todo_test.go
@@ -26,7 +26,7 @@ func TestFind(t *testing.T) {
b := tasks.Create("do B", MEDIUM)
b.Create("do C", MEDIUM)
b.Create("do D", MEDIUM)
-
+
task := tasks.Find("2.2")
if task == nil || task.Text() != "do D" {
t.Fail()
diff --git a/view.go b/view.go
index a181357..72a03fc 100644
--- a/view.go
+++ b/view.go
@@ -18,13 +18,14 @@ package main
import (
"sort"
+ "time"
)
type ViewOptions struct {
- Order Order
- Reversed bool
+ Order Order
+ Reversed bool
Summarise bool
- ShowAll bool
+ ShowAll bool
}
type View interface {
@@ -34,13 +35,13 @@ type View interface {
// A filtered, ordered view of a Tasks children.
type TaskView struct {
- tasks []Task
+ tasks []Task
options *ViewOptions
}
func CreateTaskView(node TaskNode, options *ViewOptions) *TaskView {
view := &TaskView{
- tasks: make([]Task, node.Len()),
+ tasks: make([]Task, node.Len()),
options: options,
}
for i := 0; i < node.Len(); i++ {
@@ -64,30 +65,30 @@ func (self *TaskView) Less(i, j int) bool {
less := false
switch self.options.Order {
case CREATED:
- less = left.CreationTime().Seconds() < right.CreationTime().Seconds()
+ less = left.CreationTime().Before(right.CreationTime())
case COMPLETED:
- less = left.CompletionTime().Seconds() < right.CompletionTime().Seconds()
+ less = left.CompletionTime().Before(right.CompletionTime())
case TEXT:
less = left.Text() < right.Text()
case PRIORITY:
less = left.Priority() < right.Priority()
case DURATION:
- var leftDuration, rightDuration int64
+ var leftDuration, rightDuration time.Duration
leftCompletion := left.CompletionTime()
rightCompletion := right.CompletionTime()
- if leftCompletion != nil {
- leftDuration = leftCompletion.Seconds() - left.CreationTime().Seconds()
+ if !leftCompletion.IsZero() {
+ leftDuration = leftCompletion.Sub(left.CreationTime())
} else {
leftDuration = 0
}
- if rightCompletion != nil {
- rightDuration = rightCompletion.Seconds() - right.CreationTime().Seconds()
+ if !rightCompletion.IsZero() {
+ rightDuration = rightCompletion.Sub(right.CreationTime())
} else {
rightDuration = 0
}
less = leftDuration < rightDuration
case DONE:
- less = left.CompletionTime() != nil && right.CompletionTime() == nil
+ less = !left.CompletionTime().IsZero() && right.CompletionTime().IsZero()
default:
panic("invalid ordering")
}
@@ -100,5 +101,3 @@ func (self *TaskView) Less(i, j int) bool {
func (self *TaskView) Swap(i, j int) {
self.tasks[j], self.tasks[i] = self.tasks[i], self.tasks[j]
}
-
-
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment