Skip to content

Commit

Permalink
add hour diff in summary
Browse files Browse the repository at this point in the history
  • Loading branch information
jzyinq committed Mar 25, 2024
1 parent be9e597 commit 5d14089
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
31 changes: 29 additions & 2 deletions gojira/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/rivo/tview"
"strings"
"time"
)

type Summary struct {
Expand All @@ -16,7 +17,7 @@ func NewSummary() *Summary {
app.ui.app.Draw()
}),
}
summary.SetText("Calendar ?h/?h")
summary.SetText("Loading...")
summary.SetTextAlign(tview.AlignCenter)
return summary
}
Expand All @@ -26,6 +27,32 @@ func (s *Summary) update() {
// that's a hack to remove spaces between hours and minutes
totalTimeSpent = strings.Join(strings.Fields(totalTimeSpent), "")
workingHours := workingHoursInMonthToPresentDay(app.time.Year(), app.time.Month())
s.SetText(fmt.Sprintf("Total %s/%dh", totalTimeSpent, workingHours))
difference := workingHoursAbsoluteDiff(workingHours)
status := fmt.Sprintf("Total %s/%dh", totalTimeSpent, workingHours)
if difference != 0 {
status = fmt.Sprintf("Total %s/%dh (%s)", totalTimeSpent, workingHours, FormatTimeSpent(difference))
}
s.SetText(status)
s.SetTextColor(GetTimeSpentColor(app.workLogs.TotalTimeSpentToPresentDay(), workingHours))
}

func workingHoursInMonthToPresentDay(year int, month time.Month) int {
t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
totalWorkHours := 0

for t.Month() == month && t.Before(time.Now().Local()) {
if t.Weekday() != time.Saturday && t.Weekday() != time.Sunday {
totalWorkHours += 8
}
t = t.AddDate(0, 0, 1)
}
return totalWorkHours
}

func workingHoursAbsoluteDiff(workingHours int) int {
difference := workingHours*60*60 - app.workLogs.TotalTimeSpentToPresentDay()
if difference < 0 {
difference = -difference
}
return difference
}
32 changes: 0 additions & 32 deletions gojira/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,40 +112,8 @@ func OpenURL(url string) {
}
}

func WeekRange(today time.Time) (time.Time, time.Time) {
y, w := today.ISOWeek()
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC)
for firstDay.Weekday() != time.Monday {
firstDay = firstDay.AddDate(0, 0, -1)
}

for {
y1, w1 := firstDay.ISOWeek()
if y1 == y && w1 == w {
break
}
firstDay = firstDay.AddDate(0, 0, 1)
}

lastDay := firstDay.AddDate(0, 0, 6) // Adding 6 days to get to Sunday
return firstDay.Truncate(24 * time.Hour), lastDay.Truncate(24 * time.Hour)
}

func MonthRange(t *time.Time) (time.Time, time.Time) {
firstDayOfCurrentMonth := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
firstDayOfNextMonth := firstDayOfCurrentMonth.AddDate(0, 1, 0)
return firstDayOfCurrentMonth, firstDayOfNextMonth
}

func workingHoursInMonthToPresentDay(year int, month time.Month) int {
t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
totalWorkHours := 0

for t.Month() == month && t.Before(time.Now().Local()) {
if t.Weekday() != time.Saturday && t.Weekday() != time.Sunday {
totalWorkHours += 8
}
t = t.AddDate(0, 0, 1)
}
return totalWorkHours
}

0 comments on commit 5d14089

Please sign in to comment.