add time command

This commit is contained in:
2026-06-15 17:55:17 +08:00
parent f5692585fa
commit 3e6768b1e3
2 changed files with 52 additions and 1 deletions

View File

@@ -9,8 +9,9 @@ func commandsUsage() string {
return fmt.Sprintf( return fmt.Sprintf(
`commands: `commands:
path %v path %v
time %v
week %v week %v
`, commandPathDescription, commandWeekDescription) `, commandPathDescription, commandTimeDescription, commandWeekDescription)
} }
func main() { func main() {
@@ -18,6 +19,8 @@ func main() {
switch os.Args[1] { switch os.Args[1] {
case "path": case "path":
commandPath(os.Args[1:]) commandPath(os.Args[1:])
case "time":
commandTime(os.Args[1:])
case "week": case "week":
commandWeek(os.Args[1:]) commandWeek(os.Args[1:])
default: default:

48
time.go Normal file
View File

@@ -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()))
}