mirror of
https://github.com/containers/podman.git
synced 2025-06-15 22:04:32 +08:00
Fix TS parsing for fractional values
Parse Unix timestamps that contains fractional part. Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
pkg/util
@ -3,6 +3,7 @@ package util
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"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 {
|
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
|
// input might be a duration
|
||||||
|
@ -2,6 +2,7 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -277,3 +278,17 @@ func TestPeriodAndQuotaToCores(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, PeriodAndQuotaToCores(period, quota), expectedCores)
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user