github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_time_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/os/gtime"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  	"github.com/gogf/gf/v2/util/gconv"
    16  )
    17  
    18  var (
    19  	timeStrTests  = "2024-04-22 12:00:00.123456789+00:00:00"
    20  	timeTimeTests = time.Date(
    21  		2024, 4, 22, 12, 0, 0, 123456789, time.UTC,
    22  	)
    23  )
    24  
    25  func TestTime(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		t.AssertEQ(gconv.Time(nil), time.Time{})
    28  		t.AssertEQ(gconv.Time(timeTimeTests), timeTimeTests)
    29  	})
    30  }
    31  
    32  func TestDuration(t *testing.T) {
    33  	gtest.C(t, func(t *gtest.T) {
    34  		t.AssertEQ(gconv.Duration(nil), time.Duration(0))
    35  		t.AssertEQ(gconv.Duration(timeTimeTests), time.Duration(0))
    36  		t.AssertEQ(gconv.Duration("1m"), time.Minute)
    37  		t.AssertEQ(gconv.Duration(time.Hour), time.Hour)
    38  		t.AssertEQ(gconv.Duration("-1"), time.Duration(-1))
    39  		t.AssertEQ(gconv.Duration("+1"), time.Duration(1))
    40  	})
    41  }
    42  
    43  func TestGtime(t *testing.T) {
    44  	gtest.C(t, func(t *gtest.T) {
    45  		t.AssertEQ(gconv.GTime(""), gtime.New())
    46  		t.AssertEQ(gconv.GTime(nil), nil)
    47  
    48  		t.AssertEQ(gconv.GTime(gtime.New(timeStrTests)), gtime.New(timeStrTests))
    49  		t.AssertEQ(gconv.GTime(timeTimeTests).Year(), 2024)
    50  		t.AssertEQ(gconv.GTime(timeTimeTests).Month(), 4)
    51  		t.AssertEQ(gconv.GTime(timeTimeTests).Day(), 22)
    52  		t.AssertEQ(gconv.GTime(timeTimeTests).Hour(), 12)
    53  		t.AssertEQ(gconv.GTime(timeTimeTests).Minute(), 0)
    54  		t.AssertEQ(gconv.GTime(timeTimeTests).Second(), 0)
    55  		t.AssertEQ(gconv.GTime(timeTimeTests).Nanosecond(), 123456789)
    56  		t.AssertEQ(gconv.GTime(timeTimeTests).String(), "2024-04-22 12:00:00")
    57  	})
    58  }