github.com/artpar/rclone@v1.67.3/fs/parsetime_test.go (about)

     1  package fs
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // Check it satisfies the interfaces
    14  var (
    15  	_ flagger   = (*Time)(nil)
    16  	_ flaggerNP = Time{}
    17  )
    18  
    19  func TestParseTime(t *testing.T) {
    20  	now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
    21  	oldTimeNowFunc := timeNowFunc
    22  	timeNowFunc = func() time.Time { return now }
    23  	defer func() { timeNowFunc = oldTimeNowFunc }()
    24  
    25  	for _, test := range []struct {
    26  		in   string
    27  		want time.Time
    28  		err  bool
    29  	}{
    30  		{"", time.Time{}, true},
    31  		{"1ms", now.Add(-time.Millisecond), false},
    32  		{"1s", now.Add(-time.Second), false},
    33  		{"1", now.Add(-time.Second), false},
    34  		{"1m", now.Add(-time.Minute), false},
    35  		{"1.5m", now.Add(-(3 * time.Minute) / 2), false},
    36  		{"1h", now.Add(-time.Hour), false},
    37  		{"1d", now.Add(-time.Hour * 24), false},
    38  		{"1w", now.Add(-time.Hour * 24 * 7), false},
    39  		{"1M", now.Add(-time.Hour * 24 * 30), false},
    40  		{"1y", now.Add(-time.Hour * 24 * 365), false},
    41  		{"1.5y", now.Add(-time.Hour * 24 * 365 * 3 / 2), false},
    42  		{"-1.5y", now.Add(time.Hour * 24 * 365 * 3 / 2), false},
    43  		{"-1s", now.Add(time.Second), false},
    44  		{"-1", now.Add(time.Second), false},
    45  		{"0", now, false},
    46  		{"100", now.Add(-100 * time.Second), false},
    47  		{"-100", now.Add(100 * time.Second), false},
    48  		{"1.s", now.Add(-time.Second), false},
    49  		{"1x", time.Time{}, true},
    50  		{"-1x", time.Time{}, true},
    51  		{"off", time.Time{}, false},
    52  		{"1h2m3s", now.Add(-(time.Hour + 2*time.Minute + 3*time.Second)), false},
    53  		{"2001-02-03", time.Date(2001, 2, 3, 0, 0, 0, 0, time.Local), false},
    54  		{"2001-02-03 10:11:12", time.Date(2001, 2, 3, 10, 11, 12, 0, time.Local), false},
    55  		{"2001-08-03 10:11:12", time.Date(2001, 8, 3, 10, 11, 12, 0, time.Local), false},
    56  		{"2001-02-03T10:11:12", time.Date(2001, 2, 3, 10, 11, 12, 0, time.Local), false},
    57  		{"2001-02-03T10:11:12.123Z", time.Date(2001, 2, 3, 10, 11, 12, 123000000, time.UTC), false},
    58  		{"2001-02-03T10:11:12.123+00:00", time.Date(2001, 2, 3, 10, 11, 12, 123000000, time.UTC), false},
    59  	} {
    60  		parsedTime, err := ParseTime(test.in)
    61  		if test.err {
    62  			require.Error(t, err, test.in)
    63  		} else {
    64  			require.NoError(t, err, test.in)
    65  		}
    66  		assert.True(t, test.want.Equal(parsedTime), "%v should be parsed as %v instead of %v", test.in, test.want, parsedTime)
    67  	}
    68  }
    69  
    70  func TestTimeString(t *testing.T) {
    71  	now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
    72  	oldTimeNowFunc := timeNowFunc
    73  	timeNowFunc = func() time.Time { return now }
    74  	defer func() { timeNowFunc = oldTimeNowFunc }()
    75  
    76  	for _, test := range []struct {
    77  		in   time.Time
    78  		want string
    79  	}{
    80  		{now, "2020-09-05T08:15:05.00000025Z"},
    81  		{time.Date(2021, 8, 5, 8, 15, 5, 0, time.UTC), "2021-08-05T08:15:05Z"},
    82  		{time.Time{}, "off"},
    83  	} {
    84  		got := Time(test.in).String()
    85  		assert.Equal(t, test.want, got)
    86  		// Test the reverse
    87  		reverse, err := ParseTime(test.want)
    88  		assert.NoError(t, err)
    89  		assert.Equal(t, test.in, reverse)
    90  	}
    91  }
    92  
    93  func TestTimeScan(t *testing.T) {
    94  	now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
    95  	oldTimeNowFunc := timeNowFunc
    96  	timeNowFunc = func() time.Time { return now }
    97  	defer func() { timeNowFunc = oldTimeNowFunc }()
    98  
    99  	for _, test := range []struct {
   100  		in   string
   101  		want Time
   102  	}{
   103  		{"17m", Time(now.Add(-17 * time.Minute))},
   104  		{"-12h", Time(now.Add(12 * time.Hour))},
   105  		{"0", Time(now)},
   106  		{"off", Time(time.Time{})},
   107  		{"2022-03-26T17:48:19Z", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC))},
   108  		{"2022-03-26 17:48:19", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.Local))},
   109  	} {
   110  		var got Time
   111  		n, err := fmt.Sscan(test.in, &got)
   112  		require.NoError(t, err)
   113  		assert.Equal(t, 1, n)
   114  		assert.Equal(t, test.want, got)
   115  	}
   116  }
   117  
   118  func TestParseTimeUnmarshalJSON(t *testing.T) {
   119  	now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
   120  	oldTimeNowFunc := timeNowFunc
   121  	timeNowFunc = func() time.Time { return now }
   122  	defer func() { timeNowFunc = oldTimeNowFunc }()
   123  
   124  	for _, test := range []struct {
   125  		in   string
   126  		want time.Time
   127  		err  bool
   128  	}{
   129  		{`""`, time.Time{}, true},
   130  		{"0", time.Time{}, true},
   131  		{"1", time.Time{}, true},
   132  		{"1", time.Time{}, true},
   133  		{`"2022-03-26T17:48:19Z"`, time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC), false},
   134  		{`"0"`, now, false},
   135  		{`"1ms"`, now.Add(-time.Millisecond), false},
   136  		{`"1s"`, now.Add(-time.Second), false},
   137  		{`"1"`, now.Add(-time.Second), false},
   138  		{`"1m"`, now.Add(-time.Minute), false},
   139  		{`"1h"`, now.Add(-time.Hour), false},
   140  		{`"-1h"`, now.Add(time.Hour), false},
   141  		{`"1d"`, now.Add(-time.Hour * 24), false},
   142  		{`"1w"`, now.Add(-time.Hour * 24 * 7), false},
   143  		{`"1M"`, now.Add(-time.Hour * 24 * 30), false},
   144  		{`"1y"`, now.Add(-time.Hour * 24 * 365), false},
   145  		{`"off"`, time.Time{}, false},
   146  		{`"error"`, time.Time{}, true},
   147  		{"error", time.Time{}, true},
   148  	} {
   149  		var parsedTime Time
   150  		err := json.Unmarshal([]byte(test.in), &parsedTime)
   151  		if test.err {
   152  			require.Error(t, err, test.in)
   153  		} else {
   154  			require.NoError(t, err, test.in)
   155  		}
   156  		assert.Equal(t, Time(test.want), parsedTime, test.in)
   157  	}
   158  }
   159  
   160  func TestParseTimeMarshalJSON(t *testing.T) {
   161  	for _, test := range []struct {
   162  		in   time.Time
   163  		want string
   164  		err  bool
   165  	}{
   166  		{time.Time{}, `"0001-01-01T00:00:00Z"`, false},
   167  		{time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC), `"2022-03-26T17:48:19Z"`, false},
   168  	} {
   169  		gotBytes, err := json.Marshal(test.in)
   170  		got := string(gotBytes)
   171  		if test.err {
   172  			require.Error(t, err, test.in)
   173  		} else {
   174  			require.NoError(t, err, test.in)
   175  		}
   176  		assert.Equal(t, test.want, got, test.in)
   177  	}
   178  }