MCPcopy Create free account
hub / github.com/containerd/nerdctl / GetTimestamp

Function GetTimestamp

pkg/timestamp/timestamp.go:48–118  ·  view source on GitHub ↗

GetTimestamp tries to parse given string as golang duration, then RFC3339 time and finally as a Unix timestamp. If any of these were successful, it returns a Unix timestamp as string otherwise returns the given value back. In case of duration input, the returned timestamp is computed as the given re

(value string, reference time.Time)

Source from the content-addressed store, hash-verified

46// In case of duration input, the returned timestamp is computed
47// as the given reference time minus the amount of the duration.
48func GetTimestamp(value string, reference time.Time) (string, error) {
49 if d, err := time.ParseDuration(value); value != "0" && err == nil {
50 return strconv.FormatInt(reference.Add(-d).Unix(), 10), nil
51 }
52
53 var format string
54 // if the string has a Z or a + or three dashes use parse otherwise use parseinlocation
55 parseInLocation := !strings.ContainsAny(value, "zZ+") && strings.Count(value, "-") != 3
56
57 if strings.Contains(value, ".") {
58 if parseInLocation {
59 format = rFC3339NanoLocal
60 } else {
61 format = time.RFC3339Nano
62 }
63 } else if strings.Contains(value, "T") {
64 // we want the number of colons in the T portion of the timestamp
65 tcolons := strings.Count(value, ":")
66 // if parseInLocation is off and we have a +/- zone offset (not Z) then
67 // there will be an extra colon in the input for the tz offset subtract that
68 // colon from the tcolons count
69 if !parseInLocation && !strings.ContainsAny(value, "zZ") && tcolons > 0 {
70 tcolons--
71 }
72 if parseInLocation {
73 switch tcolons {
74 case 0:
75 format = "2006-01-02T15"
76 case 1:
77 format = "2006-01-02T15:04"
78 default:
79 format = rFC3339Local
80 }
81 } else {
82 switch tcolons {
83 case 0:
84 format = "2006-01-02T15Z07:00"
85 case 1:
86 format = "2006-01-02T15:04Z07:00"
87 default:
88 format = time.RFC3339
89 }
90 }
91 } else if parseInLocation {
92 format = dateLocal
93 } else {
94 format = dateWithZone
95 }
96
97 var t time.Time
98 var err error
99
100 if parseInLocation {
101 t, err = time.ParseInLocation(format, value, time.FixedZone(reference.Zone()))
102 } else {
103 t, err = time.Parse(format, value)
104 }
105

Callers 3

viewLogsJournaldFunction · 0.92
writeEntryFunction · 0.92
TestGetTimestampFunction · 0.85

Calls 4

parseTimestampFunction · 0.85
AddMethod · 0.80
CountMethod · 0.65
ParseMethod · 0.65

Tested by 1

TestGetTimestampFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…