diff options
author | Alex <a.kiryukhin@mail.ru> | 2021-12-05 17:49:53 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2021-12-07 03:08:37 +0300 |
commit | 674d9a4a5d0528bbc0c184de2d271004c0e1c831 (patch) | |
tree | 58e084ac7fd68922141c334b1112a66edb2bb3f5 /internal | |
parent | bcdbe68ecde049ef62343584bcc26840322c4864 (diff) |
first release
Diffstat (limited to 'internal')
-rw-r--r-- | internal/tracker/types.go | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/internal/tracker/types.go b/internal/tracker/types.go index 22837fa..4ab5db6 100644 --- a/internal/tracker/types.go +++ b/internal/tracker/types.go @@ -1,6 +1,9 @@ package tracker -import "time" +import ( + "fmt" + "time" +) type Activity struct { ID int `json:"id"` @@ -19,6 +22,30 @@ func (a *Activity) Started() *Span { return nil } +func (a *Activity) Duration() time.Duration { + t := time.Duration(0) + for _, span := range a.Spans { + stop := time.Now() + if span.Stop != nil { + stop = *span.Stop + } + t += stop.Sub(span.Start) + } + return t +} + +func (a *Activity) LastDuration() time.Duration { + if len(a.Spans) == 0 { + return 0 + } + span := a.Spans[len(a.Spans)-1] + stop := time.Now() + if span.Stop != nil { + stop = *span.Stop + } + return stop.Sub(span.Start) +} + type Span struct { Start time.Time `json:"start"` Stop *time.Time `json:"stop,omitempty"` @@ -29,3 +56,24 @@ type Document struct { LastKey int `json:"last"` Activities []*Activity `json:"activities"` } + +type Timespan time.Duration + +func (t Timespan) Format() string { + z := time.Unix(0, 0).UTC() + tt := z.Add(time.Duration(t)) + result := fmt.Sprintf("%d minutes", tt.Minute()) + if tt.Hour() > 0 { + result = fmt.Sprintf("%d hours, ", tt.Hour()) + result + } + if tt.Day() > 1 { + result = fmt.Sprintf("%d days, ", tt.Day()-1) + result + } + if tt.Month() > 1 { + result = fmt.Sprintf("%d months, ", tt.Month()-1) + result + } + if tt.Year() > 1970 { + result = fmt.Sprintf("%d years, ", tt.Year()-1970) + result + } + return result +} |