gime-lib/timeentry.go
2017-07-27 10:27:10 -05:00

102 lines
2.2 KiB
Go

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
tags []string
}
// 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.
func CreateTimeEntry(s, e time.Time, t []string) (*TimeEntry, error) {
var ret *TimeEntry
ret.uuid = uuid.New()
if s.IsZero() {
// No start time given, return error
return ret, errors.New("No start time given")
}
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")
}
}
return &TimeEntry{start: s, end: e, tags: t}, nil
}
// 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
func (t *TimeEntry) GetTags() []string {
return t.tags
}
// HasTag returns if the time entry contains a specific tag
func (t *TimeEntry) HasTag(s string) bool {
for i := range t.tags {
if t.tags[i] == s {
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) {
t.tags = append(t.tags, s)
}
}
// RemoveTag removes a tag from the time entry
func (t *TimeEntry) RemoveTag(s string) {
var i int
var fnd bool
for i = range t.tags {
if t.tags[i] == s {
fnd = true
break
}
}
if fnd {
t.tags = append(t.tags[:i], t.tags[i+1:]...)
}
}
func (t *TimeEntry) Equals(tst *TimeEntry) bool {
return t.uuid == tst.uuid
}