MCPcopy
hub / github.com/moby/moby / GetTimestamp

Function GetTimestamp

client/internal/timestamp/timestamp.go:26–96  ·  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

24// In case of duration input, the returned timestamp is computed
25// as the given reference time minus the amount of the duration.
26func GetTimestamp(value string, reference time.Time) (string, error) {
27 if d, err := time.ParseDuration(value); value != "0" && err == nil {
28 return strconv.FormatInt(reference.Add(-d).Unix(), 10), nil
29 }
30
31 var format string
32 // if the string has a Z or a + or three dashes use parse otherwise use parseinlocation
33 parseInLocation := !strings.ContainsAny(value, "zZ+") && strings.Count(value, "-") != 3
34
35 if strings.Contains(value, ".") {
36 if parseInLocation {
37 format = rFC3339NanoLocal
38 } else {
39 format = time.RFC3339Nano
40 }
41 } else if strings.Contains(value, "T") {
42 // we want the number of colons in the T portion of the timestamp
43 tcolons := strings.Count(value, ":")
44 // if parseInLocation is off and we have a +/- zone offset (not Z) then
45 // there will be an extra colon in the input for the tz offset subtract that
46 // colon from the tcolons count
47 if !parseInLocation && !strings.ContainsAny(value, "zZ") && tcolons > 0 {
48 tcolons--
49 }
50 if parseInLocation {
51 switch tcolons {
52 case 0:
53 format = "2006-01-02T15"
54 case 1:
55 format = "2006-01-02T15:04"
56 default:
57 format = rFC3339Local
58 }
59 } else {
60 switch tcolons {
61 case 0:
62 format = "2006-01-02T15Z07:00"
63 case 1:
64 format = "2006-01-02T15:04Z07:00"
65 default:
66 format = time.RFC3339
67 }
68 }
69 } else if parseInLocation {
70 format = dateLocal
71 } else {
72 format = dateWithZone
73 }
74
75 var t time.Time
76 var err error
77
78 if parseInLocation {
79 t, err = time.ParseInLocation(format, value, time.FixedZone(reference.Zone()))
80 } else {
81 t, err = time.Parse(format, value)
82 }
83

Callers 1

TestGetTimestampFunction · 0.85

Calls 4

ErrorfMethod · 0.80
parseTimestampFunction · 0.70
AddMethod · 0.65
ContainsMethod · 0.45

Tested by 1

TestGetTimestampFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…