Skip to content

Commit

Permalink
feat: Add get unixtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed May 21, 2024
1 parent 739e7b1 commit aa16a2f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Commands:
split Split a string
tail Returns the last n lines
time from-unix Convert from Unix time to normal time
time get-unix Get Unix time
url encode Encode string to valid URL
url decode Decode URL to string
version Show version information
Expand Down
10 changes: 10 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type CLI struct {
Tail tailCmd `cmd:"" help:"Returns the last n lines"`
Time struct {
FromUnixTime fromUnixTimeCmd `cmd:"" name:"from-unix" help:"Convert from Unix time to normal time"`
GetUnixTime getUnixTimeCmd `cmd:"" name:"get-unix" help:"Get Unix time"`
} `cmd:"" help:"Time conversions"`
URL struct {
Encode encodeURLCmd `cmd:"" help:"Encode string to valid URL"`
Expand Down Expand Up @@ -446,3 +447,12 @@ func (c *fromUnixTimeCmd) Run(globals *Globals) error {
printOutput(t, globals.Trim)
return nil
}

type getUnixTimeCmd struct {
}

func (c *getUnixTimeCmd) Run(globals *Globals) error {
t := unixTimestamp()
printOutput(t, globals.Trim)
return nil
}
6 changes: 6 additions & 0 deletions unixtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ func convertUnixTimestamp(timestamp int64, format string) (string, error) {

return t.Format(format), nil
}

func unixTimestamp() int64 {
now := time.Now()
t := now.Unix()
return t
}
15 changes: 15 additions & 0 deletions unixtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"testing"
"time"
)

func TestConvertUnixTimestamp(t *testing.T) {
Expand Down Expand Up @@ -35,3 +36,17 @@ func TestConvertUnixTimestampInvalid(t *testing.T) {
t.Error("expected error for invalid timestamp, got nil")
}
}
func TestUnixTimestamp(t *testing.T) {
start := time.Now().Unix()

result := unixTimestamp()

end := time.Now().Unix()

// Check if the result falls within the expected range
t.Run("", func(t *testing.T) {
if result < start || result > end {
t.Errorf("Expected timestamp to be between %d and %d, but got %d", start, end, result)
}
})
}

0 comments on commit aa16a2f

Please sign in to comment.