github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/types/time/timestamp_test.go (about)

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