-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
83 lines (68 loc) · 1.36 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package clocked
import (
"fmt"
"strings"
"time"
)
type Task struct {
Code string `yaml:"code"`
Title string `yaml:"title"`
Tags []string `yaml:"tags"`
Bookings []Booking `yaml:"bookings"`
}
func (t *Task) HasTag(tag string) bool {
for _, tg := range t.Tags {
if tg == tag {
return true
}
}
return false
}
func (t *Task) Start(tm time.Time) error {
b := Booking{}
b.SetStart(tm)
t.Bookings = append(t.Bookings, b)
return nil
}
func (t *Task) Stop(tm time.Time) error {
b := &t.Bookings[len(t.Bookings)-1]
b.SetStop(tm)
return nil
}
func (t Task) Label() string {
return fmt.Sprintf("%s %s", t.Code, t.Title)
}
type Booking struct {
Start string `yaml:"start"`
Stop string `yaml:"stop"`
}
func (b *Booking) SetStart(t time.Time) {
b.Start = t.Format(time.RFC3339)
}
func (b *Booking) SetStop(t time.Time) {
b.Stop = t.Format(time.RFC3339)
}
func (b *Booking) StartTime() *time.Time {
if b.Start == "" {
return nil
}
t, _ := time.Parse(time.RFC3339, b.Start)
return &t
}
func (b *Booking) StopTime() *time.Time {
if b.Stop == "" {
return nil
}
t, _ := time.Parse(time.RFC3339, b.Stop)
return &t
}
type ByCode []Task
func (l ByCode) Len() int {
return len(l)
}
func (l ByCode) Less(i int, j int) bool {
return strings.Compare(l[i].Code, l[j].Code) < 0
}
func (l ByCode) Swap(i int, j int) {
l[i], l[j] = l[j], l[i]
}