summaryrefslogtreecommitdiff
path: root/internal/worktime/entries.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/worktime/entries.go')
-rw-r--r--internal/worktime/entries.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/internal/worktime/entries.go b/internal/worktime/entries.go
index fd116ba..2a6dc24 100644
--- a/internal/worktime/entries.go
+++ b/internal/worktime/entries.go
@@ -12,6 +12,7 @@ const (
actionLogin = "login"
actionLogout = "logout"
actionAdd = "add"
+ dayOffHours = 8
)
var (
@@ -76,6 +77,11 @@ func Add(dbDir, hostname, category string, duration time.Duration, at time.Time,
return appendHostEntry(dbDir, host, entry)
}
+// AddDayOff creates an 8-hour day-off entry for the provided day.
+func AddDayOff(dbDir, hostname string, day time.Time, descr string) (Entry, error) {
+ return Add(dbDir, hostname, "off", time.Duration(dayOffHours)*time.Hour, startOfDay(day), descr)
+}
+
// Sub creates an add entry with a negative duration value.
func Sub(dbDir, hostname, category string, duration time.Duration, at time.Time, descr string) (Entry, error) {
host, err := normalizeHostname(hostname)
@@ -141,6 +147,15 @@ func EditEntry(dbDir, hostname string, index int, replacement Entry) (Entry, err
return normalized, nil
}
+// NormalizeEditedEntry validates and normalizes an edited entry for a host without persisting it.
+func NormalizeEditedEntry(entry Entry, hostname string) (Entry, error) {
+ host, err := normalizeHostname(hostname)
+ if err != nil {
+ return Entry{}, err
+ }
+ return normalizeEditedEntry(entry, host)
+}
+
// DeleteEntry removes an entry by index from the host database.
func DeleteEntry(dbDir, hostname string, index int) (Entry, error) {
host, err := normalizeHostname(hostname)
@@ -230,6 +245,12 @@ func durationToSeconds(duration time.Duration) int64 {
return int64(duration / time.Second)
}
+func startOfDay(value time.Time) time.Time {
+ effective := effectiveTime(value)
+ year, month, day := effective.Date()
+ return time.Date(year, month, day, 0, 0, 0, 0, effective.Location())
+}
+
func effectiveTime(at time.Time) time.Time {
if at.IsZero() {
return time.Now()