github.com/AbhinandanKurakure/podman/v3@v3.4.10/pkg/timetype/timestamp_test.go (about)

     1  package timetype
     2  
     3  // code adapted from https://github.com/moby/moby/blob/master/api/types/time/timestamp.go
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestGetTimestamp(t *testing.T) {
    12  	now := time.Now().In(time.UTC)
    13  	cases := []struct {
    14  		in, expected string
    15  		expectedErr  bool
    16  	}{
    17  		// Partial RFC3339 strings get parsed with second precision
    18  		{"2006-01-02T15:04:05.999999999+07:00", "1136189045.999999999", false},
    19  		{"2006-01-02T15:04:05.999999999Z", "1136214245.999999999", false},
    20  		{"2006-01-02T15:04:05.999999999", "1136214245.999999999", false},
    21  		{"2006-01-02T15:04:05Z", "1136214245.000000000", false},
    22  		{"2006-01-02T15:04:05", "1136214245.000000000", false},
    23  		{"2006-01-02T15:04:0Z", "", true},
    24  		{"2006-01-02T15:04:0", "", true},
    25  		{"2006-01-02T15:04Z", "1136214240.000000000", false},
    26  		{"2006-01-02T15:04+00:00", "1136214240.000000000", false},
    27  		{"2006-01-02T15:04-00:00", "1136214240.000000000", false},
    28  		{"2006-01-02T15:04", "1136214240.000000000", false},
    29  		{"2006-01-02T15:0Z", "", true},
    30  		{"2006-01-02T15:0", "", true},
    31  		{"2006-01-02T15Z", "1136214000.000000000", false},
    32  		{"2006-01-02T15+00:00", "1136214000.000000000", false},
    33  		{"2006-01-02T15-00:00", "1136214000.000000000", false},
    34  		{"2006-01-02T15", "1136214000.000000000", false},
    35  		{"2006-01-02T1Z", "1136163600.000000000", false},
    36  		{"2006-01-02T1", "1136163600.000000000", false},
    37  		{"2006-01-02TZ", "", true},
    38  		{"2006-01-02T", "", true},
    39  		{"2006-01-02+00:00", "1136160000.000000000", false},
    40  		{"2006-01-02-00:00", "1136160000.000000000", false},
    41  		{"2006-01-02-00:01", "1136160060.000000000", false},
    42  		{"2006-01-02Z", "1136160000.000000000", false},
    43  		{"2006-01-02", "1136160000.000000000", false},
    44  		{"2015-05-13T20:39:09Z", "1431549549.000000000", false},
    45  
    46  		// unix timestamps returned as is
    47  		{"1136073600", "1136073600", false},
    48  		{"1136073600.000000001", "1136073600.000000001", false},
    49  		// Durations
    50  		{"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix()), false},
    51  		{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
    52  		{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
    53  
    54  		{"invalid", "", true},
    55  		{"", "", true},
    56  	}
    57  
    58  	for _, c := range cases {
    59  		o, err := GetTimestamp(c.in, now)
    60  		if o != c.expected ||
    61  			(err == nil && c.expectedErr) ||
    62  			(err != nil && !c.expectedErr) {
    63  			t.Errorf("wrong value for '%s'. expected:'%s' got:'%s' with error: `%s`", c.in, c.expected, o, err)
    64  			t.Fail()
    65  		}
    66  	}
    67  }
    68  
    69  func TestParseTimestamps(t *testing.T) {
    70  	cases := []struct {
    71  		in                        string
    72  		def, expectedS, expectedN int64
    73  		expectedErr               bool
    74  	}{
    75  		// unix timestamps
    76  		{"1136073600", 0, 1136073600, 0, false},
    77  		{"1136073600.000000001", 0, 1136073600, 1, false},
    78  		{"1136073600.0000000010", 0, 1136073600, 1, false},
    79  		{"1136073600.00000001", 0, 1136073600, 10, false},
    80  		{"foo.bar", 0, 0, 0, true},
    81  		{"1136073600.bar", 0, 1136073600, 0, true},
    82  		{"", -1, -1, 0, false},
    83  	}
    84  
    85  	for _, c := range cases {
    86  		s, n, err := ParseTimestamps(c.in, c.def)
    87  		if s != c.expectedS ||
    88  			n != c.expectedN ||
    89  			(err == nil && c.expectedErr) ||
    90  			(err != nil && !c.expectedErr) {
    91  			t.Errorf("wrong values for input `%s` with default `%d` expected:'%d'seconds and `%d`nanosecond got:'%d'seconds and `%d`nanoseconds with error: `%s`", c.in, c.def, c.expectedS, c.expectedN, s, n, err)
    92  			t.Fail()
    93  		}
    94  	}
    95  }