From f5692585fa5511f4a81b6f8742dcaa5142fc51f7 Mon Sep 17 00:00:00 2001 From: lixulun Date: Mon, 15 Jun 2026 11:43:41 +0800 Subject: [PATCH] add week command --- main.go | 5 ++++- week.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 week.go diff --git a/main.go b/main.go index 32aaaa1..963d4e3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,8 @@ func commandsUsage() string { return fmt.Sprintf( `commands: path %v -`, commandPathDescription) + week %v +`, commandPathDescription, commandWeekDescription) } func main() { @@ -17,6 +18,8 @@ func main() { switch os.Args[1] { case "path": commandPath(os.Args[1:]) + case "week": + commandWeek(os.Args[1:]) default: fmt.Printf("unknow command `%v`\n\n", os.Args[1]) fmt.Printf("%v", commandsUsage()) diff --git a/week.go b/week.go new file mode 100644 index 0000000..87803d1 --- /dev/null +++ b/week.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "time" +) + +var commandWeekDescription = "get week in this year and week begin and end offset by this week" + +func commandWeek(args []string) { + var err error + offset := 0 + if len(args) == 2 { + offset, err = strconv.Atoi(args[1]) + if err != nil { + fmt.Printf("expect `%v` to be a integer\n", args[1]) + os.Exit(1) + } + } else if len(args) > 2 { + fmt.Printf("only accept 1 argument, got %v\n", len(args)-1) + os.Exit(1) + } + offsetTime := time.Hour * time.Duration(offset) * 7 * 24 + targetTime := time.Now().Add(offsetTime) + _, week := targetTime.ISOWeek() + weekday := int(targetTime.Weekday()) + // make Sunday = 7 + if weekday == 0 { + weekday = 7 + } + weekBegin := targetTime.Add(time.Hour * 24 * time.Duration(weekday-1) * -1) + weekEnd := targetTime.Add(time.Hour * 24 * time.Duration(7-weekday)) + fmt.Printf("week-%v-%v-%v\n", week, weekBegin.Format("0102"), weekEnd.Format("0102")) +}