github.com/gogf/gf/v2@v2.7.4/os/gtime/gtime_z_unit_issue_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 gtime_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  )
    16  
    17  // https://github.com/gogf/gf/issues/1681
    18  func Test_Issue1681(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		t.Assert(gtime.New("2022-03-08T03:01:14-07:00").Local().Time, gtime.New("2022-03-08T10:01:14Z").Local().Time)
    21  		t.Assert(gtime.New("2022-03-08T03:01:14-08:00").Local().Time, gtime.New("2022-03-08T11:01:14Z").Local().Time)
    22  		t.Assert(gtime.New("2022-03-08T03:01:14-09:00").Local().Time, gtime.New("2022-03-08T12:01:14Z").Local().Time)
    23  		t.Assert(gtime.New("2022-03-08T03:01:14+08:00").Local().Time, gtime.New("2022-03-07T19:01:14Z").Local().Time)
    24  	})
    25  }
    26  
    27  // https://github.com/gogf/gf/issues/2803
    28  func Test_Issue2803(t *testing.T) {
    29  	gtest.C(t, func(t *gtest.T) {
    30  		newTime := gtime.New("2023-07-26").LayoutTo("2006-01")
    31  		t.Assert(newTime.Year(), 2023)
    32  		t.Assert(newTime.Month(), 7)
    33  		t.Assert(newTime.Day(), 1)
    34  		t.Assert(newTime.Hour(), 0)
    35  		t.Assert(newTime.Minute(), 0)
    36  		t.Assert(newTime.Second(), 0)
    37  	})
    38  }
    39  
    40  // https://github.com/gogf/gf/issues/3558
    41  func Test_Issue3558(t *testing.T) {
    42  	gtest.C(t, func(t *gtest.T) {
    43  		timeStr := "1880-10-24T00:00:00+08:05"
    44  		gfTime := gtime.NewFromStr(timeStr)
    45  		t.Assert(gfTime.Year(), 1880)
    46  		t.Assert(gfTime.Month(), 10)
    47  		t.Assert(gfTime.Day(), 24)
    48  		t.Assert(gfTime.Hour(), 0)
    49  		t.Assert(gfTime.Minute(), 0)
    50  		t.Assert(gfTime.Second(), 0)
    51  
    52  		stdTime, err := time.Parse(time.RFC3339, timeStr)
    53  		t.AssertNil(err)
    54  		stdTimeFormat := stdTime.Format("2006-01-02 15:04:05")
    55  		gfTimeFormat := gfTime.Format("Y-m-d H:i:s")
    56  		t.Assert(gfTimeFormat, stdTimeFormat)
    57  	})
    58  	gtest.C(t, func(t *gtest.T) {
    59  		timeStr := "1880-10-24T00:00:00-08:05"
    60  		gfTime := gtime.NewFromStr(timeStr)
    61  		t.Assert(gfTime.Year(), 1880)
    62  		t.Assert(gfTime.Month(), 10)
    63  		t.Assert(gfTime.Day(), 24)
    64  		t.Assert(gfTime.Hour(), 0)
    65  		t.Assert(gfTime.Minute(), 0)
    66  		t.Assert(gfTime.Second(), 0)
    67  		stdTime, err := time.Parse(time.RFC3339, timeStr)
    68  		t.AssertNil(err)
    69  		stdTimeFormat := stdTime.Format("2006-01-02 15:04:05")
    70  		gfTimeFormat := gfTime.Format("Y-m-d H:i:s")
    71  		t.Assert(gfTimeFormat, stdTimeFormat)
    72  	})
    73  }