From 2437680c296bbcd994e570203db3daba21af74f1 Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 30 Jan 2023 10:31:47 +0100 Subject: [PATCH] added gitignore file and updated some stuff --- .gitignore | 2 ++ internal/database/timestamp.go | 22 ++++++++++++++++++++-- internal/tools/datetime.go | 3 +++ internal/workday/workday.go | 30 ++++++++++++++++++++++-------- 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a19b49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__debug_bin +worklog diff --git a/internal/database/timestamp.go b/internal/database/timestamp.go index cbffa97..9c3d737 100644 --- a/internal/database/timestamp.go +++ b/internal/database/timestamp.go @@ -58,9 +58,27 @@ func StopTimestamp() (bool, Timestamp) { } func GetFirstTimestamp(user string, date time.Time) Timestamp { + return GetTodaysTimestamp(user, date, true) +} + +func GetLastTimestamp(user string, date time.Time) Timestamp { + return GetTodaysTimestamp(user, date, false) +} + +func GetTodaysTimestamp(user string, date time.Time, first bool) Timestamp { var timestamp Timestamp - date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC) - connection.Where("start > ? and user = ?", date, user).First(×tamp) + var where string + var order string + if first { + date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC) + order = "asc" + where = "start > ?" + } else { + date = time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.UTC) + order = "desc" + where = "start < ?" + } + connection.Where(where+" and user = ?", date, user).Order("start " + order).First(×tamp) return timestamp } diff --git a/internal/tools/datetime.go b/internal/tools/datetime.go index 247267e..e7131f4 100644 --- a/internal/tools/datetime.go +++ b/internal/tools/datetime.go @@ -27,6 +27,9 @@ func TimeToDDMMYYYY(time time.Time) string { } func TimeToHHMMSS(time time.Time) string { + if time == ZeroDate { + return "" + } tmp := strconv.Itoa(time.Second()) if len(tmp) < 2 { tmp = "0" + tmp diff --git a/internal/workday/workday.go b/internal/workday/workday.go index 2ab77a5..c857ec6 100644 --- a/internal/workday/workday.go +++ b/internal/workday/workday.go @@ -41,6 +41,7 @@ func ToJSON(from time.Time, to time.Time, user string) (string, error) { Stop: tools.TimeToHHMMSS(workday.Stop), Duration: tools.DurationToHHMMSS(workday.Duration), } + workdayJson.Open = workday.isOpen() if workday.Pause.Duration > 0 { pauseJson := pauseJson{ Start: tools.TimeToHHMMSS(workday.Pause.Start), @@ -63,6 +64,7 @@ func (workday *Workday) ToJSON(user string) (string, error) { Stop: tools.TimeToHHMMSS(workday.Stop), Duration: tools.DurationToHHMMSS(workday.Duration), } + workdayJson.Open = workday.isOpen() if workday.Pause.Duration > 0 { pauseJson := pauseJson{ Start: tools.TimeToHHMMSS(workday.Pause.Start), @@ -77,17 +79,24 @@ func (workday *Workday) ToJSON(user string) (string, error) { func GetToday(user string) (Workday, error) { var workday Workday - timestamp := database.GetFirstTimestamp(user, time.Now()) - if timestamp.ID == 0 { + firstTimestamp := database.GetFirstTimestamp(user, time.Now()) + if firstTimestamp.ID == 0 { return workday, errors.New("no workday for user '" + user + "' started today") } - workday.Date = tools.TimeToDDMMYYYY(timestamp.Start) - workday.Start = timestamp.Start - workday.Stop = timestamp.End - if workday.Stop == tools.ZeroDate { - workday.Stop = time.Now() - } + workday.Date = tools.TimeToDDMMYYYY(firstTimestamp.Start) + workday.Start = firstTimestamp.Start + workday.Stop = firstTimestamp.End workday.Duration = workday.Stop.Sub(workday.Start) + if workday.isOpen() { + return workday, nil + } + currentTimestamp := database.GetLastTimestamp(user, time.Now()) + workday.Stop = currentTimestamp.End + if workday.Stop == tools.ZeroDate { + workday.Duration = time.Since(workday.Start) + } else { + workday.Duration = workday.Stop.Sub(workday.Start) + } return workday, nil } @@ -144,6 +153,10 @@ func aggregatedTimestampsToWorkdays(aggregatedTimestamps map[string][]database.T return workdays } +func (workday *Workday) isOpen() bool { + return workday.Stop == tools.ZeroDate +} + func (workday *Workday) insertFakePause() { var pause Pause deviationStart := rand.Intn(15-(-15)) + (-15) @@ -177,6 +190,7 @@ type workdayJson struct { Start string Stop string Duration string + Open bool Pause pauseJson }