-
Notifications
You must be signed in to change notification settings - Fork 74
/
timetrace.go
207 lines (167 loc) · 5.05 KB
/
timetrace.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package core
import (
"errors"
"fmt"
"time"
"github.com/dominikbraun/timetrace/config"
)
var (
ErrNoEndTime = errors.New("no end time for last record")
ErrTrackingNotStarted = errors.New("start tracking first")
)
type Report struct {
Current *Record
TrackedTimeCurrent *time.Duration
TrackedTimeToday time.Duration
}
// Filesystem represents a filesystem used for storing and loading resources.
type Filesystem interface {
ProjectFilepath(key string) string
ProjectFilepaths() ([]string, error)
RecordFilepath(start time.Time) string
RecordFilepaths(dir string, less func(a, b string) bool) ([]string, error)
RecordDirs() ([]string, error)
RecordDirFromDate(date time.Time) string
EnsureDirectories() error
EnsureRecordDir(date time.Time) error
}
type Timetrace struct {
config *config.Config
fs Filesystem
formatter *Formatter
}
func New(config *config.Config, fs Filesystem) *Timetrace {
return &Timetrace{
config: config,
fs: fs,
formatter: &Formatter{
use12Hours: config.Use12Hours,
},
}
}
// Start starts tracking time for the given project key. This will create a new
// record with the current time as start time.
//
// Since parallel work isn't supported, the previous work must be stopped first.
func (t *Timetrace) Start(projectKey string, isBillable bool) error {
latestRecord, err := t.LoadLatestRecord()
if err != nil {
return err
}
// If there is no end time of the latest record, the user has to stop first.
if latestRecord != nil && latestRecord.End == nil {
return ErrNoEndTime
}
var project *Project
if projectKey != "" {
if project, err = t.LoadProject(projectKey); err != nil {
return err
}
}
record := Record{
Start: time.Now(),
Project: project,
IsBillable: isBillable,
}
return t.SaveRecord(record, false)
}
// Status calculates and returns a status report.
//
// If the user isn't tracking time at the moment of calling this function, the
// Report.Current and Report.TrackedTimeCurrent fields will be nil. If the user
// hasn't tracked time today, ErrTrackingNotStarted will be returned.
func (t *Timetrace) Status() (*Report, error) {
now := time.Now()
firstRecord, err := t.loadOldestRecord(time.Now())
if err != nil {
return nil, err
}
if firstRecord == nil {
return nil, ErrTrackingNotStarted
}
latestRecord, err := t.LoadLatestRecord()
if err != nil {
return nil, err
}
trackedTimeToday, err := t.trackedTime(now)
if err != nil {
return nil, err
}
report := &Report{
TrackedTimeToday: trackedTimeToday,
}
// If the latest record has been stopped, there is no active time tracking.
// Therefore, just calculate the tracked time of today and return.
if latestRecord.End != nil {
return report, nil
}
report.Current = latestRecord
// If the latest record has not been stopped yet, time tracking is active.
// Calculate the time tracked for the current record and for today.
trackedTimeCurrent := now.Sub(latestRecord.Start)
report.TrackedTimeCurrent = &trackedTimeCurrent
return report, nil
}
// Stop stops the time tracking and marks the current record as ended.
func (t *Timetrace) Stop() error {
latestRecord, err := t.LoadLatestRecord()
if err != nil {
return err
}
if latestRecord == nil || latestRecord.End != nil {
return ErrTrackingNotStarted
}
end := time.Now()
latestRecord.End = &end
return t.SaveRecord(*latestRecord, true)
}
func (t *Timetrace) EnsureDirectories() error {
return t.fs.EnsureDirectories()
}
func (t *Timetrace) Config() *config.Config {
return t.config
}
func (t *Timetrace) Formatter() *Formatter {
return t.formatter
}
func (t *Timetrace) trackedTime(date time.Time) (time.Duration, error) {
records, err := t.loadAllRecords(date)
if err != nil {
return 0, err
}
var trackedTime time.Duration
for _, record := range records {
// If the record doesn't have an end time, it is expected that this is
// the current record and time is still being tracked.
if record.End == nil {
trackedTime += time.Now().Sub(record.Start)
continue
}
trackedTime += record.End.Sub(record.Start)
}
return trackedTime, nil
}
// FormatTodayTime returns the formated string of the total
// time of today follwoing the format convention
func (report *Report) FormatTodayTime() string {
return formatDuration(report.TrackedTimeToday)
}
// FormatCurrentTime returns the formated string of the current
// report time follwoing the format convention
func (report *Report) FormatCurrentTime() string {
return formatDuration(*report.TrackedTimeCurrent)
}
// formatDuration formats the passed duration into a string.
// The format will be "8h 24min". If the duration is less then 60 secods
// the format will be "0h 0min 12sec".
func formatDuration(duration time.Duration) string {
hours := int64(duration.Hours()) % 60
minutes := int64(duration.Minutes()) % 60
secods := int64(duration.Seconds()) % 60
// as by convention if the duarion is < then 60 secods
// return "0h 0min Xsec"
if hours == 0 && minutes == 0 {
return fmt.Sprintf("0h 0min %dsec", secods)
}
return fmt.Sprintf("%dh %dmin", hours, minutes)
}