From 64218a31da72649402d5bb9dcda5301cb94712ec Mon Sep 17 00:00:00 2001 From: Paul Gaiduk Date: Tue, 26 Nov 2024 18:09:19 +0100 Subject: [PATCH] add function to get timestamp from gzip file name Since the functionality is used across pillar, newlog and edgeview code, it is better to have it in a separate file imported by all of them. Signed-off-by: Paul Gaiduk --- pkg/pillar/types/newlogtypes.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/pillar/types/newlogtypes.go b/pkg/pillar/types/newlogtypes.go index d280d0f940..05e0022894 100644 --- a/pkg/pillar/types/newlogtypes.go +++ b/pkg/pillar/types/newlogtypes.go @@ -4,6 +4,9 @@ package types import ( + "fmt" + "strconv" + "strings" "time" ) @@ -81,3 +84,22 @@ type NewlogMetrics struct { DevMetrics logfileMetrics // Device metrics AppMetrics logfileMetrics // App metrics } + +// GetTimestampFromGzipName - get timestamp from gzip file name +func GetTimestampFromGzipName(fName string) (time.Time, error) { + // here are example file names: + // app.6656f860-7563-4bbf-8bba-051f5942982b.log.1730464687367.gz + // dev.log.keep.1730404601953.gz + // dev.log.upload.1730404601953.gz + // the timestamp is the number between the last two dots + nameParts := strings.Split(fName, ".") + if len(nameParts) < 2 { + return time.Time{}, fmt.Errorf("getTimestampFromGzipName: invalid log file name %s", fName) + } + timeStr := nameParts[len(nameParts)-2] + fTime, err := strconv.Atoi(timeStr) + if err != nil { + return time.Time{}, fmt.Errorf("getTimestampFromGzipName: %w", err) + } + return time.Unix(0, int64(fTime)*int64(time.Millisecond)), nil +}