From aa16a2fe3bd94b95526d2c8fa49a51185d562920 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 21 May 2024 18:18:08 +0200 Subject: [PATCH] feat: Add get unixtime --- README.md | 1 + cli.go | 10 ++++++++++ unixtime.go | 6 ++++++ unixtime_test.go | 15 +++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/README.md b/README.md index fadd19e..70900cb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli.go b/cli.go index 869f460..4ddc038 100644 --- a/cli.go +++ b/cli.go @@ -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"` @@ -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 +} diff --git a/unixtime.go b/unixtime.go index dc039e8..07e0d6a 100644 --- a/unixtime.go +++ b/unixtime.go @@ -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 +} diff --git a/unixtime_test.go b/unixtime_test.go index dab6ae7..49b6867 100644 --- a/unixtime_test.go +++ b/unixtime_test.go @@ -2,6 +2,7 @@ package main import ( "testing" + "time" ) func TestConvertUnixTimestamp(t *testing.T) { @@ -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) + } + }) +}