aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/tracker/types.go50
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
+}