From 3e6768b1e37c5f04364ab53e5af5ff9808de3bb5 Mon Sep 17 00:00:00 2001 From: lixulun Date: Mon, 15 Jun 2026 17:55:17 +0800 Subject: [PATCH] add time command --- main.go | 5 ++++- time.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 time.go diff --git a/main.go b/main.go index 963d4e3..7df6147 100644 --- a/main.go +++ b/main.go @@ -9,8 +9,9 @@ func commandsUsage() string { return fmt.Sprintf( `commands: path %v + time %v week %v -`, commandPathDescription, commandWeekDescription) +`, commandPathDescription, commandTimeDescription, commandWeekDescription) } func main() { @@ -18,6 +19,8 @@ func main() { switch os.Args[1] { case "path": commandPath(os.Args[1:]) + case "time": + commandTime(os.Args[1:]) case "week": commandWeek(os.Args[1:]) default: diff --git a/time.go b/time.go new file mode 100644 index 0000000..9f9c5fc --- /dev/null +++ b/time.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "time" +) + +var commandTimeDescription = "display more information about target timestamp/time" + +func commandTime(args []string) { + var err error + targetTime := time.Now() + if len(args) > 1 { + targetTime, err = time.Parse(time.RFC3339, args[1]) + if err == nil { + goto GOTTIME + } + if len(args[1]) <= 10 { + var unix int + unix, err = strconv.Atoi(args[1]) + if err != nil { + fmt.Printf("`%v` is not a Unix Epoch Time\n", args[1]) + os.Exit(1) + } + targetTime = time.Unix(int64(unix), 0) + } else if len(args[1]) >= 13 { + var unixMilli int + unixMilli, err = strconv.Atoi(args[1]) + if err != nil { + fmt.Printf("`%v` is not a Unix Epoch Time in milliseconds\n", args[1]) + os.Exit(1) + } + targetTime = time.UnixMilli(int64(unixMilli)) + } else { + fmt.Printf("`%v` is neighter a Unix Epoch Time, nor a formatted time in ISO8601", args[1]) + os.Exit(1) + } + + } +GOTTIME: + fmt.Printf("Unix: %v\n", targetTime.Unix()) + fmt.Printf("Unix milliseconds: %v\n", targetTime.UnixMilli()) + fmt.Printf("Local time: %v\n", targetTime.Local().Format(time.RFC3339)) + fmt.Printf("UTC time: %v\n", targetTime.UTC().Format(time.RFC3339)) + fmt.Printf("Time to target: %v\n", targetTime.Sub(time.Now())) +}