-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpomo.go
65 lines (55 loc) · 1.07 KB
/
pomo.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
package pomo
import (
"time"
)
type Pomo struct {
Start time.Time `yaml:"start,omitempty"`
End time.Time `yaml:"end,omitempty"`
Tasks []Task `yaml:"tasks"`
}
func (p Pomo) MarshalYAML() (any, error) {
var start, end string
if !p.Start.IsZero() {
start = p.Start.Format(time.RFC3339Nano)
}
if !p.End.IsZero() {
end = p.End.Format(time.RFC3339Nano)
}
return pomoYaml{
Start: start,
End: end,
Tasks: p.Tasks,
}, nil
}
func (p *Pomo) UnmarshalYAML(unmarshal func(any) error) error {
var data pomoYaml
var err error
if err = unmarshal(&data); err != nil {
return err
}
start, err := parseTime(data.Start)
if err != nil {
return err
}
end, err := parseTime(data.End)
if err != nil {
return err
}
*p = Pomo{
Start: start,
End: end,
Tasks: data.Tasks,
}
return nil
}
type pomoYaml struct {
Start string `yaml:"start,omitempty"`
End string `yaml:"end,omitempty"`
Tasks []Task `yaml:"tasks,omitempty"`
}
func parseTime(s string) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}
return time.Parse(time.RFC3339Nano, s)
}