worktime/main.go
2025-01-06 13:34:57 +01:00

48 lines
1012 B
Go

package main
import (
"flag"
"fmt"
"time"
)
const (
defaultFormat = "15:04"
defaultGoal = "8.3h"
)
func main() {
format := flag.String("f", defaultFormat, "Set the input time format ")
goalS := flag.String("g", defaultGoal, "Set the duration goal ")
flag.Parse()
goal, err := time.ParseDuration(*goalS)
if err != nil {
fmt.Printf("Error parsing \"%s\": %v", *goalS, err)
eight := 8.3 * float64(time.Hour)
goal = time.Duration(eight)
}
acc := time.Second * 0
for i, arg := range flag.Args() {
t, err := time.Parse(*format, arg)
if err != nil {
fmt.Printf("Error parsing \"%s\": %v", arg, err)
continue
}
dur := time.Hour*time.Duration(t.Hour()) + time.Minute*time.Duration(t.Minute())
if i%2 == 0 {
acc -= dur
} else {
acc += dur
}
}
if len(flag.Args())%2 != 0 {
diff := goal - acc
fmt.Printf("You reached your goal of %v at: %s\n", goal, time.Time{}.Add(diff).Format(*format))
} else {
fmt.Printf("With these hour's you have worked: %v\n", acc)
}
}