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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package main
import (
"errors"
"log"
"regexp"
"time"
)
type Entry struct {
Id int
Date Date
From *Time
To *Time
Tasks map[int]*time.Duration
Comment string
}
type Task struct {
Id int
Group *string
Ident *string
Text string
}
type Date string
type Time string
type EntrySubDuration string
var (
timeMatch = regexp.MustCompile("[0-9]{2}:[0-9]{2}")
dateMatch = regexp.MustCompile("[0-9]{4}-[0-9]{2}-[0-9]{2}")
subDurationMatch = regexp.MustCompile("[0-9]")
)
var (
ErrInvalidTime = errors.New("Invalid time string")
ErrInvalidDate = errors.New("Invalid date string")
)
func NewEntry(date Date) *Entry {
return &Entry{
Id: -1,
From: nil,
Date: date,
To: nil,
Tasks: map[int]*time.Duration{},
Comment: "",
}
}
func NewTask() *Task {
return &Task{
Id: -1,
Group: nil,
Ident: nil,
Text: "",
}
}
func TimeFromStd(time time.Time) Time {
return Time(time.Format("15:04"))
}
func TimeFromString(str string) (*Time, error) {
if str == "" {
return nil, nil
}
matched := timeMatch.MatchString(str)
if !matched {
return nil, ErrInvalidTime
}
time := Time(str)
return &time, nil
}
func DateFromStd(time time.Time) Date {
return Date(time.Format("2006-01-02"))
}
func DateFromString(str string) (*Date, error) {
if str == "" {
return nil, nil
}
matched := dateMatch.MatchString(str)
if !matched {
return nil, ErrInvalidTime
}
date := Date(str)
return &date, nil
}
func (d Date) ToStd() time.Time {
t, err := time.Parse("2006-01-02", string(d))
if err != nil {
log.Fatal(err)
}
return t
}
|