add time command
This commit is contained in:
5
main.go
5
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:
|
||||
|
||||
48
time.go
Normal file
48
time.go
Normal 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()))
|
||||
}
|
||||
Reference in New Issue
Block a user