mirror of
https://github.com/seqizz/go-tools.git
synced 2024-09-07 10:50:30 +02:00
Add ago
This commit is contained in:
parent
1a5c653324
commit
d40120ccc4
2 changed files with 55 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
|||
# go-tools
|
||||
Collection of tools I've written in golang
|
||||
|
||||
- **ago**: Get the time difference between now and given unix timestamp, in human readable format
|
||||
|
||||
- **did**: A simple, cli-based task logger. Mainly intended for multi-admin environments.
|
||||
|
||||
- **kahin**: A tool to find which directory fills the given disk, with a somewhat unique algorithm.
|
||||
|
|
53
ago/ago.go
Normal file
53
ago/ago.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func relativeTime(timestamp int64) string {
|
||||
now := time.Now().Unix()
|
||||
diff := math.Abs(float64(timestamp - now))
|
||||
isFuture := timestamp > now
|
||||
|
||||
var result string
|
||||
switch {
|
||||
case diff < 60:
|
||||
result = "Just now"
|
||||
case diff < 3600:
|
||||
result = fmt.Sprintf("%d minutes", int64(diff)/60)
|
||||
case diff < 86400:
|
||||
result = fmt.Sprintf("%d hours", int64(diff)/3600)
|
||||
case diff < 2592000:
|
||||
result = fmt.Sprintf("%d days", int64(diff)/86400)
|
||||
case diff < 31536000:
|
||||
result = fmt.Sprintf("%d months", int64(diff)/2592000)
|
||||
default:
|
||||
result = fmt.Sprintf("%d years", int64(diff)/31536000)
|
||||
}
|
||||
|
||||
if isFuture && result != "Just now" {
|
||||
return "In " + result
|
||||
} else if !isFuture && result != "Just now" {
|
||||
return result + " ago"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Please provide a Unix timestamp as an argument")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
timestamp, err := strconv.ParseInt(os.Args[1], 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println("Invalid timestamp:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(relativeTime(timestamp))
|
||||
}
|
Loading…
Reference in a new issue