2017-07-27 15:27:10 +00:00
|
|
|
package gime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Entry is a time entry
|
|
|
|
type TimeEntry struct {
|
|
|
|
uuid string
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
2017-07-31 12:00:16 +00:00
|
|
|
tags *TagCollection
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTimeEntry creates an entry with the given parameters and returns it.
|
|
|
|
// An error is returned if no start time is given or if the end time given is
|
|
|
|
// non-zero and earlier than the start time.
|
2017-07-31 12:00:16 +00:00
|
|
|
func CreateTimeEntry(s, e time.Time, t *TagCollection) (*TimeEntry, error) {
|
2017-07-28 11:44:08 +00:00
|
|
|
ret := new(TimeEntry)
|
2017-07-27 15:27:10 +00:00
|
|
|
ret.uuid = uuid.New()
|
|
|
|
if s.IsZero() {
|
|
|
|
// No start time given, return error
|
|
|
|
return ret, errors.New("No start time given")
|
|
|
|
}
|
2017-07-31 12:00:16 +00:00
|
|
|
ret.start = s
|
2017-07-27 15:27:10 +00:00
|
|
|
if !e.IsZero() {
|
|
|
|
if !e.After(s) {
|
|
|
|
// Given end time is earlier than start time
|
|
|
|
return ret, errors.New("End time is before start time")
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 12:00:16 +00:00
|
|
|
ret.end = e
|
|
|
|
ret.tags = t
|
|
|
|
return ret, nil
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 20:39:24 +00:00
|
|
|
// GetUUID returns the UUID for the entry
|
|
|
|
func (t *TimeEntry) GetUUID() string {
|
|
|
|
return t.uuid
|
|
|
|
}
|
|
|
|
|
2017-07-27 15:27:10 +00:00
|
|
|
// GetStart returns the start time for the entry
|
|
|
|
func (t *TimeEntry) GetStart() time.Time {
|
|
|
|
return t.start
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStart sets the start time on the time entry
|
|
|
|
func (t *TimeEntry) SetStart(s time.Time) {
|
|
|
|
t.start = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnd returns the end time for the entry
|
|
|
|
func (t *TimeEntry) GetEnd() time.Time {
|
|
|
|
return t.end
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnd sets the end time on the time entry
|
|
|
|
func (t *TimeEntry) SetEnd(e time.Time) {
|
|
|
|
t.end = e
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsActive return true if start is earlier than now and end is zero
|
|
|
|
func (t *TimeEntry) IsActive() bool {
|
|
|
|
return time.Now().After(t.start) && !t.end.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTags returns all of the tags associated with this time entry
|
2017-07-31 12:00:16 +00:00
|
|
|
func (t *TimeEntry) GetTags() *TagCollection {
|
2017-07-27 15:27:10 +00:00
|
|
|
return t.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasTag returns if the time entry contains a specific tag
|
|
|
|
func (t *TimeEntry) HasTag(s string) bool {
|
2017-07-31 12:00:16 +00:00
|
|
|
for i := 0; i < t.tags.Length(); i++ {
|
|
|
|
if t.tags.Get(i) == s {
|
2017-07-27 15:27:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTag adds a tag to the time entry (if it isn't already there)
|
|
|
|
func (t *TimeEntry) AddTag(s string) {
|
|
|
|
if !t.HasTag(s) {
|
2017-07-31 12:00:16 +00:00
|
|
|
t.tags.Push(s)
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTag removes a tag from the time entry
|
|
|
|
func (t *TimeEntry) RemoveTag(s string) {
|
2017-07-31 12:00:16 +00:00
|
|
|
i := t.tags.Index(s)
|
|
|
|
if i != -1 {
|
|
|
|
t.tags.Remove(i)
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:14:53 +00:00
|
|
|
// Equals tests if the passed time entry is the same
|
|
|
|
// as the one that we're calling it on.
|
2017-07-27 15:27:10 +00:00
|
|
|
func (t *TimeEntry) Equals(tst *TimeEntry) bool {
|
|
|
|
return t.uuid == tst.uuid
|
|
|
|
}
|
2017-08-01 20:39:24 +00:00
|
|
|
|
2017-08-02 19:14:53 +00:00
|
|
|
// StartsToday returns if the timer's start time is today
|
2017-08-01 20:39:24 +00:00
|
|
|
func (t *TimeEntry) StartsToday() bool {
|
2017-08-02 19:14:53 +00:00
|
|
|
currTime := time.Now()
|
2018-01-11 16:52:44 +00:00
|
|
|
dur := currTime.Hour()*int(time.Hour) + currTime.Minute()*int(time.Minute)
|
|
|
|
return int(time.Since(t.GetStart())) < dur
|
2017-08-01 20:39:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:14:53 +00:00
|
|
|
// StartsToday returns if the timer's end time is today
|
2017-08-01 20:39:24 +00:00
|
|
|
func (t *TimeEntry) EndsToday() bool {
|
2017-08-02 19:14:53 +00:00
|
|
|
currTime := time.Now()
|
2018-01-11 16:52:44 +00:00
|
|
|
dur := currTime.Hour()*int(time.Hour) + currTime.Minute()*int(time.Minute)
|
|
|
|
return int(time.Since(t.GetEnd())) < dur
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRunning returns if the timer is still running
|
|
|
|
func (t *TimeEntry) IsRunning() bool {
|
|
|
|
return t.GetEnd().IsZero()
|
2017-08-01 20:39:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:14:53 +00:00
|
|
|
// String formats a string of the time entry
|
2017-08-01 20:39:24 +00:00
|
|
|
func (t *TimeEntry) String() string {
|
|
|
|
var ret string
|
|
|
|
ret = t.GetStart().Format(time.RFC3339)
|
|
|
|
if !t.GetEnd().IsZero() {
|
|
|
|
ret += " - " + t.GetEnd().Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
tags := t.GetTags()
|
|
|
|
if tags.Length() > 0 {
|
|
|
|
ret += " [ "
|
|
|
|
for i := 0; i < tags.Length(); i++ {
|
|
|
|
ret += tags.Get(i) + " "
|
|
|
|
}
|
|
|
|
ret += "]"
|
|
|
|
}
|
|
|
|
if t.GetEnd().IsZero() {
|
|
|
|
ret += " Running"
|
|
|
|
}
|
|
|
|
if t.GetUUID() != "" {
|
|
|
|
ret += " (" + t.GetUUID() + ")"
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|