github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/timestamp_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package protocol
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestFormatTime(t *testing.T) {
    12  	cases := map[string]struct {
    13  		formatName     string
    14  		expectedOutput string
    15  		input          time.Time
    16  	}{
    17  		"UnixTest1": {
    18  			formatName:     UnixTimeFormatName,
    19  			expectedOutput: "946845296",
    20  			input:          time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
    21  		},
    22  		"UnixTest2": {
    23  			formatName:     UnixTimeFormatName,
    24  			expectedOutput: "946845296.123",
    25  			input:          time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    26  		},
    27  		"ISO8601Test1": {
    28  			formatName:     ISO8601TimeFormatName,
    29  			expectedOutput: "2000-01-02T20:34:56Z",
    30  			input:          time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
    31  		},
    32  		"RFC822Test1": {
    33  			formatName:     RFC822TimeFormatName,
    34  			expectedOutput: "Sun, 02 Jan 2000 20:34:56 GMT",
    35  			input:          time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
    36  		},
    37  		"ISO8601Test2": {
    38  			formatName:     ISO8601TimeFormatName,
    39  			expectedOutput: "2000-01-02T20:34:56.123Z",
    40  			input:          time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    41  		},
    42  		"ISO8601Test3": {
    43  			formatName:     ISO8601TimeFormatName,
    44  			expectedOutput: "2000-01-02T20:34:56.123Z",
    45  			input:          time.Date(2000, time.January, 2, 20, 34, 56, .123456e9, time.UTC),
    46  		},
    47  	}
    48  
    49  	for name, c := range cases {
    50  		t.Run(name, func(t *testing.T) {
    51  			if e, a := c.expectedOutput, FormatTime(c.formatName, c.input); e != a {
    52  				t.Errorf("expected %s, got %s for %s format ", e, a, c.formatName)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestParseTime(t *testing.T) {
    59  	//input and output times are considered equal if they are equal until three decimal places
    60  	cases := map[string]struct {
    61  		formatName, input string
    62  		expectedOutput    time.Time
    63  	}{
    64  		"UnixTestExponent": {
    65  			formatName:     UnixTimeFormatName,
    66  			input:          "1.583858715232899e9",
    67  			expectedOutput: time.Date(2020, time.March, 10, 16, 45, 15, .233e9, time.UTC),
    68  		},
    69  		"UnixTest1": {
    70  			formatName:     UnixTimeFormatName,
    71  			input:          "946845296.123",
    72  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    73  		},
    74  		"UnixTest2": {
    75  			formatName:     UnixTimeFormatName,
    76  			input:          "946845296.12344",
    77  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    78  		},
    79  		"UnixTest3": {
    80  			formatName:     UnixTimeFormatName,
    81  			input:          "946845296.1229999",
    82  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    83  		},
    84  		"ISO8601Test milliseconds": {
    85  			formatName:     ISO8601TimeFormatName,
    86  			input:          "2000-01-02T20:34:56.123Z",
    87  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    88  		},
    89  		"ISO8601Test milliseconds, no Z": {
    90  			formatName:     ISO8601TimeFormatName,
    91  			input:          "2000-01-02T20:34:56.123",
    92  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
    93  		},
    94  		"ISO8601Test nanoseconds": {
    95  			formatName:     ISO8601TimeFormatName,
    96  			input:          "2000-01-02T20:34:56.123456789Z",
    97  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123456789e9, time.UTC),
    98  		},
    99  		"ISO8601Test millisecond utc offset": {
   100  			formatName:     ISO8601TimeFormatName,
   101  			input:          "2000-01-02T20:34:56.123-07:00",
   102  			expectedOutput: time.Date(2000, time.January, 3, 3, 34, 56, .123e9, time.UTC),
   103  		},
   104  		"ISO8601Test millisecond positive utc offset": {
   105  			formatName:     ISO8601TimeFormatName,
   106  			input:          "2000-01-02T20:34:56.123+07:00",
   107  			expectedOutput: time.Date(2000, time.January, 2, 13, 34, 56, .123e9, time.UTC),
   108  		},
   109  		"ISO8601Test nanosecond utc offset": {
   110  			formatName:     ISO8601TimeFormatName,
   111  			input:          "2000-01-02T20:34:56.123456789-07:00",
   112  			expectedOutput: time.Date(2000, time.January, 3, 3, 34, 56, .123456789e9, time.UTC),
   113  		},
   114  		"RFC822Test single digit day": {
   115  			formatName:     RFC822TimeFormatName,
   116  			input:          "Sun, 2 Jan 2000 20:34:56 GMT",
   117  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
   118  		},
   119  		"RFC822Test two digit day": {
   120  			formatName:     RFC822TimeFormatName,
   121  			input:          "Sun, 02 Jan 2000 20:34:56 GMT",
   122  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
   123  		},
   124  		"RFC822Test two digit day year": {
   125  			formatName:     RFC822TimeFormatName,
   126  			input:          "Sun, 2 Jan 00 20:34:56 GMT",
   127  			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
   128  		},
   129  	}
   130  
   131  	for name, c := range cases {
   132  		t.Run(name, func(t *testing.T) {
   133  			timeVal, err := ParseTime(c.formatName, c.input)
   134  			if err != nil {
   135  				t.Errorf("expect no error, got %v", err)
   136  			}
   137  			if e, a := c.expectedOutput, timeVal.UTC(); !e.Equal(a) {
   138  				t.Errorf("expect %v time, got %v", e, a)
   139  			}
   140  		})
   141  	}
   142  }