From 7520f1d57d6c8f54825db188edd05c04a04eb72a Mon Sep 17 00:00:00 2001 From: u80864958 Date: Mon, 6 Jan 2025 13:34:57 +0100 Subject: [PATCH] works --- go.mod | 3 +++ main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5a3cc8a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.schreifuchs.ch/schreifuchs/worktime + +go 1.22.5 diff --git a/main.go b/main.go new file mode 100644 index 0000000..2281ff7 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +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) + } +}