-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
69 lines (54 loc) · 1.25 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package togo
import (
"time"
)
type Priority int32
const (
None Priority = 0
Low = 1
Medium = 2
High = 3
)
type Task struct {
Name string
Description string
Priority Priority
Created time.Time
Completed *time.Time
DueDate *time.Time
}
func NewTask(name, description string) Task {
return Task{Name: name, Description: description, Created: time.Now()}
}
func (t *Task) IsCompleted() bool {
return t.Completed != nil && t.Completed.Before(time.Now())
}
func (t *Task) Overdue() bool {
return t.DueDate != nil && t.DueDate.Before(time.Now())
}
func (t *Task) Complete() {
completionTime := time.Now()
t.Completed = &completionTime
}
func (t *Task) CompletionDate() *time.Time {
return t.Completed
}
// AddDueDate strips off the time component and stores the result as the due date
func (t *Task) AddDueDate(due time.Time) {
yyyy, mm, dd := due.Date()
newDate := time.Date(yyyy, mm, dd, 0, 0, 0, 0, time.UTC)
t.DueDate = &newDate
}
func (t *Task) DueOn() *time.Time {
return t.DueDate
}
type Tasks []Task
func (ts Tasks) Len() int {
return len(ts)
}
func (ts Tasks) Less(i, j int) bool {
return ts[i].Priority < ts[j].Priority
}
func (ts Tasks) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}