podman
1package timetype2
3// code adapted from https://github.com/moby/moby/blob/master/api/types/time/timestamp.go
4
5import (6"fmt"7"math"8"strconv"9"strings"10"time"11)
12
13// These are additional predefined layouts for use in Time.Format and Time.Parse
14// with --since and --until parameters for `docker logs` and `docker events`
15const (16rFC3339Local = "2006-01-02T15:04:05" // RFC3339 with local timezone17rFC3339NanoLocal = "2006-01-02T15:04:05.999999999" // RFC3339Nano with local timezone18dateWithZone = "2006-01-02Z07:00" // RFC3339 with time at 00:00:0019dateLocal = "2006-01-02" // RFC3339 with local timezone and time at 00:00:0020)
21
22// GetTimestamp tries to parse given string as golang duration,
23// then RFC3339 time and finally as a Unix timestamp. If
24// any of these were successful, it returns a Unix timestamp
25// as string otherwise returns the given value back.
26// In case of duration input, the returned timestamp is computed
27// as the given reference time minus the amount of the duration.
28func GetTimestamp(value string, reference time.Time) (string, error) {29if d, err := time.ParseDuration(value); value != "0" && err == nil {30return strconv.FormatInt(reference.Add(-d).Unix(), 10), nil31}32
33var format string34// if the string has a Z or a + or three dashes use parse otherwise use parseinlocation35parseInLocation := !(strings.ContainsAny(value, "zZ+") || strings.Count(value, "-") == 3)36
37switch {38case strings.Contains(value, "."):39if parseInLocation {40format = rFC3339NanoLocal41} else {42format = time.RFC3339Nano43}44case strings.Contains(value, "T"):45// we want the number of colons in the T portion of the timestamp46tcolons := strings.Count(value, ":")47// if parseInLocation is off and we have a +/- zone offset (not Z) then48// there will be an extra colon in the input for the tz offset subtract that49// colon from the tcolons count50if !parseInLocation && !strings.ContainsAny(value, "zZ") && tcolons > 0 {51tcolons--52}53if parseInLocation {54switch tcolons {55case 0:56format = "2006-01-02T15"57case 1:58format = "2006-01-02T15:04"59default:60format = rFC3339Local61}62} else {63switch tcolons {64case 0:65format = "2006-01-02T15Z07:00"66case 1:67format = "2006-01-02T15:04Z07:00"68default:69format = time.RFC333970}71}72case parseInLocation:73format = dateLocal74default:75format = dateWithZone76}77
78var t time.Time79var err error80
81if parseInLocation {82t, err = time.ParseInLocation(format, value, time.FixedZone(reference.Zone()))83} else {84t, err = time.Parse(format, value)85}86
87if err != nil {88// if there is a `-` then it's an RFC3339 like timestamp89if strings.Contains(value, "-") {90return "", err // was probably an RFC3339 like timestamp but the parser failed with an error91}92if _, _, err := parseTimestamp(value); err != nil {93return "", fmt.Errorf("failed to parse value as time or duration: %q", value)94}95return value, nil // unix timestamp in and out case (meaning: the value passed at the command line is already in the right format for passing to the server)96}97
98return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil99}
100
101// ParseTimestamps returns seconds and nanoseconds from a timestamp that has the
102// format "%d.%09d", time.Unix(), int64(time.Nanosecond()))
103// if the incoming nanosecond portion is longer or shorter than 9 digits it is
104// converted to nanoseconds. The expectation is that the seconds and
105// seconds will be used to create a time variable. For example:
106//
107// seconds, nanoseconds, err := ParseTimestamp("1136073600.000000001",0)
108// if err == nil since := time.Unix(seconds, nanoseconds)
109//
110// returns seconds as def(aultSeconds) if value == ""
111func ParseTimestamps(value string, def int64) (int64, int64, error) {112if value == "" {113return def, 0, nil114}115return parseTimestamp(value)116}
117
118func parseTimestamp(value string) (int64, int64, error) {119spart, npart, hasParts := strings.Cut(value, ".")120s, err := strconv.ParseInt(spart, 10, 64)121if err != nil {122return s, 0, err123}124if !hasParts {125return s, 0, nil126}127n, err := strconv.ParseInt(npart, 10, 64)128if err != nil {129return s, n, err130}131// should already be in nanoseconds but just in case convert n to nanoseconds132n = int64(float64(n) * math.Pow(float64(10), float64(9-len(npart))))133return s, n, nil134}
135