github.com/damirazo/docker@v1.9.0/pkg/timeutils/utils_test.go (about)

     1  package timeutils
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestGetTimestamp(t *testing.T) {
    10  	now := time.Now()
    11  	cases := []struct{ in, expected string }{
    12  		{"0", "-62167305600"}, // 0 gets parsed year 0
    13  
    14  		// Partial RFC3339 strings get parsed with second precision
    15  		{"2006-01-02T15:04:05.999999999+07:00", "1136189045"},
    16  		{"2006-01-02T15:04:05.999999999Z", "1136214245"},
    17  		{"2006-01-02T15:04:05.999999999", "1136214245"},
    18  		{"2006-01-02T15:04:05", "1136214245"},
    19  		{"2006-01-02T15:04", "1136214240"},
    20  		{"2006-01-02T15", "1136214000"},
    21  		{"2006-01-02T", "1136160000"},
    22  		{"2006-01-02", "1136160000"},
    23  		{"2006", "1136073600"},
    24  		{"2015-05-13T20:39:09Z", "1431549549"},
    25  
    26  		// unix timestamps returned as is
    27  		{"1136073600", "1136073600"},
    28  
    29  		// Durations
    30  		{"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix())},
    31  		{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
    32  		{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
    33  
    34  		// String fallback
    35  		{"invalid", "invalid"},
    36  	}
    37  
    38  	for _, c := range cases {
    39  		o := GetTimestamp(c.in, now)
    40  		if o != c.expected {
    41  			t.Fatalf("wrong value for '%s'. expected:'%s' got:'%s'", c.in, c.expected, o)
    42  		}
    43  	}
    44  }