github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xtime/datetime_test.go (about)

     1  package xtime
     2  
     3  import (
     4  	"github.com/Aoi-hosizora/ahlib/xtesting"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestNewJsonDateAndDateTime(t *testing.T) {
    10  	_ = RFC3339Date
    11  	_ = CJKDate
    12  	_ = RFC3339DateTime
    13  	_ = CJKDateTime
    14  
    15  	for _, tc := range []struct {
    16  		give         time.Time
    17  		wantDate     time.Time
    18  		wantDateTime time.Time
    19  	}{
    20  		{time.Date(2020, time.Month(9), 30, 23, 39, 18, 789, time.FixedZone("test", 8*60*60)),
    21  			time.Date(2020, time.Month(9), 30, 0, 0, 0, 0, time.FixedZone("", 8*60*60)),
    22  			time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 8*60*60))},
    23  
    24  		{time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.UTC),
    25  			time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.FixedZone("", 0)),
    26  			time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.FixedZone("", 0))},
    27  
    28  		{time.Date(2, time.Month(2), 2, 0, 0, 1, 1, time.FixedZone("test_test", -9*3600-30*60)),
    29  			time.Date(2, time.Month(2), 2, 0, 0, 0, 0, time.FixedZone("", -9*3600-30*60)),
    30  			time.Date(2, time.Month(2), 2, 0, 0, 1, 0, time.FixedZone("", -9*3600-30*60))},
    31  	} {
    32  		date := NewJsonDate(tc.give)
    33  		dateTime := NewJsonDateTime(tc.give)
    34  
    35  		xtesting.Equal(t, date.Time(), tc.wantDate)
    36  		xtesting.Equal(t, dateTime.Time(), tc.wantDateTime)
    37  	}
    38  
    39  	now := time.Now()
    40  	xtesting.Equal(t, NewJsonDate(now), NewJsonDate(now))
    41  	xtesting.Equal(t, NewJsonDateTime(now), NewJsonDateTime(now))
    42  	xtesting.Equal(t, NewJsonDate(now).Time(), time.Time(NewJsonDate(now)))
    43  	xtesting.Equal(t, NewJsonDateTime(now).Time(), time.Time(NewJsonDateTime(now)))
    44  	xtesting.Equal(t, NewJsonDate(now).Time(), ToDate(now))
    45  	xtesting.Equal(t, NewJsonDateTime(now).Time(), ToDateTime(now))
    46  }
    47  
    48  func TestMarshalJsonDateAndDateTime(t *testing.T) {
    49  	for _, tc := range []struct {
    50  		give           time.Time
    51  		wantDate       string
    52  		wantDateTime   string
    53  		wantDateBs     []byte
    54  		wantDateTimeBs []byte
    55  	}{
    56  		{time.Date(2020, time.Month(9), 30, 23, 39, 18, 789, time.FixedZone("test", 8*60*60)),
    57  			"2020-09-30", "2020-09-30T23:39:18+08:00", []byte(`"2020-09-30"`), []byte(`"2020-09-30T23:39:18+08:00"`)},
    58  
    59  		{time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.UTC),
    60  			"0001-01-01", "0001-01-01T00:00:00Z", []byte(`"0001-01-01"`), []byte(`"0001-01-01T00:00:00Z"`)},
    61  
    62  		{time.Date(2, time.Month(2), 2, 0, 0, 1, 1, time.FixedZone("test_test", -9*3600-30*60)),
    63  			"0002-02-02", "0002-02-02T00:00:01-09:30", []byte(`"0002-02-02"`), []byte(`"0002-02-02T00:00:01-09:30"`)},
    64  	} {
    65  		date := NewJsonDate(tc.give)
    66  		dateTime := NewJsonDateTime(tc.give)
    67  
    68  		xtesting.Equal(t, date.String(), tc.wantDate)
    69  		xtesting.Equal(t, dateTime.String(), tc.wantDateTime)
    70  
    71  		bs, err := date.MarshalJSON()
    72  		xtesting.Equal(t, bs, tc.wantDateBs)
    73  		xtesting.Nil(t, err, nil)
    74  		bs, err = dateTime.MarshalJSON()
    75  		xtesting.Equal(t, bs, tc.wantDateTimeBs)
    76  		xtesting.Nil(t, err, nil)
    77  	}
    78  }
    79  
    80  func TestUnmarshalJsonDateAndDateTime(t *testing.T) {
    81  	for _, tc := range []struct {
    82  		give           string
    83  		wantDateOk     bool
    84  		wantDate       JsonDate
    85  		wantDateTimeOk bool
    86  		wantDateTime   JsonDateTime
    87  	}{
    88  		{``, false, JsonDate{}, false, JsonDateTime{}},
    89  		{`"null"`, false, JsonDate{}, false, JsonDateTime{}},
    90  		{`2020-09-30`, false, JsonDate{}, false, JsonDateTime{}},
    91  		{`"2020-09-30`, false, JsonDate{}, false, JsonDateTime{}},
    92  		{`"2020-09"`, false, JsonDate{}, false, JsonDateTime{}},
    93  		{`"2020-09-30Z"`, false, JsonDate{}, false, JsonDateTime{}},
    94  		{`2020-09-30T23:39:+08:00`, false, JsonDate{}, false, JsonDateTime{}},
    95  		{`"2020-09-30T23:39:18+08:00`, false, JsonDate{}, false, JsonDateTime{}},
    96  		{`"2020-09-30T23:39"`, false, JsonDate{}, false, JsonDateTime{}},
    97  		{`"2020-09-30T23:39:18"`, false, JsonDate{}, false, JsonDateTime{}},
    98  
    99  		{`null`, true, JsonDate{}, true, JsonDateTime{}},
   100  		{`"2020-09-30"`, true, JsonDate(time.Date(2020, time.Month(9), 30, 0, 0, 0, 0, GetLocalLocation())),
   101  			false, JsonDateTime{}},
   102  		{`"2020-09-30T23:39:18Z"`, false, JsonDate{}, true,
   103  			JsonDateTime(time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 0)))},
   104  		{`"2020-09-30T23:39:18+08:00"`, false, JsonDate{}, true,
   105  			JsonDateTime(time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 8*3600)))},
   106  	} {
   107  		d := JsonDate{}
   108  		err := d.UnmarshalJSON([]byte(tc.give))
   109  		if tc.wantDateOk {
   110  			xtesting.Nil(t, err)
   111  			xtesting.Equal(t, d, tc.wantDate)
   112  		} else {
   113  			xtesting.NotNil(t, err)
   114  		}
   115  
   116  		dt := JsonDateTime{}
   117  		err = dt.UnmarshalJSON([]byte(tc.give))
   118  		if tc.wantDateTimeOk {
   119  			xtesting.Nil(t, err)
   120  			xtesting.Equal(t, dt.Time(), tc.wantDateTime.Time())
   121  		} else {
   122  			xtesting.NotNil(t, err)
   123  		}
   124  	}
   125  }
   126  
   127  func TestScanValueJsonDateAndDateTime(t *testing.T) {
   128  	for _, tc := range []struct {
   129  		give              interface{}
   130  		wantError         bool
   131  		wantDate          JsonDate
   132  		wantDateTime      JsonDateTime
   133  		wantDateValue     time.Time
   134  		wantDateTimeValue time.Time
   135  	}{
   136  		{nil, false, JsonDate{}, JsonDateTime{}, time.Time{}, time.Time{}},
   137  		{0, true, JsonDate{}, JsonDateTime{}, time.Time{}, time.Time{}},
   138  		{time.Date(2020, time.Month(9), 30, 23, 39, 18, 789, time.FixedZone("test", 8*60*60)), false,
   139  			JsonDate(time.Date(2020, time.Month(9), 30, 0, 0, 0, 0, time.FixedZone("", 8*60*60))),
   140  			JsonDateTime(time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 8*60*60))),
   141  			time.Date(2020, time.Month(9), 30, 0, 0, 0, 0, time.FixedZone("", 8*60*60)),
   142  			time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 8*60*60))},
   143  		{time.Date(1, time.Month(1), 1, 1, 1, 1, 1, time.UTC), false,
   144  			JsonDate(time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.FixedZone("", 0))),
   145  			JsonDateTime(time.Date(1, time.Month(1), 1, 1, 1, 1, 0, time.FixedZone("", 0))),
   146  			time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.FixedZone("", 0)),
   147  			time.Date(1, time.Month(1), 1, 1, 1, 1, 0, time.FixedZone("", 0))},
   148  	} {
   149  		d := JsonDate{}
   150  		err := d.Scan(tc.give)
   151  		if tc.wantError {
   152  			xtesting.NotNil(t, err)
   153  		} else {
   154  			xtesting.Equal(t, d, tc.wantDate)
   155  			xtesting.Nil(t, err)
   156  
   157  			val, err := d.Value()
   158  			xtesting.Equal(t, val, tc.wantDateValue)
   159  			xtesting.Nil(t, err)
   160  		}
   161  
   162  		dt := JsonDateTime{}
   163  		err = dt.Scan(tc.give)
   164  		if tc.wantError {
   165  			xtesting.NotNil(t, err)
   166  		} else {
   167  			xtesting.Equal(t, dt, tc.wantDateTime)
   168  			xtesting.Nil(t, err)
   169  
   170  			val, err := dt.Value()
   171  			xtesting.Equal(t, val, tc.wantDateTimeValue)
   172  			xtesting.Nil(t, err)
   173  		}
   174  	}
   175  }
   176  
   177  func TestParseJsonDateAndDateTime(t *testing.T) {
   178  	for _, tc := range []struct {
   179  		give           string
   180  		wantDateOk     bool
   181  		wantDate       JsonDate
   182  		wantDateTimeOk bool
   183  		wantDateTime   JsonDateTime
   184  	}{
   185  		{"", false, JsonDate{}, false, JsonDateTime{}},
   186  		{"2020-09", false, JsonDate{}, false, JsonDateTime{}},
   187  		{"2020-09-30Z", false, JsonDate{}, false, JsonDateTime{}},
   188  		{"2020-09-30T23:39", false, JsonDate{}, false, JsonDateTime{}},
   189  		{"2020-09-30T23:39:18", false, JsonDate{}, false, JsonDateTime{}},
   190  		{"2020-09-30", true, JsonDate(time.Date(2020, time.Month(9), 30, 0, 0, 0, 0, GetLocalLocation())),
   191  			false, JsonDateTime{}},
   192  		{"2020-09-30T23:39:18Z", false, JsonDate{}, true,
   193  			JsonDateTime(time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 0)))},
   194  		{"2020-09-30T23:39:18+08:00", false, JsonDate{}, true,
   195  			JsonDateTime(time.Date(2020, time.Month(9), 30, 23, 39, 18, 0, time.FixedZone("", 8*3600)))},
   196  	} {
   197  		d, err := ParseJsonDate(tc.give)
   198  		if !tc.wantDateOk {
   199  			xtesting.NotNil(t, err)
   200  			now := NewJsonDate(time.Now())
   201  			xtesting.Equal(t, ParseJsonDateOr(tc.give, now), now)
   202  		} else {
   203  			xtesting.Nil(t, err)
   204  			xtesting.Equal(t, d, tc.wantDate)
   205  			now := NewJsonDate(time.Now())
   206  			xtesting.Equal(t, ParseJsonDateOr(tc.give, now), tc.wantDate)
   207  		}
   208  
   209  		dt, err := ParseJsonDateTime(tc.give)
   210  		if !tc.wantDateTimeOk {
   211  			xtesting.NotNil(t, err)
   212  			now := NewJsonDateTime(time.Now())
   213  			xtesting.Equal(t, ParseJsonDateTimeOr(tc.give, now), now)
   214  		} else {
   215  			xtesting.Nil(t, err)
   216  			xtesting.Equal(t, dt, tc.wantDateTime)
   217  			now := NewJsonDateTime(time.Now())
   218  			xtesting.Equal(t, ParseJsonDateTimeOr(tc.give, now), tc.wantDateTime)
   219  		}
   220  	}
   221  }