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

Function TestGetTimestamp

pkg/timestamp/timestamp_test.go:32–89  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30)
31
32func TestGetTimestamp(t *testing.T) {
33 now := time.Now().In(time.UTC)
34 cases := []struct {
35 in, expected string
36 expectedErr bool
37 }{
38 // Partial RFC3339 strings get parsed with second precision
39 {"2006-01-02T15:04:05.999999999+07:00", "1136189045.999999999", false},
40 {"2006-01-02T15:04:05.999999999Z", "1136214245.999999999", false},
41 {"2006-01-02T15:04:05.999999999", "1136214245.999999999", false},
42 {"2006-01-02T15:04:05Z", "1136214245.000000000", false},
43 {"2006-01-02T15:04:05", "1136214245.000000000", false},
44 {"2006-01-02T15:04:0Z", "", true},
45 {"2006-01-02T15:04:0", "", true},
46 {"2006-01-02T15:04Z", "1136214240.000000000", false},
47 {"2006-01-02T15:04+00:00", "1136214240.000000000", false},
48 {"2006-01-02T15:04-00:00", "1136214240.000000000", false},
49 {"2006-01-02T15:04", "1136214240.000000000", false},
50 {"2006-01-02T15:0Z", "", true},
51 {"2006-01-02T15:0", "", true},
52 {"2006-01-02T15Z", "1136214000.000000000", false},
53 {"2006-01-02T15+00:00", "1136214000.000000000", false},
54 {"2006-01-02T15-00:00", "1136214000.000000000", false},
55 {"2006-01-02T15", "1136214000.000000000", false},
56 {"2006-01-02T1Z", "1136163600.000000000", false},
57 {"2006-01-02T1", "1136163600.000000000", false},
58 {"2006-01-02TZ", "", true},
59 {"2006-01-02T", "", true},
60 {"2006-01-02+00:00", "1136160000.000000000", false},
61 {"2006-01-02-00:00", "1136160000.000000000", false},
62 {"2006-01-02-00:01", "1136160060.000000000", false},
63 {"2006-01-02Z", "1136160000.000000000", false},
64 {"2006-01-02", "1136160000.000000000", false},
65 {"2015-05-13T20:39:09Z", "1431549549.000000000", false},
66
67 // unix timestamps returned as is
68 {"1136073600", "1136073600", false},
69 {"1136073600.000000001", "1136073600.000000001", false},
70 {"1136073600.123456789123456789123", "1136073600.123456789123456789123", false},
71 // Durations
72 {"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix()), false},
73 {"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
74 {"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
75
76 {"invalid", "", true},
77 {"", "", true},
78 }
79
80 for _, c := range cases {
81 o, err := GetTimestamp(c.in, now)
82 if o != c.expected ||
83 (err == nil && c.expectedErr) ||
84 (err != nil && !c.expectedErr) {
85 t.Errorf("wrong value for '%s'. expected:'%s' got:'%s' with error: `%s`", c.in, c.expected, o, err)
86 t.Fail()
87 }
88 }
89}

Callers

nothing calls this directly

Calls 3

GetTimestampFunction · 0.85
AddMethod · 0.80
FailMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…