aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorMatej Vasek <mvasek@redhat.com>2021-08-04 21:30:47 +0200
committerMatthew Heon <matthew.heon@pm.me>2021-08-11 15:28:18 -0400
commit4b42265b520238ecb30059430011e63fabdd23d2 (patch)
tree5bf960e4e35d146fff54f8f869b5b2732acb92b5 /pkg
parent85d5c24ede45d5455d8ddad4ac8c1b714ce2b14a (diff)
downloadpodman-4b42265b520238ecb30059430011e63fabdd23d2.tar.gz
podman-4b42265b520238ecb30059430011e63fabdd23d2.tar.bz2
podman-4b42265b520238ecb30059430011e63fabdd23d2.zip
Fix TS parsing for fractional values
Parse Unix timestamps that contains fractional part. Signed-off-by: Matej Vasek <mvasek@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r--pkg/util/utils.go6
-rw-r--r--pkg/util/utils_test.go15
2 files changed, 19 insertions, 2 deletions
diff --git a/pkg/util/utils.go b/pkg/util/utils.go
index 60aa64ac1..774590f44 100644
--- a/pkg/util/utils.go
+++ b/pkg/util/utils.go
@@ -3,6 +3,7 @@ package util
import (
"encoding/json"
"fmt"
+ "math"
"os"
"os/user"
"path/filepath"
@@ -530,9 +531,10 @@ func ParseInputTime(inputTime string) (time.Time, error) {
}
}
- unixTimestamp, err := strconv.ParseInt(inputTime, 10, 64)
+ unixTimestamp, err := strconv.ParseFloat(inputTime, 64)
if err == nil {
- return time.Unix(unixTimestamp, 0), nil
+ iPart, fPart := math.Modf(unixTimestamp)
+ return time.Unix(int64(iPart), int64(fPart*1_000_000_000)).UTC(), nil
}
// input might be a duration
diff --git a/pkg/util/utils_test.go b/pkg/util/utils_test.go
index cb737bd76..027acbdab 100644
--- a/pkg/util/utils_test.go
+++ b/pkg/util/utils_test.go
@@ -2,6 +2,7 @@ package util
import (
"testing"
+ "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -277,3 +278,17 @@ func TestPeriodAndQuotaToCores(t *testing.T) {
assert.Equal(t, PeriodAndQuotaToCores(period, quota), expectedCores)
}
+
+func TestParseInputTime(t *testing.T) {
+ tm, err := ParseInputTime("1.5")
+ if err != nil {
+ t.Errorf("expected error to be nil but was: %v", err)
+ }
+
+ expected, err := time.ParseInLocation(time.RFC3339Nano, "1970-01-01T00:00:01.500000000Z", time.UTC)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ assert.Equal(t, expected, tm)
+}