From 583a82d78accccd90be7ca1b7e9aa9687b040abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Wed, 22 Jul 2020 11:34:42 +0200 Subject: [PATCH] pkg/utils: Add function creating human-readable duration strings https://github.com/containers/toolbox/pull/503 --- src/pkg/utils/utils.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index 6d38b709f..634d981e8 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -28,9 +28,11 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/acobaugh/osrelease" "github.com/containers/toolbox/pkg/shell" + "github.com/docker/go-units" "github.com/godbus/dbus/v5" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" @@ -333,6 +335,14 @@ func GetMountOptions(target string) (string, error) { return mountOptions, nil } +// HumanDuration accepts a Unix time value and converts it into a human readable +// string. +// +// Examples: "5 minutes ago", "2 hours ago", "3 days ago" +func HumanDuration(duration int64) string { + return units.HumanDuration(time.Since(time.Unix(duration, 0))) + " ago" +} + // ImageReferenceCanBeID checks if 'image' might be the ID of an image func ImageReferenceCanBeID(image string) (bool, error) { matched, err := regexp.MatchString("^[a-f0-9]\\{6,64\\}$", image)