gime-lib/timeentry.go

98 lines
2.1 KiB
Go
Raw Normal View History

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
}
// 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
}
}
func (t *TimeEntry) Equals(tst *TimeEntry) bool {
return t.uuid == tst.uuid
}