github.com/searKing/golang/go@v1.2.117/time/time_test.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time_test
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  	"time"
    11  
    12  	time_ "github.com/searKing/golang/go/time"
    13  )
    14  
    15  func TestTruncateByLocation(t *testing.T) {
    16  	cst, err := time.LoadLocation("Asia/Shanghai")
    17  	if err != nil {
    18  		t.Fatalf("malformed location: %s: %s", "Asia/Shanghai", err)
    19  	}
    20  
    21  	tests := []struct {
    22  		t                  time.Time
    23  		d                  time.Duration
    24  		wantTimeByUTC      time.Time
    25  		wantTimeByLocation time.Time
    26  	}{
    27  		{
    28  			t:                  time.Date(2012, time.January, 1, 4, 15, 30, 0, time.UTC),
    29  			d:                  time.Hour * 24,
    30  			wantTimeByUTC:      time.Date(2012, time.January, 1, 0, 0, 0, 0, time.UTC),
    31  			wantTimeByLocation: time.Date(2012, time.January, 1, 0, 0, 0, 0, time.UTC),
    32  		},
    33  		{
    34  			t:                  time.Date(2012, time.January, 1, 4, 15, 30, 0, cst),
    35  			d:                  time.Hour * 24,
    36  			wantTimeByUTC:      time.Date(2011, time.December, 31, 8, 0, 0, 0, cst),
    37  			wantTimeByLocation: time.Date(2012, time.January, 1, 0, 0, 0, 0, cst),
    38  		},
    39  	}
    40  
    41  	for i, tt := range tests {
    42  		gotTime := tt.t.Truncate(tt.d)
    43  		if tt.wantTimeByUTC != gotTime {
    44  			t.Errorf("#%d: Truncate expected %q got %q", i, tt.wantTimeByUTC, gotTime)
    45  		}
    46  		gotTime = time_.TruncateByLocation(tt.t, tt.d)
    47  		if tt.wantTimeByLocation != gotTime {
    48  			t.Errorf("#%d: TruncateByLocation expected %q got %q", i, tt.wantTimeByLocation, gotTime)
    49  		}
    50  	}
    51  }
    52  
    53  var jsonTests = []struct {
    54  	duration time_.Duration
    55  	json     string
    56  }{
    57  	// simple
    58  	{0, `"0s"`},
    59  	{time_.Duration(5 * time.Second), `"5s"`},
    60  	{time_.Duration(30 * time.Second), `"30s"`},
    61  	{time_.Duration(1478 * time.Second), `"24m38s"`},
    62  	// sign
    63  	{time_.Duration(-5 * time.Second), `"-5s"`},
    64  	{time_.Duration(5 * time.Second), `"5s"`},
    65  	// decimal
    66  	{time_.Duration(5*time.Second + 600*time.Millisecond), `"5.6s"`},
    67  	{time_.Duration(5*time.Second + 4*time.Millisecond), `"5.004s"`},
    68  	{time_.Duration(100*time.Second + 1*time.Millisecond), `"1m40.001s"`},
    69  	// different units
    70  	{time_.Duration(10 * time.Nanosecond), `"10ns"`},
    71  	{time_.Duration(11 * time.Microsecond), `"11µs"`},
    72  	{time_.Duration(13 * time.Millisecond), `"13ms"`},
    73  	{time_.Duration(14 * time.Second), `"14s"`},
    74  	{time_.Duration(15 * time.Minute), `"15m0s"`},
    75  	{time_.Duration(16 * time.Hour), `"16h0m0s"`},
    76  	// composite durations
    77  	{time_.Duration(3*time.Hour + 30*time.Minute), `"3h30m0s"`},
    78  	{time_.Duration(49*time.Minute + 48*time.Second + 372539827*time.Nanosecond), `"49m48.372539827s"`},
    79  }
    80  
    81  func TestDurationJSON(t *testing.T) {
    82  	for _, tt := range jsonTests {
    83  		var jsonDuration time_.Duration
    84  		if jsonBytes, err := json.Marshal(tt.duration); err != nil {
    85  			t.Errorf("%v json.Marshal error = %v, want nil", tt.duration, err)
    86  		} else if string(jsonBytes) != tt.json {
    87  			t.Errorf("%v JSON = %#q, want %#q", tt.duration, string(jsonBytes), tt.json)
    88  		} else if err = json.Unmarshal(jsonBytes, &jsonDuration); err != nil {
    89  			t.Errorf("%v json.Unmarshal error = %v, want nil", tt.duration, err)
    90  		}
    91  	}
    92  }
    93  
    94  func TestInvalidDurationJSON(t *testing.T) {
    95  	var tt time_.Duration
    96  	err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt)
    97  	_, isParseErr := err.(*time.ParseError)
    98  	if !isParseErr {
    99  		t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err)
   100  	}
   101  }